libevent_tcpserver

master
zcy 2021-07-02 14:25:37 +08:00
parent 3f77ca29fb
commit e7ef89daae
2 changed files with 24 additions and 14 deletions

View File

@ -58,16 +58,19 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
{ {
struct sockaddr_in* client = (sockaddr_in*)addr ; struct sockaddr_in* client = (sockaddr_in*)addr ;
cout << "connect new client: " << inet_ntoa(client->sin_addr) << "::"<< ntohs(client->sin_port)<< endl; cout << "connect new client: " << inet_ntoa(client->sin_addr) << "::"<< ntohs(client->sin_port)<< endl;
struct event_base *base = (struct event_base*)ptr; TcpServerLibevent *server = (TcpServerLibevent*)ptr;
//添加新事件 // 添加新事件
struct bufferevent *bev; if(server != nullptr){
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); struct bufferevent *bev;
//给bufferevent缓冲区设置回调 bev = bufferevent_socket_new(server->m_event_base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, read_cb, write_cb, event_cb, inet_ntoa(client->sin_addr)); // 给bufferevent缓冲区设置回调
//启动 bufferevent的 读缓冲区。默认是disable 的 bufferevent_setcb(bev, read_cb, write_cb, event_cb, inet_ntoa(client->sin_addr));
bufferevent_enable(bev, EV_READ); // 启动 bufferevent的 读缓冲区。默认是disable 的
bufferevent_enable(bev, EV_READ);
}
} }
int test_tcp_server() int test_tcp_server()
{ {
#ifdef WIN32 #ifdef WIN32
@ -142,7 +145,7 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) {
m_event_listener = evconnlistener_new_bind(m_event_base, m_event_listener = evconnlistener_new_bind(m_event_base,
cb_listener, cb_listener,
m_event_base, this,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
m_backlog, m_backlog,
(struct sockaddr*)&m_server_addr, (struct sockaddr*)&m_server_addr,
@ -154,19 +157,22 @@ TcpServerLibevent::TcpServerLibevent(int port,string bindip) {
m_status = STOP; m_status = STOP;
} }
/** /**
* @description: * @description: ,线
* @param {*} * @param {*}
* @return {*} * @return {*}
*/ */
int TcpServerLibevent::StartServerSync(){ int TcpServerLibevent::StartServerSync(){
if(m_status == STOP){ 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 0;
} }
return -1; return -1;
} }
/** /**
* @description: * @description: ,线
* @param {*} * @param {*}
* @return {*} * @return {*}
*/ */

View File

@ -1,7 +1,7 @@
/* /*
* @Author: your name * @Author: your name
* @Date: 2021-06-30 16:23:10 * @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 * @LastEditors: Please set LastEditors
* @Description: In User Settings Edit * @Description: In User Settings Edit
* @FilePath: \server\tcp_server_libevent.h * @FilePath: \server\tcp_server_libevent.h
@ -55,7 +55,11 @@ public:
~TcpServerLibevent(); ~TcpServerLibevent();
int StartServerSync(); // 同步启动服务器 int StartServerSync(); // 同步启动服务器
int StartServerAsync(); // 异步启动服务 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: private:
uint32_t m_port; // 监听端口号 uint32_t m_port; // 监听端口号
string m_bind_ip; // 绑定端口号 string m_bind_ip; // 绑定端口号