64,648
社区成员
发帖
与我相关
我的任务
分享
class connection{
....
};
class iodriver{
public:
void start_accept();
void handle_accept(connection pConnect, const boost::system::error_code& error);
private:
boost::asio::io_service m_IOServer; //IO句柄
boost::asio::ip::tcp::acceptor m_acceptor; //套接字接收器
}
typedef boost::shared_ptr<connection> connection_ptr;
void iodriver:start_accept()
{
connection_ptr pConnect = connection_ptr(new connection(m_IOServer));
m_acceptor.async_accept(pConnect ->GetSock(), boost::bind(&iodirver::handle_accept, this, pConnect , boost::asio::placeholders::error));
}
void odriver::handle_accept(connection_ptr pConnect, const boost::system::error_code& error)
{
if (!error)
{
boost::system::error_code err;
unsigned int uPort = pConnect->GetSock().remote_endpoint(err).port();
std::string strIp = pConnect->GetSock().remote_endpoint(err).address().to_string();
printf("+accept socket form [%s-%u][0x%x]", strIp.c_str(), uPort, pConnect.get());
m_xNewConnect.AddConnect(pConnect);
start_recv_head(pConnect);
}
else
{
printf("accept error [%d]", error.value());
}
start_accept();
}
void iodriver::start_recv_head(connection_ptr pConnect)
{
xWMessage_ptr pMsg = xWMessage_ptr(new xWMessage);
boost::asio::async_read(pConnect->GetSock(), boost::asio::buffer(pMsg->head_data(), xWMessage::Msg_Head_Len), boost::bind(&xIODirve::handle_recv_head, this, pConnect, pMsg, _1));
}
void iodriver::handle_recv_head(connection_ptr pConnect, xWMessage_ptr pMsg, const boost::system::error_code& error)
{
if (!error)
{
start_recv_head(pConnect);
}
else
{
printf("recv one msg head error[%u] \n", error.value());
//偶尔存在此处会打印995错误
....
}
}