Results 1 to 7 of 7

Thread: QUdpSocket Buffering - Approach?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    May 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3

    Wink Re: QUdpSocket Buffering - Solution (Long Post!)

    Quote Originally Posted by alexisdm View Post
    Although MyUDPSocket represents a thread, its slots are not automatically executed in that thread.
    Ah! I read a lot about that and got confused very quickly. I went back to my code and added some more debug information to better appreciate what is going on. Essentially I just print out the thread address using QThread::currenThread(). It worked before because I had the normal event loop setup in my main thread, like this:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. QTimer::singleShot(30000, &a, SLOT(quit()));
    5. MyUDPSocket myUDPSocket;
    6. myUDPSocket.start();
    7. return a.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    So before adding moveToThread(), I'd get output like:

    Qt Code:
    1. Thread: QThread(0x99f1400) In main...
    2. Thread: QThread(0x99f1400) Setting up MyUDPSocket...
    3. Thread: MyUDPSocket(0xbf96d1d8) Setting up MySocketBuffer...
    4. Thread: MyUDPSocket(0xbf96d1d8) Starting MyUDPSocket event loop...
    5. Thread: MySocketBuffer(0x9a0def0) MySocketBuffer running...
    6. // Events for QUdpSocket are processed in main thread!
    7. Thread: QThread(0x99f1400) Received a packet! Adding it to queue...
    8. Thread: MySocketBuffer(0x9a0def0) Packets Left: 0
    9. Thread: QThread(0x99f1400) Queued packet send...
    To copy to clipboard, switch view to plain text mode 

    After moveToThread:

    Qt Code:
    1. Thread: QThread(0x95ca400) In main...
    2. Thread: QThread(0x95ca400) Setting up MyUDPSocket...
    3. Thread: MyUDPSocket(0xbfb580e8) Setting up MySocketBuffer...
    4. Thread: MySocketBuffer(0x95d2b50) MySocketBuffer running...
    5. Thread: MyUDPSocket(0xbfb580e8) Starting MyUDPSocket event loop...
    6. // The MyUDPSocket thread is now handling all the events! :)
    7. Thread: MyUDPSocket(0xbfb580e8) Received a packet! Adding it to queue...
    8. Thread: MySocketBuffer(0x95d2b50) Packets Left: 0
    To copy to clipboard, switch view to plain text mode 

    The moveToThread call is not required for MySocketBuffer as it is already created in the context of MyUdpSocket's thread, right? Just want to be sure I understand this...

    Quote Originally Posted by alexisdm View Post
    And since QByteArray is basically already a ref-counted pointer to an internal buffer, you should use constant references (const QByteArray &) rather than pointer to pass it through signals/slots.
    Yep, tired eyes and compiler errors threw me on that. Had to read a bit about const declaration and const correctness to understand that better - so my knowledge was increased there too!

    I've updated the code and attached to this post so others can learn. I'm looking now at expanding on this code now so that I can plug in different queue processors - for example one queue will slow sending down as per the code in this post, while another will manipulate the data somehow. Looks like the Strategy design pattern will come in handy for this...But my previous question still stands: Is there a more efficient way to queue packets? Or is this a reasonable approach?

    Cheers,

    Xav.
    Attached Files Attached Files

Similar Threads

  1. Encryption, approach request
    By unix7777 in forum Newbie
    Replies: 5
    Last Post: 22nd February 2010, 16:19
  2. double buffering
    By HelloDan in forum Newbie
    Replies: 10
    Last Post: 31st March 2009, 04:14
  3. thread - right approach?
    By heinz32 in forum Qt Programming
    Replies: 3
    Last Post: 17th June 2008, 18:39
  4. Double Buffering for plot graphs
    By Tavit in forum Qt Programming
    Replies: 0
    Last Post: 20th March 2008, 14:10
  5. what is the best approach
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 10:02

Tags for this Thread

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.