吉林北京网站建设,移动端网站开发项目报告,物联网工程主要学什么,海南最新政策文章目录 一、LoopThreadPool模块二、实现思想#xff08;一#xff09;管理#xff08;二#xff09;流程#xff08;三#xff09;功能设计 三、代码 一、LoopThreadPool模块 TcpServer模块#xff1a; 对所有模块的整合#xff0c;通过 tcpserver 模块实例化的对象一管理二流程三功能设计 三、代码 一、LoopThreadPool模块 TcpServer模块 对所有模块的整合通过 tcpserver 模块实例化的对象可以非常简单的完成一个服务器的搭建。 对前面所有子模块的整合模块提供给用户用于搭建一个高性能服务器的模块
二、实现思想
一管理
Acceptor对象创建一个监听套接字EventLoop 对象baseloop对象实现对监听套接字的事件监控std::vector conns,实现对新建连接的管理EventLoopPool 对象创建loop线程池对新建连接进行事件监控和处理
二流程
流程 1. 在TcpServer中实例一个Acceptor对象以及一个EventLoop 对象baseloop) 2. 将Acceptor 挂在baseloop 进行事件监控 3. 一旦Acceptor 对象就绪了可读事件则执行时间回调函数获取新建连接 4. 对新连接创造一个 Connection 进行管理 5. 对新连接对应的 Connection 设置功能回调 连接完成回调消息回调关闭回调任意事件监控 6. 启动Connettion 的非活跃链接的超时销毁功能 7. 将新连接对应的Connection 挂到 LoopThreadPool 中的丛书线程对应的Eventloop 中进行事件监控 8. 一旦Connection对应的链接就绪了可读事件则这个时候执行读事件回调函数读取数据读取完毕后调用TcpServer设置的消息回调
三功能设计
设置从属线程池数量 2. 启动服务器 3. 设置各种回调函数连接建立完成消息关闭任意 用户设置给TcpServer TcpServer设置获取的新连接 4. 是否启动非活跃连接超时销毁功能 5. 添加任务
三、代码
class TcpServer {private:uint64_t _next_id; //这是一个自动增长的连接IDint _port;int _timeout; //这是非活跃连接的统计时间---多长时间无通信就是非活跃连接bool _enable_inactive_release;//是否启动了非活跃连接超时销毁的判断标志EventLoop _baseloop; //这是主线程的EventLoop对象负责监听事件的处理Acceptor _acceptor; //这是监听套接字的管理对象LoopThreadPool _pool; //这是从属EventLoop线程池std::unordered_mapuint64_t, PtrConnection _conns;//保存管理所有连接对应的shared_ptr对象using ConnectedCallback std::functionvoid(const PtrConnection);using MessageCallback std::functionvoid(const PtrConnection, Buffer *);using ClosedCallback std::functionvoid(const PtrConnection);using AnyEventCallback std::functionvoid(const PtrConnection);using Functor std::functionvoid();ConnectedCallback _connected_callback;MessageCallback _message_callback;ClosedCallback _closed_callback;AnyEventCallback _event_callback;private:void RunAfterInLoop(const Functor task, int delay) {_next_id;_baseloop.TimerAdd(_next_id, delay, task);}//为新连接构造一个Connection进行管理void NewConnection(int fd) {_next_id;PtrConnection conn(new Connection(_pool.NextLoop(), _next_id, fd));conn-SetMessageCallback(_message_callback);conn-SetClosedCallback(_closed_callback);conn-SetConnectedCallback(_connected_callback);conn-SetAnyEventCallback(_event_callback);conn-SetSrvClosedCallback(std::bind(TcpServer::RemoveConnection, this, std::placeholders::_1));if (_enable_inactive_release) conn-EnableInactiveRelease(_timeout);//启动非活跃超时销毁conn-Established();//就绪初始化_conns.insert(std::make_pair(_next_id, conn));}void RemoveConnectionInLoop(const PtrConnection conn) {int id conn-Id();auto it _conns.find(id);if (it ! _conns.end()) {_conns.erase(it);}}//从管理Connection的_conns中移除连接信息void RemoveConnection(const PtrConnection conn) {_baseloop.RunInLoop(std::bind(TcpServer::RemoveConnectionInLoop, this, conn));}public:TcpServer(int port):_port(port), _next_id(0), _enable_inactive_release(false), _acceptor(_baseloop, port),_pool(_baseloop) {_acceptor.SetAcceptCallback(std::bind(TcpServer::NewConnection, this, std::placeholders::_1));_acceptor.Listen();//将监听套接字挂到baseloop上}void SetThreadCount(int count) { return _pool.SetThreadCount(count); }void SetConnectedCallback(const ConnectedCallbackcb) { _connected_callback cb; }void SetMessageCallback(const MessageCallbackcb) { _message_callback cb; }void SetClosedCallback(const ClosedCallbackcb) {_closed_callback cb; }void SetAnyEventCallback(const AnyEventCallbackcb) { _event_callback cb; }void EnableInactiveRelease(int timeout) { _timeout timeout; _enable_inactive_release true; }//用于添加一个定时任务void RunAfter(const Functor task, int delay) {_baseloop.RunInLoop(std::bind(TcpServer::RunAfterInLoop, this, task, delay));}void Start() { _pool.Create(); _baseloop.Start(); }
};