From e7ef89daae89fa6058e7297152381ed549f37653 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Fri, 2 Jul 2021 14:25:37 +0800 Subject: [PATCH] libevent_tcpserver --- server/tcp_server_libevent.cpp | 30 ++++++++++++++++++------------ server/tcp_server_libevent.h | 8 ++++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/server/tcp_server_libevent.cpp b/server/tcp_server_libevent.cpp index 60badbf..0b77350 100644 --- a/server/tcp_server_libevent.cpp +++ b/server/tcp_server_libevent.cpp @@ -58,16 +58,19 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc { struct sockaddr_in* client = (sockaddr_in*)addr ; cout << "connect new client: " << inet_ntoa(client->sin_addr) << "::"<< ntohs(client->sin_port)<< endl; - struct event_base *base = (struct event_base*)ptr; - //添加新事件 - struct bufferevent *bev; - bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); - //给bufferevent缓冲区设置回调 - bufferevent_setcb(bev, read_cb, write_cb, event_cb, inet_ntoa(client->sin_addr)); - //启动 bufferevent的 读缓冲区。默认是disable 的 - bufferevent_enable(bev, EV_READ); + TcpServerLibevent *server = (TcpServerLibevent*)ptr; + // 添加新事件 + if(server != nullptr){ + struct bufferevent *bev; + bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE); + // 给bufferevent缓冲区设置回调 + bufferevent_setcb(bev, read_cb, write_cb, event_cb, inet_ntoa(client->sin_addr)); + // 启动 bufferevent的 读缓冲区。默认是disable 的 + bufferevent_enable(bev, EV_READ); + } } + int test_tcp_server() { #ifdef WIN32 @@ -142,7 +145,7 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) { m_event_listener = evconnlistener_new_bind(m_event_base, cb_listener, - m_event_base, + this, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, m_backlog, (struct sockaddr*)&m_server_addr, @@ -154,19 +157,22 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) { m_status = STOP; } /** - * @description: 同步启动服务端 + * @description: 同步启动服务端,运行在本线程中 * @param {*} * @return {*} */ int TcpServerLibevent::StartServerSync(){ if(m_status == STOP){ - + m_status = RUNNING; + event_base_dispatch(m_event_base); + evconnlistener_free(m_event_listener); + event_base_free(m_event_base); return 0; } return -1; } /** - * @description: 异步启动服务端 + * @description: 异步启动服务端,运行在其他线程中 * @param {*} * @return {*} */ diff --git a/server/tcp_server_libevent.h b/server/tcp_server_libevent.h index e31fd19..931776a 100644 --- a/server/tcp_server_libevent.h +++ b/server/tcp_server_libevent.h @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-06-30 16:23:10 - * @LastEditTime: 2021-07-02 13:45:27 + * @LastEditTime: 2021-07-02 14:05:05 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \server\tcp_server_libevent.h @@ -55,7 +55,11 @@ public: ~TcpServerLibevent(); int StartServerSync(); // 同步启动服务器 int StartServerAsync(); // 异步启动服务 - + friend void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr); + friend void read_cb(struct bufferevent *bev, void *arg); + friend void event_cb(struct bufferevent *bev, short events, void *arg); + friend void write_cb(struct bufferevent *bev, void *arg); + private: uint32_t m_port; // 监听端口号 string m_bind_ip; // 绑定端口号