Skip to content

Commit 73d8de3

Browse files
Create TcpServer.h
1 parent e3a1ab7 commit 73d8de3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

TcpServer.h

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

0 commit comments

Comments
 (0)