forked from fishfishfishh/FishWebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpServer.h
65 lines (59 loc) · 1.48 KB
/
TcpServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#define ET
#include <string>
#include <memory>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <assert.h>
#include <string.h>
#include <vector>
#include <fcntl.h>
#include <stdlib.h>
#include <cassert>
#include <unordered_map>
#include "HttpConnection.h"
#include "CallBacks.h"
#include "ThreadPool.h"
#include <signal.h>
#include "TimeWheel.h"
const int TIMESLOT = 1; //最小超时单位
class HttpConnection;
class TcpServer
{
public:
typedef std::vector<epoll_event> EventVector;
typedef std::unordered_map<int, TcpConnectionPtr> TcpMap;
TcpServer(std::string ipAddr, uint16_t port);
void sethandleRead(const handleEventCallBack& cb) { handleRead = cb; }
void sethandleWrite(const handleEventCallBack& cb) { handleWrite = cb; }
void start();
~TcpServer()
{
close(m_listenfd);
close(socketPair[0]);
close(socketPair[1]);
}
private:
int initialSocketfd();
private:
handleEventCallBack handleRead;
handleEventCallBack handleWrite;
void newConnection(int socketfd);
void removeConnection(int socketfd);
void removeConnection(const TcpConnectionPtr);
void processEvents(int, const handleEventCallBack&);
static const uint32_t initEventVectorSize = 100;
sockaddr_in address;
EventVector events;
int m_epollfd;
int m_listenfd;
int start_loop;
TcpMap userMap;
ThreadPool handleEventsPool;
//timer
int socketPair[2];
TimeWheel timeWheel;
};