no message

master
ATTIOT\zhengcy 2021-07-02 13:53:07 +08:00
parent e7d1f310b7
commit 3f77ca29fb
3 changed files with 130 additions and 11 deletions

View File

@ -1,17 +1,22 @@
/*
* @Author: your name
* @Date: 2021-06-30 10:02:04
* @LastEditTime: 2021-07-02 11:46:43
* @LastEditors: your name
* @Description: In User Settings Edit
* @FilePath: \server\main.cpp
*/
#include <iostream>
#include "tcp_server_libevent.h"
#include "winsock2.h"
#include <stdio.h>
int main(){
WORD wdVersion = MAKEWORD(2, 2);//定义自己需要的网络库版本这里是2.2
WSADATA wdSockMsg;//这是一个结构体
int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
if (0 != nRes)
{
switch (nRes)
{
if (0 != nRes) {
switch (nRes) {
case WSASYSNOTREADY:
printf("check your library");
break;
@ -26,13 +31,11 @@ int main(){
break;
}
}
if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion))
{
printf("");
if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion)) {
printf("WSACleanup");
WSACleanup();
return 0;
}
std::cout<<"start server libevent\r\n";
test_tcp_server();
// test_tcp_server();
}

View File

@ -92,7 +92,14 @@ int test_tcp_server()
// 绑定
// 接收连接请求
struct evconnlistener* listener;
listener = evconnlistener_new_bind(base, cb_listener, base, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, 36, (struct sockaddr*)&serv, sizeof(serv));
listener = evconnlistener_new_bind(base,
cb_listener,
base,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
30000,
(struct sockaddr*)&serv,
sizeof(serv));
if(NULL != listener){
// 启动循环监听
event_base_dispatch(base);
@ -103,3 +110,70 @@ int test_tcp_server()
return -1;
}
}
/**
* @description:
* @param {*}
* @return {*}
*/
TcpServerLibevent::SERVER_STATUS TcpServerLibevent::Status(){
return m_status;
}
/**
* @description:
* @param {int} ports
* @param {string} bindip
* @return {*}
*/
TcpServerLibevent::TcpServerLibevent(int port,string bindip) {
m_backlog = 10000;
this->m_bind_ip = bindip;
this->m_port = port;
memset(&m_server_addr, 0, sizeof(m_server_addr));
m_server_addr.sin_family = AF_INET;
m_server_addr.sin_port = htons(port);
m_server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// 创建 event_base
m_event_base = event_base_new();
if(NULL == m_event_base){
return;
}
m_event_listener = evconnlistener_new_bind(m_event_base,
cb_listener,
m_event_base,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
m_backlog,
(struct sockaddr*)&m_server_addr,
sizeof(m_server_addr));
if(NULL == m_event_listener)
{
m_status = FAIL;
}
m_status = STOP;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::StartServerSync(){
if(m_status == STOP){
return 0;
}
return -1;
}
/**
* @description:
* @param {*}
* @return {*}
*/
int TcpServerLibevent::StartServerAsync(){
if(m_status == STOP){
return 0;
}
return -1;
}

View File

@ -1,3 +1,11 @@
/*
* @Author: your name
* @Date: 2021-06-30 16:23:10
* @LastEditTime: 2021-07-02 13:45:27
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\tcp_server_libevent.h
*/
#ifndef GENERAL_TCPSERVER_H
#define GENERAL_TCPSERVER_H
@ -19,11 +27,45 @@ extern "C"{
#include "event2/event.h"
#include "event2/thread.h"
};
#include <stdint.h>
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
// tcp 连接
class Connection{
public:
Connection();
private:
int m_bytes_send;
int m_bytes_recv;
};
// 管理服务端
class TcpServerLibevent{
typedef enum{
RUNNING,
STOP,
FAIL
}SERVER_STATUS;
public:
TcpServerLibevent(int port,string bindip);
SERVER_STATUS Status();
~TcpServerLibevent();
int StartServerSync(); // 同步启动服务器
int StartServerAsync(); // 异步启动服务
private:
uint32_t m_port; // 监听端口号
string m_bind_ip; // 绑定端口号
int m_current_conection; // 当前连接数目
uint16_t m_backlog;
struct sockaddr_in m_server_addr; // 服务器地址
struct event_base * m_event_base;
struct evconnlistener* m_event_listener;
SERVER_STATUS m_status;
};
int test_tcp_server();