Results 1 to 20 of 42

Thread: Problem by subclassing the QIODevice

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem by subclassing the QIODevice

    Quote Originally Posted by erqsor View Post
    As I said at begining, I have some problems :
    - Inherit the QWsServer from QTcpServer : I can't because the newConnection signal is already used in background by the QTcpServer. And I can't offer this signal to the QWsServer users by inheritance.
    - Inherit the QWsSocket from QTcpSocket : I can't for the same reason with the readyRead signal.

    So I have not used inheritance, but aggregation (I don't know if thats the right word in English). So the QWsServer has a ptr to a QTcpServer as member. And the QWsSocket has a QTcpSocket ptr as member too.
    QWsServer inherits from QObject and QWsSocket inherits from QAbstratSocket.
    And finaly, for now I created my own signals and fonctions to read data in a QWsSocket, but I wanted to use the QAbstractSocket features to the fullest, and so reimplementing the common read functions and signals.
    That's all ok however in my opinion there is just no point in deriving your QWsSocket from QAbstractSocket. QIODevice would do just fine and you could reimplement only those methods you really needed (unfortunately not only the two you mentioned in the beginning).

    And here is why I think the way I think... Unless you want to implement the client side as well, your QWsSocket won't sanely implement the following methods from QAbstractSocket:

    Qt Code:
    1. void connectToHost ( const QString & hostName, quint16 port, OpenMode openMode = ReadWrite )
    2. void connectToHost ( const QHostAddress & address, quint16 port, OpenMode openMode = ReadWrite )
    3. QNetworkProxy proxy () const
    4. void setProxy ( const QNetworkProxy & networkProxy )
    5. bool waitForConnected ( int msecs = 30000 )
    6. bool waitForDisconnected ( int msecs = 30000 )
    To copy to clipboard, switch view to plain text mode 

    So what is the point in having those methods around?

    Correct me if I'm wrong, but what your users need is:
    Qt Code:
    1. read()
    2. write()
    3. close()
    4. bytesAvailable()
    5. signal readyRead()
    6. signal bytesWritten()
    7. QHostAddress peerAddress()
    8. quint16 peerPort()
    To copy to clipboard, switch view to plain text mode 

    Apart from the last two methods the rest is defined in QIODevice. So you are leaving 6 dangling methods just for the benefit of having other two methods that are pretty simple to implement.

    A potential advantage of subclassing QAbstractSocket would be if someone wanted to replace one socket implementation for another. Unfortunately protocols differ so much that in 99% of the cases it is just not possible (and where it is possible, you can just use QIODevice API). So what is the point of deriving from QAbstractSocket?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem by subclassing the QIODevice

    I developped an application with my API (So that's a project that uses my websocket server API). And I found usefull to allow server to permit connexion by using one QTcpServer and one QWsServer (So users can connect by two ways : a Qt application that uses TCP protocol, and a web application that uses websocket protocol). And next I stored the socket with a total abstraction of the protocol in a member of my client class (QAbstractSocket * socket) that can stores a QTcpSocket or a QWsSocket (never both). And I want to use the write and read methods with a total abstraction of the type of the socket to simplify and make more powerful the use of the API.
    I want users uses the QWsSocket like a QTcpSocket, so I redirect the peerAddress, peerPort, state, socketType functions to the QAbstractSocket (and I need too the connected, disconnected, stateChanged signals). So if my QWsSocket doesn't inherit from QAbstractSocket, I can't use this network part of QIODevice I need.
    Last edited by erqsor; 17th January 2012 at 02:36.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem by subclassing the QIODevice

    The abstraction of the protocol probably could have as well used QIODevice instead of QAbstractSocket (since protocols care only about data exchange and not who they are talking to and how). The signal connected() is useless on the server side since when the server accepts a connection, the socket is already in the connected state. Disconnected() is pretty much useless for the server as well and even if not, adding that signal is not a problem. The only two meaningful SocketState values for the server side of the connection are Connected and Closing since the server is accepting the connection so it doesn't do host lookup, it doesn't connect and it's not listening (since it is an incoming connection). However, I'm not going to convince you anymore, it is your project and I just hope people using your code think exactly the same way you do, otherwise you'll get a lot of stupid "why doesn't it work" questions from them.

    By the way, there is this thing called QLocalSocket. It's derived from QIODevice and not QAbstractSocket. To be honest I wondered many times why QUdpSocket inherits QAbstractSocket since it's neither connecting nor streaming. I wouldn't be suprised if in Qt5 QAbstractSocket simply vanished into thin air.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem by subclassing the QIODevice

    Yes, you said interesting things, but I really want to construct my API like the QTcpSocket and QUdpSocket (and other sockets), That's why I did it like this. I don't want to limit my users. For a personnal project, I will do like you say, but here I think I can't do that :/

    Anyway, I did some functions that works:
    Qt Code:
    1. qint64 QWsSocket::bytesAvailable() const
    2. {
    3. return buffer.size();
    4. }
    5.  
    6. qint64 QWsSocket::readData( char * data, qint64 maxSize )
    7. {
    8. int dataSize=buffer.size();
    9. int i=0;
    10. while( i<maxSize && i<dataSize )
    11. {
    12. data[i] = buffer.dequeue().toAscii();
    13. i++;
    14. }
    15. return i;
    16. }
    17.  
    18. qint64 QWsSocket::writeData( const char * data, qint64 maxSize )
    19. {
    20. return tcpSocket->write(data, maxSize);
    21. }
    22.  
    23.  
    24. QByteArray QWsSocket::readAll()
    25. {
    26. qint64 sz = bytesAvailable();
    27. char * data = new char[sz];
    28. sz = readData(data, sz);
    29. return QByteArray(data, sz);
    30. }
    31.  
    32. bool QWsSocket::getChar(char * c)
    33. {
    34. *c = 0;
    35. if ( bytesAvailable() == 0 )
    36. return false;
    37.  
    38. readData(c, 1);
    39. return true;
    40. }
    41.  
    42. qint64 QWsSocket::read(char * data, qint64 maxSize)
    43. {
    44. return readData(data, maxSize);
    45. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem by subclassing the QIODevice

    Quote Originally Posted by erqsor View Post
    but I really want to construct my API like the QTcpSocket and QUdpSocket (and other sockets)
    What other sockets?

    I don't want to limit my users
    That's what you're currently doing.
    Qt Code:
    1. qint64 QWsSocket::bytesAvailable() const
    2. {
    3. return buffer.size();
    4. }
    To copy to clipboard, switch view to plain text mode 
    That's incorrect. See docs for QIODevice::bytesAvailable().


    Qt Code:
    1. QByteArray QWsSocket::readAll()
    2. {
    3. qint64 sz = bytesAvailable();
    4. char * data = new char[sz];
    5. sz = readData(data, sz);
    6. return QByteArray(data, sz);
    7. }
    To copy to clipboard, switch view to plain text mode 
    That's incorrect. readAll() is not virtual.


    Qt Code:
    1. qint64 QWsSocket::read(char * data, qint64 maxSize)
    2. {
    3. return readData(data, maxSize);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Incorrect, read() is not virtual.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem by subclassing the QIODevice

    I don't want to limit my users
    That's what you're currently doing.
    I'm doing like Qt.

    That's incorrect. readAll() is not virtual.
    Incorrect, read() is not virtual.
    So what can I do ? QIODevice is useless if I cant use this functions...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem by subclassing the QIODevice

    Quote Originally Posted by erqsor View Post
    I'm doing like Qt.
    What "Qt does" is not always correct. And you're not "doing like Qt". Qt tends to inherit QIODevice rather than its subclasses.

    So what can I do ? QIODevice is useless if I cant use this functions...
    Those methods are already implemented, you don't implement them yourself. Implement readData(), writeData(), bytesAvailable(), waitForBytesWritten(), waitForReadyRead(), possibly canReadLine() and readLineData(). QAbstractSocket uses an internal "socket engine" which does most of the work. If you want to implement your own subclass of QAbstractSocket class, you have to shadow all that because you don't have a working socket engine for your protocol underneath since your QWsSocket is not a real native socket. So you have to rewrite pretty much everything QAbstractSocket does (apart from close(), isSequential() and atEnd()).

    The more I look at the source code of QAbstractSocket the more I think this class was simply not meant to be subclassed outside Qt tree (not in this particular case but rather not meant to be subclassed at all). It has hardcoded support for UDP, TCP and SSL and doesn't allow hooking into the code with other implementations. QTcpSocket and QUdpSocket are simply stubs over QAbstractSocket which implements everything from both protocols itself (or actually delegates everything to a subclass of QAbstractSocketEngine such as QNativeSocketEngine).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QIODevice read() problem (reads more than maxSize)
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 22nd February 2011, 12:09
  2. Subclassing QSortFilterProxyModel problem
    By e79ene in forum Newbie
    Replies: 2
    Last Post: 21st February 2011, 14:23
  3. Phonon: subclassing QIODevice
    By iDm.MuFFin123 in forum Qt Programming
    Replies: 0
    Last Post: 23rd May 2010, 14:29
  4. Subclassing QMainWindow problem
    By lerwys in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 09:40
  5. Problem with QTreeWidget after subclassing
    By steve918 in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2006, 19:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.