Question

I'm developing an application that made sense that I wrapped my classes around the implemented QTcpSocket (since it's a TCP protocol, and I need to be able to use the raw socket and the added features as seemlessly as possible).

So, for the sake of simplicity, my code is structured like this:

class MyTCPSocket : public QTcpSocket
{
 Q_OBJECT
 // ....
}

class MyTCPServer : public QTcpServer
{
 Q_OBJECT
 // ....
}

And with QTcpServer, the default nextPendingConnection() function returns an instance of QTcpSocket, which I can't cast up to MyTCPSocket (as far as I'm aware, QTcpSocket doesn't have a copy constructure).

What's my best option for handing this? I was thinking that I could create a new MyTCPSocket and simply set the socket descriptor to the one returned by nextPendingConnection() from MyTCPServer, but I wasn't sure how that would work when the old socket gets deleted.

Is there a better way to handle this situation?

Was it helpful?

Solution

Exactly that: override incomingConnection but don't call the base class, instead create an instance of your QTcpSocket subclass and use setSocketDescriptor on it. If you want to keep QTcpSocket "signal behaviour", also call addPendingConnection and emit newConnection.

Then just downcast the socket you get from nextPendingConnection to your QTcpSocket subclass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top