Increase multi-threaded asio tcp server with synchronous I / O on a given stream

Basically, I'm trying to implement a universal multi-threaded TCP server that can handle arbitrary usage requests on two different servers with slightly different needs.

My requirements:

  • The request cannot begin to be processed until the entire initial request has been received. (Essentially, I have a fixed size request header, which, among other things, includes the size of the entire request).
  • Processing the request may result in several response messages to the requesting client. IE, as a rule, requests can be processed in one answer, but sometimes, in response to lengthy database transactions, I need to respond to the client, letting them know that I am still working and not wasting time on the connection.

To achieve this, I followed the example of HTTP server # 2 from boost v1.44 pretty closely. In general, the example worked for simple cases. I noticed that when I scale to address multiple requests at the same time, the changes I made somehow led to the fact that all requests were processed in turn by one thread. Obviously I'm doing something wrong.

I can’t publish all the actual code that I use due to the limitations of the employer, but suffice it to say that I saved asynchronous calls to accept new connections, but replaced asynchronous read / write with synchronous calls. If there are specific fragments that you think you need to see, I can see what I can do.

Essentially, I'm looking for how to specify how to use boost :: asio for a multi-threaded TCP server, where individual connections are handled by a single thread with synchronous I / O. Again, keep in mind that my abstraction is based on the example of http # 2 server (one io_service per processor), but I'm flexible for an alternative

+3
source share
3 answers

Boost.Asio io_service io_service::run .

, read write deadline_timer . , reads writes.

+2

: io_service_pool_.get_io_service() , ?

// from server.cpp
void server::handle_accept(const boost::system::error_code& e)
{
  if (!e)
  {
    new_connection_->start();
    new_connection_.reset(new connection(
          io_service_pool_.get_io_service(), request_handler_));
    acceptor_.async_accept(new_connection_->socket(),
        boost::bind(&server::handle_accept, this,
          boost::asio::placeholders::error));
  }
}

, new_connection_.reset(); get_io_service() .

, io_service.

0

-, concurrency , . io_service - (.. , ), , , -.

- , concurrency. , I/O, , , / concurrency. io_service, io_service:: dispatch() , .

- .

0
source

Source: https://habr.com/ru/post/1777590/


All Articles