1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
for (std::size_t i = 0; i < io_size; ++i)
{
for(std::size_t t = 0; t < thread_size; ++t)
{
thread_ptr thread(new boost::thread(
boost::bind(&boost::asio::io_service::run, io_services_[i])));
}
}
void session::read()
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(), message::header_length),
boost::bind(&session::handle_read_header, shared_from_this(),
boost::asio::placeholders::error));
}
void session::handle_read_header(const boost::system::error_code& error)
{
if (!error && read_msg_.decode_header())
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
boost::bind(&session::handle_read_body, shared_from_this(),
boost::asio::placeholders::error));
}
else
{
do_close(error);
}
}
void session::do_write(message msg)
{
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&session::handle_write, shared_from_this(),
boost::asio::placeholders::error,
write_msgs_.front().length()));
}
}
void session::handle_write(const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
write_msgs_.pop_front();
if (!write_msgs_.empty())
{
boost::asio::async_write(socket_,
boost::asio::buffer(write_msgs_.front().data(),
write_msgs_.front().length()),
boost::bind(&session::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
else
{
do_close(error);
}
}
|