superdog/server/tcp_server_libevent.h

76 lines
1.9 KiB
C
Raw Normal View History

2021-07-02 05:53:07 +00:00
/*
* @Author: your name
* @Date: 2021-06-30 16:23:10
2021-07-02 06:25:37 +00:00
* @LastEditTime: 2021-07-02 14:05:05
2021-07-02 05:53:07 +00:00
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \server\tcp_server_libevent.h
*/
2021-06-30 08:46:08 +00:00
#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"
};
2021-07-02 05:53:07 +00:00
#include <stdint.h>
2021-06-30 08:46:08 +00:00
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
2021-07-02 05:53:07 +00:00
// 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(); // 异步启动服务
2021-07-02 06:25:37 +00:00
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);
2021-07-02 05:53:07 +00:00
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;
};
2021-06-30 09:31:56 +00:00
int test_tcp_server();
2021-06-30 08:46:08 +00:00
#endif