Boost.Asio - how to queue incoming messages?

Mar 23, 2012 at 12:43pm
Hi, title is my question.

I have a server that uses asynchronous read/write operations to interacting with any connecting clients. It works good so far, however there are still problems when the server is still processing some incoming data packets while new packets are coming in (using tcp with ssl).

So now I would like to do some kind of message/packet queue where I add all the packets that couldn't be processed because the server is busy with other things. And one he is finished doing his work, he goes through the list of queued packets and works them off, one after another (until the list is empty, then he is only waiting for new packets).

However I really dont know exactly how to realize this, I already searched for examples and stuff like that but I could only find these related links:
http://www.boost.org/doc/libs/1_41_0/doc/html/boost/interprocess/message_queue.html
http://prakharprakhar.blogspot.com/2009/12/boost-message-queue-interprocess.html (this is an example but not asynchronous)

I hope you can point me to the right direction of how it could be done (just the basic principle) or some related examples, thanks.


Last edited on Mar 23, 2012 at 12:44pm
Mar 23, 2012 at 2:15pm
In the completion even handler for an async read, instead of processing it and doing an async_write() with the result immediately, add it to a thread safe queue, perhaps?


Mar 23, 2012 at 5:23pm
What do you suggest to add to a thread safe queue? The data that I have recieved from the client?

If yes then how shall I proceed after adding it? Immediatly calling async_read again with the handler that adds the data to a queue as a parameter? (so he wont miss any more packets)

Also at what point could I do the data processing in this whole process?
And I suppose I also send the data back after it is finished processing but is that actually possible when I'm already waiting with async_read() for more to come?

Btw I dont always immediately send the data back or send only a single one. Sometimes I wait for more incoming packets and sometimes I send multiple packets (without waiting for a response between the sending).
Mar 24, 2012 at 1:50pm
> What do you suggest to add to a thread safe queue?
> The data that I have recieved from the client?

Yes.
Either that or the processing logic encapsulated in a command object.
http://en.wikipedia.org/wiki/Command_pattern


> Also at what point could I do the data processing in this whole process?
>And I suppose I also send the data back after it is finished processing
> but is that actually possible when I'm already waiting with async_read() for more to come?

In the completion handler for the async_read()
a. Add the pending task to the queue.
b. Check if more data has arrived with socket_base::bytes_readable()
- If there is more data to be read, or the queue is empty, issue another read request
- If not, remove and process one pending request from the queue, issue an async write with the result; go back to b
Mar 25, 2012 at 3:30pm
ok...I will try to play around and see if I get it to work, if not I'll return back here.

Thanks for all your help so far ;)
Topic archived. No new replies allowed.