Trouble with understanding Boost.Asio

Mar 19, 2012 at 12:08pm
Hello I am currently tring to realize a server program with Boost.Asio.

Since I am rather unexperienced on that topic I simply took the example code as a basis to test if I can make it work (the SSL example code to be precise (since I am using SSL) -> http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/example/ssl/server.cpp )
I only slightly modified the code so everything is pretty much the same as in the example of the link (excpet that I modified context_ a little).

So first of all it would be nice if someone could explain to me what happens when a client connects :D
I mean I know that the incoming connection first gets accepted with start_accept() and a new object of session is created where the packets are recieved and sent back but how exactly does the "handler" stuff work?

For example, what are the functions handle_handshake(...), handle_read(...) and handle_write(...) doing and where are they called? What confuses me most is that in handle_read(...), async_write(..) is called and in handle_write(...), socket_.async_read_some(..) which seems kind of contradictory to me.

I already managed to get the code working and a packet is successfully recieved from the client but for some reason I get the data somewhere between handle_handshake and handle_read (and not after read or after handle_handshake what I would think), is that correct so?
If yes, then what's the purpose of handle_read/handle_handshake?
Its hard for me to fully understand the example code when there are no comments whatsoever in it.


Also I would like to know if it is possible to send two (or more) consecutive tcp packets back to the client, is this possible (with the ssl example)? If yes please give me an example what it would look like, thanks in advance.
Mar 19, 2012 at 12:19pm
You may find Boris Schäling's e-book (Chapter 7) useful:
http://en.highscore.de/cpp/boost/index.html
Mar 19, 2012 at 2:07pm
Thanks a lot for the link, I think I understand the whole concept of Asio now a lot better.

Still I am unsure what the handler handle_write in the SSL example I posted is doing since handle_read already does the packet sending.

Also I am still not sure how I am supposed to send two consecutive packets, I already found out that async_write actually supports sending multiple buffers in one call by using a ConstBufferSequence with boost::buffer.
So I could create the data for each packet and send it away in one call but is the way it is supposed to be?

And will the client actually recieve two packets or a single one which contains the content of both packets/buffers?
Or is there another way where I dont use a single function to send two packets in a row?
Also I suppose if I use the first method I have to specify the size of the whole buffer sequence when I call the async_write function?
Last edited on Mar 19, 2012 at 2:42pm
Mar 19, 2012 at 2:50pm
> I am unsure what the handler handle_write in the SSL example I posted is doing

handle_read() and handle_write() are completion even handlers. A typical asynchronous write proceeds is as follows:

1. You call async_write() with a socket.
2. The socket forwards the write request to the io_service object.
3. The io_service object uses the underlying platform facilities to initiate the asynchronous write.
4. You call io_service::run() to a. wait for the asynchronous write to complete and b. the result of the asynchronous write to be retrieved.
5. The io_service object waits for the asynchronous write to complete (if it is still incomplete) and calls the completion event handler - in this case handle_write() - when the write completes.

In the example, handle_write() issues the next async_read request.


> how I am supposed to send two consecutive packets

One way (using only async operations) would be:
a. Send the first packet via a call to async_write()
b. Call io_service::run() on the associated io_service object.
c. When the handler handle_write() is called, issue the async_write() for the second packet.

Perhaps, simpler would be:
a. Send the first packet synchronously with a call to write()
b. When it returns, send the second packet.
Last edited on Mar 19, 2012 at 2:55pm
Mar 19, 2012 at 3:10pm
A typical asynchronous write proceeds is as follows:
I see. So the two handlers handle_read() and handle_write() are actually calling each other once they complete that means it keeps the "packet checking" alive or am I missing something here?

I guess it wouldnt be good if I mix async operations with sync operations (where I use synchronously calls only when I need to send or recieve multiple packets at once for example) or?
Last edited on Mar 19, 2012 at 3:10pm
Mar 19, 2012 at 3:20pm
> the two handlers handle_read() and handle_write() are actually calling each other once they complete

The two handlers are issuing async requests - handle_read() issues the write request and handle_write() issues a read request. The other handler is called by the service when the async operation completes.


> I guess it wouldnt be good if I mix async operations with sync operations

Yes.
Topic archived. No new replies allowed.