I have some simple questions, I have a client-server application and data sent on the wire.
I'd like to be able to recover the data and handle it properly.
struct T1
{
int id;
int foo;
};
struct T2
{
int id;
char foo;
int bar;
};
Let's take these structures, they are sent on the network preceded by an int which will tell if either T1 or T2 follows. If the client sends me T1, then sends me T2, do I have the assurance that I can read a full structure with asio::ip::tcp::socket.async_read() ? I'd like to set up a handler which will handle a single struct, however what will happen if I'm unable to read everything in a single async_read() ?
The asynchronous operation will continue until one of the following conditions is true:
- The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes.
- An error occurred.
Will it discard the data that cannot be read ? will it trigger another async_read ? And, am I assured that an async_read will only get one ID+structure if my client sends me sequentially the ID+structure ? Or may the OS optimize things and put them both in the same paquet ? As you may have seen, I'm a little confused, I'd like to make the right decisions when designing a server/client application, any help will be appreciated.
Thank you.