1

在第 67 行我有代码

void session::read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
      std::cout<<bytes_transferred<<std::endl;
}

每当服务器在会话类中接收到数据包时,都应该对其进行轮询,但由于某种原因,该函数永远不会被调用。被io_service.run();称为int main()for read_handler()。该代码基本上是 Boost 站点上的异步服务器示例,其中包含我添加的功能。这是代码。

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

class session
{
public:
  session(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    socket_.async_read_some(boost::asio::buffer(data_, max_length),
        boost::bind(&session::handle_read, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred);


private:
  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
    if (!error)
    {
      boost::asio::async_write(socket_,
          boost::asio::buffer(data_, bytes_transferred),
          boost::bind(&session::handle_write, this,
            boost::asio::placeholders::error));
    }
    else
    {
      delete this;
    }
  }

  void handle_write(const boost::system::error_code& error)
  {
    if (!error)
    {
      socket_.async_read_some(boost::asio::buffer(data_, max_length),
          boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
    else
    {
      delete this;
    }
  }

  tcp::socket socket_;
  enum { max_length = 1024 };
  char data_[max_length];
};
void session::read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
      std::cout<<bytes_transferred<<std::endl;
}

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    start_accept();
  }


private:
  void start_accept()
  {
    session* new_session = new session(io_service_);
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

  void handle_accept(session* new_session,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_session->start();
    }
    else
    {
      delete new_session;
    }

    start_accept();
  }

  boost::asio::io_service& io_service_;
  tcp::acceptor acceptor_;
};

int main()
{
  try
  {
    boost::asio::io_service io_service;
    server s(io_service, 4000);

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}
4

2 回答 2

2

session::read_handler(...) 未设置为处理程序。

调用 handle_accept() 是因为它被设置为处理程序:

acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));

调用 handle_read(...) 是因为它被设置为处理程序:

socket_.async_read_some(boost::asio::buffer(data_, max_length),
          boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

与 handle_write(...) 相同 - 由 handle_read 函数设置。

但是 read_handler 没有设置。请改用handle_read。或者从handle_read 调用read_handler。

  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
    if (!error)
    {
      std::cout<<bytes_transferred<<std::endl; // try this.
      read_handler(error, bytes_transferred);  // OR this.

      boost::asio::async_write(socket_,
          boost::asio::buffer(data_, bytes_transferred),
          boost::bind(&session::handle_write, this,
            boost::asio::placeholders::error));
    }
    else
    {
      delete this;
    }
  }
于 2013-04-12T09:23:17.857 回答
1

该函数永远不会被调用,因为您既没有调用它,也没有将它作为处理程序传递给异步函数之一。

您可以将处理程序视为状态机的状态,并将 async_* 调用视为这些状态之间的转换。您的状态如下(起点是start_accept,调用自server::server

<<start>>                   --async_accept-----> <server::handle_accept> //via server::server > server::start_accept
<server::handle_accept>     --async_read_some--> <session::handle_read>  //via session::start
<session::handle_read>      --async_write------> <session::handle_write>
<session::handle_write>     --async_read_some--> <session::handle_read>

路径仅适用于没有错误的情况。错误情况是微不足道的,它们只是结束程序,或者在handle_accept重试转换的情况下。

handle_read您可以看到和之间可能无限循环handle_write,但这read_handler不是游戏的一部分。

于 2013-04-12T09:19:39.397 回答