Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 42

Thread: Problem by subclassing the QIODevice

  1. #21
    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
    OK, so can you say to me how can I replace my QTcpServer by a QNetworkReply ? The QtAssistant doesn't illustrate this case...
    For now, I'm using the QTcpServer::listen function for waiting client connexions, and creating a QTcpSocket to communicate with this client.
    Come on. Read my posts carefully, I never said you should replace QTcpServer with QNetworkReply.
    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. #22
    Join Date
    Jun 2010
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem by subclassing the QIODevice

    Ok, misunderstanding, sorry...

    No. I thought you were interested in the client side because QAbstractSocket represents a client side of the connection. The server is implemented by Q*Server.
    But I need sockets in server side ^^

    Anyway, I wanted to do my API like this : The user uses these two ws objects (QWsServer, QWsSocket) as he uses these two tcp objects (QTcpServer, QTcpSocket).
    The user don't use tcp objects: The TCP instructions are done in background (hidden).

    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.

    I hope what I said is clear !
    Last edited by erqsor; 17th January 2012 at 00:19.

  3. #23
    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.


  4. #24
    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 01:36.

  5. #25
    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.


  6. #26
    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 

  7. #27
    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.


  8. #28
    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...

  9. #29
    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.


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

    Default Re: Problem by subclassing the QIODevice

    OK, I deleted these functions:
    Qt Code:
    1. qint64 read( char * data, qint64 maxSize );
    2. QByteArray readAll();
    3. bool getChar( char * c );
    To copy to clipboard, switch view to plain text mode 

    But that does not work anymore.
    The implementation of the others functions, can you take a look ? (only for the read part)

    Qt Code:
    1. bool QWsSocket::atEnd() const
    2. {
    3. if ( buffer.size() )
    4. return false;
    5. return true;
    6. }
    7. bool QWsSocket::canReadLine() const
    8. {
    9. return buffer.contains('\n') || QIODevice::canReadLine();
    10. }
    11. bool QWsSocket::isSequential() const
    12. {
    13. return true;
    14. }
    15. qint64 QWsSocket::readData( char * data, qint64 maxSize )
    16. {
    17. int dataSize = buffer.size();
    18. int i = 0;
    19. while( i<maxSize && i<dataSize )
    20. {
    21. data[i] = buffer.dequeue().toAscii();
    22. i++;
    23. }
    24. return i;
    25. }
    26. qint64 QWsSocket::readLineData( char * data, qint64 maxSize )
    27. {
    28. int dataSize = buffer.size();
    29. int i = 0;
    30. bool endLineReached = false;
    31. while( i<maxSize && i<dataSize && !endLineReached )
    32. {
    33. char c = buffer.dequeue().toAscii();
    34. if ( c == '\n' )
    35. endLineReached = true;
    36. data[i] = c;
    37. i++;
    38. }
    39. return i;
    40. }
    To copy to clipboard, switch view to plain text mode 


    Added after 9 minutes:


    And I must precise that I tried to read data from the QIODevice with readAll and read(bytesAvailable) functions
    Last edited by erqsor; 17th January 2012 at 15:02.

  11. #31
    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
    Qt Code:
    1. qint64 QWsSocket::readData( char * data, qint64 maxSize )
    2. {
    3. int dataSize = buffer.size();
    4. int i = 0;
    5. while( i<maxSize && i<dataSize )
    6. {
    7. data[i] = buffer.dequeue().toAscii();
    8. i++;
    9. }
    10. return i;
    11. }
    To copy to clipboard, switch view to plain text mode 
    This is suboptimal (aka slow). Try this:

    Qt Code:
    1. qint64 QWsSocket::readData( char * data, qint64 maxSize )
    2. {
    3. int dataSize = buffer.size(); // assuming buffer is QByteArray
    4. int i = qMin(maxSize, dataSize);
    5. if(i==0) return 0;
    6. memcpy(data, buffer.data(), i);
    7. buffer.remove(0, i);
    8. return i;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qint64 QWsSocket::readLineData( char * data, qint64 maxSize )
    2. {
    3. int dataSize = buffer.size();
    4. int i = 0;
    5. bool endLineReached = false;
    6. while( i<maxSize && i<dataSize && !endLineReached )
    7. {
    8. char c = buffer.dequeue().toAscii();
    9. if ( c == '\n' )
    10. endLineReached = true;
    11. data[i] = c;
    12. i++;
    13. }
    14. return i;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Suboptimal. First search for the newline and then read everything in one go.

    This all doesn't change the fact that you have to override each and every method implemented by QAbstractSocket because implementations from that class are incompatible with yours.
    Last edited by wysota; 17th January 2012 at 15:25.
    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.


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

    Default Re: Problem by subclassing the QIODevice

    Thanks for the optimization.

    This all doesn't change the fact that you have to override each and every method implemented by QAbstractSocket because implementations from that class are incompatible with yours.
    What do you mean ?

    OK, the read and readAll functions I removed they were not virtual, but what should I do instead ? I've already re-implemented all the read functions I can :/
    Last edited by erqsor; 17th January 2012 at 15:56.

  13. #33
    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
    What do you mean ?
    See post #29.

    OK, the read and readAll functions I removed they were not virtual, but what should I do instead ? I've already re-implemented all the read functions I can :/
    Then don't limit yourself to read functions, reimplement the rest as well (you need to shadow all implementation done by QAbstractSocket, including those methods which are introduced by that class. Or... derive from QIODevice instead of 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.


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

    Default Re: Problem by subclassing the QIODevice

    Quote Originally Posted by wysota View Post
    Then don't limit yourself to read functions, reimplement the rest as well (you need to shadow all implementation done by QAbstractSocket, including those methods which are introduced by that class. Or... derive from QIODevice instead of QAbstractSocket
    But you will cry if I re-implement functions that are not virtual (in QIODevice or QAbstractSocket).

  15. #35
    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

    I never said you were to reimplement any non-virtual methods.
    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.


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

    Default Re: Problem by subclassing the QIODevice

    OK, I had not time to work on it for 1 week. So I will see that when I can.

    Anyway, I have one change in my project, somebody asked me to enable the socket client feature. So I think I will enable it soon.

    I wanted to speak about another problem. The socketDescriptor functions allow tcp server users to use the socket in another threads. My architecture don't allow this (because my QWsSocket are not real sockets, so I can't give a socketDescriptor of the WsSocket, I can't just give the socketDescriptor of the TcpSocket, but that's not a good idea (I think)). So I need to find a solution. If you have any Idea, that could be cool

  17. #37
    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

    Your socket still has a real socket descriptor, it is the TCP socket you are using. So all you need to do is to make sure that moving your socket to another thread also moves the TCP socket with it (for example by making the TCP socket a child of the websocket). So advise your users to use QObject::moveToThread() rather than manipulating the socket descriptor.
    Last edited by wysota; 25th January 2012 at 14:19.
    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.


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

    Default Re: Problem by subclassing the QIODevice

    OK, that's a good idea. Anyway I think I can't do that by another way
    Thanks

  19. #39
    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

    You don't need to do anything at all, to be honest. The socket will do perfectly fine in the same thread along all the other sockets.
    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.


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

    Default Re: Problem by subclassing the QIODevice

    OK, In my actual code, I declare the QWsSocket as the parent of the QTcpSocket, So I think I have nothing to do more. I will test that.

Similar Threads

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