trinitycore魔兽服务器源码分析(⼆)⽹络
书接上⽂ 继续分析Socket.h SocketMgr.h
template
class Socket : public std::enable_shared_from_this
根据智能指针的使⽤规则 类中有使⽤本类⾃⼰的指针 必须继承⾃enable_shared_from_this<> 防⽌⾃引⽤ 不能释放的BUG
class Socket封装了asio中的socket类 获取远端ip 端⼝等功能, 并且额外提供异步读写的功能
类中的两个原⼦变量 _closed _closing标记该socket的关闭开启状态
bool Update()函数根据socket是否是同步异步标记进⾏写⼊队列的处理。 同步则进⾏处理 异步则暂缓
void AsyncRead() void AsyncReadWithCallback(void (T::*callback)(boost::system::error_code, std::size_t))
则采取异步读取socket 调⽤默认函数ReadHandlerInternal() 或者输⼊函数T::*callback()
由于AsyncReadWithCallback 函数中bind 需要 T类的指针 所以才有开头的继承std::enable_shared_from_this
但是使⽤⽐较怪异 std::enable_shared_from_this<>⽤法⼀般是继承⾃⼰本⾝
class self :: public std::enable_shared_from_this{
public:
void test(){
// only for test
std::bind(&self ::test, shared_from_this());
}
}
异步写write类似 ,由bool AsyncProcessQueue()函数发起
使⽤asio的async_write_some函数异步读取连接内容 并调⽤回调函数WriteHandler()或者WriteHandlerWrapper()