server/DemoServer/Tcp_Session.cpp

(开头部分) 2KB

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


#include "stdafx.h"

#include "Tcp_Session.h"
//private constructor
Tcp_Session::Tcp_Session(boost::asio::io_service& io_service)
: m_socket(io_service)
, m_isConnected(false)
, m_delimiter("\r\n")
{

}
//free resource
Tcp_Session::~Tcp_Session()
{
	stop();
}
//connection start function
void Tcp_Session::start()
{
// 	//first connection return flags
// 	m_socket.async_write_some(boost::asio::buffer("link successed!")
// 		, boost::bind(&Tcp_Session::handle_write, shared_from_this()
// 		, boost::asio::placeholders::error
// 		, boost::asio::placeholders::bytes_transferred));

	m_isConnected = true;
	//get client IP address
	std::cout << "get new client  " << m_socket.remote_endpoint().address() << std::endl;
	read();
}
void Tcp_Session::stop()
{
	if (m_socket.is_open())
	{
		m_socket.close();
	}
	m_isConnected = false;
}
void Tcp_Session::read()
{
	boost::asio::async_read_until(m_socket
		, m_buffer
		, m_delimiter
		, boost::bind(&Tcp_Session::handle_read
		, this
		, boost::asio::placeholders::error
		, boost::asio::placeholders::bytes_transferred));
}
void Tcp_Session::handle_read(const boost::system::error_code& e, uint32_t bytes_transferred)
{
	if (!e)
	{
		// TODO: you could do some message processing here, like breaking it up
		// into smaller parts, rejecting unknown messages or handling the message protocol
		std::string message(boost::asio::buffer_cast<const char*>(m_buffer.data()), bytes_transferred);

		//std::cout <<"Tcp_Session::handle_read : "<< message << std::endl;
		m_buffer.consume(bytes_transferred);
		// create function to notify listeners
后面还有 49 行代码,购买后查看完整代码

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

AI 解读

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

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