no message

master
zcy 2021-07-02 17:25:47 +08:00
parent e7ef89daae
commit d908502855
3 changed files with 47 additions and 11 deletions

View File

@ -1,8 +1,8 @@
/*
* @Author: your name
* @Date: 2021-06-30 10:02:04
* @LastEditTime: 2021-07-02 11:46:43
* @LastEditors: your name
* @LastEditTime: 2021-07-02 17:11:17
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\main.cpp
*/
@ -10,7 +10,10 @@
#include "tcp_server_libevent.h"
#include "winsock2.h"
#include <stdio.h>
#include <thread>
#include <stdlib.h>
using namespace std;
int main(){
WORD wdVersion = MAKEWORD(2, 2);//定义自己需要的网络库版本这里是2.2
WSADATA wdSockMsg;//这是一个结构体
@ -37,5 +40,9 @@ int main(){
return 0;
}
std::cout<<"start server libevent\r\n";
// test_tcp_server();
TcpServerLibevent server(8000,"0.0.0.0");
server.StartServerSync();
while(true){
}
}

View File

@ -10,7 +10,7 @@ void read_cb(struct bufferevent *bev, void *arg)
{
char buf[1024] = {0};
char* ip = (char*)arg;
bufferevent_read(bev, buf, sizeof(buf));
cout << "client " << ip << " say:" << buf << endl;
//写数据给客户端
@ -47,12 +47,11 @@ void event_cb(struct bufferevent *bev, short events, void *arg)
bufferevent_free(bev);
cout << "bufferevent 资源已经被释放..." << endl;
}
/************************************
@ Brief:
@ Author: woniu201
@ Created: 2018/11/21
@ Return:
@ Return:
************************************/
void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *addr, int len, void *ptr)
{
@ -70,7 +69,6 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
}
}
int test_tcp_server()
{
#ifdef WIN32
@ -113,6 +111,18 @@ int test_tcp_server()
return -1;
}
}
void server_run(TcpServerLibevent *p){
if(nullptr != p){
if(p->m_status == TcpServerLibevent::STOP){
p->m_status = TcpServerLibevent::RUNNING;
event_base_dispatch(p->m_event_base);
evconnlistener_free(p->m_event_listener);
event_base_free(p->m_event_base);
}
}
}
/**
* @description:
* @param {*}
@ -127,7 +137,11 @@ TcpServerLibevent::SERVER_STATUS TcpServerLibevent::Status(){
* @param {string} bindip
* @return {*}
*/
TcpServerLibevent::TcpServerLibevent(int port,string bindip) {
TcpServerLibevent::TcpServerLibevent(int port,string bindip) :
m_thread(nullptr),
m_event_base(nullptr),
m_event_listener(nullptr)
{
m_backlog = 10000;
this->m_bind_ip = bindip;
this->m_port = port;
@ -178,8 +192,21 @@ int TcpServerLibevent::StartServerSync(){
*/
int TcpServerLibevent::StartServerAsync(){
if(m_status == STOP){
#ifdef WIN32
evthread_use_windows_threads();
#endif
m_thread = new thread(server_run,this);
m_thread->detach();
return 0;
}
return -1;
}
TcpServerLibevent::~TcpServerLibevent(){
if(this->m_status == RUNNING){
m_thread->detach();
event_base_loopbreak(m_event_base);
this->m_status = STOP;
}
}

View File

@ -1,7 +1,7 @@
/*
* @Author: your name
* @Date: 2021-06-30 16:23:10
* @LastEditTime: 2021-07-02 14:05:05
* @LastEditTime: 2021-07-02 16:05:20
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\tcp_server_libevent.h
@ -59,7 +59,8 @@ public:
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);
friend void server_run(TcpServerLibevent *p);
private:
uint32_t m_port; // 监听端口号
string m_bind_ip; // 绑定端口号
@ -69,6 +70,7 @@ private:
struct event_base * m_event_base;
struct evconnlistener* m_event_listener;
SERVER_STATUS m_status;
thread *m_thread;
};
int test_tcp_server();