superdog/server/tcp_server_libevent.h

72 lines
1.5 KiB
C++

/*
* @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
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#ifdef linux
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define EVENT__HAVE_PTHREADS
#endif
extern "C"{
#include "event2/bufferevent.h"
#include "event2/buffer.h"
#include "event2/listener.h"
#include "event2/util.h"
#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();
#endif