[Qt-interest] Subclassing QTcpSocket

Jason H scorp1us at yahoo.com
Wed Apr 20 16:03:42 CEST 2011


I would like to make an observation, and invite comment.

It is not immediately obvious, and what I've found is it is often better to to 
not subclass TCP socket to add application features, like client header. 


It is my observation that you should use a raw QTcpSocket which directs bytes to 
your own class, which is connected() to the socket instance.  Then this class 
provides your application logic. Immediately, you can then use QSslSocket with 
your class. True, QSslSocket inherits QTcpSpcket, but what do you have to change 
(source code) to use QSslSocket? You'd have to make a QSslSocket derived class 
with the same functionality, and you then have code duplication. 


Given that the socket classes are QIODevices, wouldn't it be cooler if you could 
connect() your application logic to any QIODevice? Say a file or compressed byte 
stream (QtIOCompressor) ? What I have found in my experience is that between the 
socket and your application, there can be many layers. For instance, in HTTP, a 
worst case example would be:
ChunkedLengthDecoding->MimeDecoding->Base64Decoding->actual data. If you had a 
class to handle each stage, you can layer the byte stream as needed
socket.connectTo(&chunkedLengthDecoder) ;
chunkedLengthDecoder.connectTo(&mimePartDecoder);
mimePartDecoder.connectTo(&base64Decoder);
base64Decoder.connectTo(&applicationLogic); // or 
zipDecoder.connectTo(&applicationLogic)

where connectTo() minimally connects the reading and writing signals and slots 
(readyRead, write(), etc) Not matter the translation scheme, your application 
logic is isolated from the communications.





----- Original Message ----
From: Sven Grunewaldt <strayer at olle-orks.org>
To: qt-interest at qt.nokia.com
Sent: Wed, April 20, 2011 8:52:36 AM
Subject: [Qt-interest] Subclassing QTcpSocket

Hi,

I want to attach some values to a QTcpSocket so I'm sublassing it like this:

class ClientData
{
public:
    QByteArray clientDataBuffer;
    HttpHeaders clientHeaders;
};

class QTcpSocketEx : public QTcpSocket
{
    Q_OBJECT

public:
    QTcpSocketEx(QObject *parent = 0) : QTcpSocket(parent) {}

    ClientData clientData;
};

I use the new class in my subclassed QTcpServer like this:
while (hasPendingConnections())
{
    QTcpSocket *socket = nextPendingConnection();
    QTcpSocketEx *client = qobject_cast<QTcpSocketEx*>(socket);

    qDebug() << socket << client;
}

Sadly this does not seem to work. The output is always
QTcpSocket(0x22318e0) QObject(0x0)

What am I doing wrong?

Regards,
Sven Grunewaldt
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list