nim_duilib/examples/proto_debuger/tcp_server_libevent.h

98 lines
2.5 KiB
C
Raw Normal View History

2021-11-19 00:37:35 +08:00
#ifndef GENERAL_TCPSERVER_H
#define GENERAL_TCPSERVER_H
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
2021-12-30 11:29:04 +08:00
#include <WinSock2.h>
2021-11-19 00:37:35 +08:00
#endif
2021-12-30 11:29:04 +08:00
2021-11-19 00:37:35 +08:00
#ifdef linux
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#define EVENT__HAVE_PTHREADS
#endif
2021-12-30 11:29:04 +08:00
#include <stdint.h>
2021-11-19 00:37:35 +08:00
#include <iostream>
#include <mutex>
#include <thread>
#include <map>
2021-12-30 11:29:04 +08:00
#include <functional>
2021-11-19 00:37:35 +08:00
using namespace std;
class TcpServerLibevent;
// tcp 连接
2021-12-30 11:29:04 +08:00
class ConnectionLibevent {
2021-11-19 00:37:35 +08:00
public:
2021-12-30 11:29:04 +08:00
ConnectionLibevent(TcpServerLibevent* p,
struct bufferevent* v,
uint32_t fd,
struct sockaddr_in* p1);
ConnectionLibevent(struct bufferevent* v,
uint32_t fd,
struct sockaddr_in* p1);
2022-01-13 01:37:24 +08:00
typedef std::function<void (char* p, uint32_t len)> OnRecvHandle;
2021-12-30 11:29:04 +08:00
virtual int OnRecv(char* p, uint32_t len); // data ready callback
virtual int OnClose(); // close callback
virtual int OnWrite(); // write data done callback
int WriteData(const char* p, uint16_t len);
int SetServer(TcpServerLibevent*);
2022-01-13 01:37:24 +08:00
int SetRecvHandler(OnRecvHandle);
2021-12-30 11:29:04 +08:00
TcpServerLibevent* Server();
string IpAddress();
uint32_t SocketFd();
int Close();
2021-11-19 00:37:35 +08:00
private:
2021-12-30 11:29:04 +08:00
int m_bytes_send;
int m_bytes_recv;
TcpServerLibevent* m_parent_server;
struct bufferevent* m_event;
struct sockaddr_in* m_addr;
uint32_t m_fd;
2022-01-13 01:37:24 +08:00
OnRecvHandle m_recv_handle;
2021-11-19 00:37:35 +08:00
};
// 管理服务端
class TcpServerLibevent {
2021-12-30 11:29:04 +08:00
typedef enum {
RUNNING,
STOP,
FAIL
}SERVER_STATUS;
2021-11-19 00:37:35 +08:00
public:
2022-01-08 00:23:09 +08:00
typedef std::function<void (ConnectionLibevent*)> OnAccept;
2022-01-05 01:41:58 +08:00
typedef std::function<void (ConnectionLibevent*)> OnDisconnect;
2021-12-30 11:29:04 +08:00
TcpServerLibevent(int port, string bindip);
SERVER_STATUS Status();
~TcpServerLibevent();
int StartServerAndRunSync(); // 同步启动服务器
int StartServerAsync(); // 异步启动服务
int RemoveConnection(uint32_t);
int SetNewConnectionHandle(OnAccept);
2022-01-06 00:31:24 +08:00
int SetConnectionLeaveHandle(OnDisconnect);
2021-12-30 11:29:04 +08:00
int AddConnection(uint32_t fd, ConnectionLibevent* p);
2021-12-30 16:56:16 +08:00
uint64_t SocketFd();
2021-12-31 15:35:46 +08:00
int ConnectionCount();
2021-12-30 11:29:04 +08:00
friend struct ServerCallbacks;
2021-11-19 00:37:35 +08:00
private:
2021-12-30 11:29:04 +08:00
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;
thread* m_thread;
map<uint32_t, ConnectionLibevent*> m_map_client;
OnAccept m_handle_accept;
2022-01-05 01:41:58 +08:00
OnDisconnect m_handle_disconnect;
2021-12-30 16:56:16 +08:00
intptr_t mSocketFD; // 操作系统原生socket
2021-11-19 00:37:35 +08:00
};
2021-12-30 11:29:04 +08:00
#endif