7
votes

I am using boost asio to develop a tcp server process which listens for client connections. The clients are expected to send partial packets. Server process can't process the packet until it receives the entire packet from the client (header,data and footer).

In order to handle partial packets, server process needs to push the packets( or partial packets) from each client connection to a queue. Server would maintain a queue per client connection.

Now my question is how to get a socket file descriptor when using boost asio. I am planning to use the integer value from socket file descriptor and create a std::map container that handles queue per client connection.

std::map< int, std::deque< std::string >

If retrieving socket file descriptor is not feasible what could be the alternatives for optimal performance. Do I need to use

std::map< tcp::socket *,std::deque< std::string> > ?

Thanks in advance.

2

2 Answers

1
votes

When you call the various receive functions, pass in a pointer (or shared pointer) to the structure associated with that connection. There's no need for a map from the socket descriptor because you never need to touch the socket descriptor.

Like this:

boost::asio::async_read(socket, boost::asio::buffer(buf),
    boost::bind(&MyConnection::doRead, this, boost::asio::placeholders::error));


void MyConnection::doRead(const boost::system::error_code& error)
{
 // Here, we are in the object associated with this connection
22
votes

There's the basic_socket::native_handle() member function inherited by the socket classes, but you should really consider @David's answer.