server/DemoServer/tcp_server.cpp

(开头部分) 1KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。


#include "stdafx.h"

#include "tcp_server.h"
//tcp constructor
tcp_server::tcp_server(boost::asio::io_service& io_service, const boost::asio::ip::tcp::endpoint& endpoint)
: acceptor_(io_service, endpoint)
, connection_manager_()
, new_connection_()
{
	start_accept();
}

tcp_server::~tcp_server()
{
	connection_manager_.stop_all();
}
void tcp_server::start_accept()
{
	//重新设置指向对话实例的指针
	new_connection_.reset(new Tcp_Session(acceptor_.get_io_service()));

	acceptor_.async_accept(new_connection_->socket()
		, boost::bind(&tcp_server::handle_accept
		, this
		, boost::asio::placeholders::error));
}
void tcp_server::handle_accept(const boost::system::error_code& error)
{
	// Check whether the server was stopped by a signal before this completion
	// handler had a chance to run.
	if (!acceptor_.is_open())
	{
		return;
	}
	if (!error)
	{
		//register  callback
		new_connection_->setConnectionCallback(connectionCallback_);
		new_connection_->setMessageCallback(messageCallback_);
		new_connection_->setWriteCompleteCallback(writeCompleteCallback_);
		new_connection_->setDisconnectedCallback(
			boost::bind(&tcp_server::removeConnection, this, _1));
		connection_manager_.start(new_connection_);
	}
	//accept new client connection
	start_accept();
}

void tcp_server::handle_stop()
{
	// The server is stopped by cancelling all outstanding asynchronous
	// operations. Once all operations have finished the io_service::run() call
	// will exit.
	acceptor_.close();
	connection_manager_.stop_all();
}

void tcp_server::removeConnection(const SessionPtr& session)
{
后面还有 3 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。