-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpServer.h
57 lines (53 loc) · 1.54 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
//
// Created by yb on 4/5/21.
//
#include <iostream>
#include "Tcptool.h"
#include "Epoll.h"
#include <memory>
#ifndef REACTOR_TCPSERVER_H
#define REACTOR_TCPSERVER_H
class TcpServer{
public:
int createListenFd(int port) {
int _listenFd = 0;
_listenFd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
struct sockaddr_in _servAddr;
port = ((port <= 1024) || (port >= 65536)) ? 6666 : port;
::memset(&_servAddr, 0, sizeof(_servAddr));
_servAddr.sin_family = AF_INET;
_servAddr.sin_port = ::htons((unsigned short)port);
_servAddr.sin_addr.s_addr = ::htonl(INADDR_ANY);
if ((::bind(_listenFd, (struct sockaddr *) &_servAddr, sizeof(_servAddr))) == -1){
std::cout<<"listen error" <<std::endl;
return -1;
}
if((::listen(_listenFd,5)) == -1){
std::cout<<"listen error!"<<std::endl;
return -1;
}
return _listenFd;
}
public:
TcpServer(int port):_port(port),
_listenFd(createListenFd(_port)),
_listenTool(new Tcptool(_listenFd)),
_epoll(new Epoll()){
assert(_listenFd>=0);
}
void run();
private:
void _acceptConnection();
void _closeConnection(Tcptool *tcptool);
void _handleWrite(Tcptool *tcptool);
void _handleRead(Tcptool *tcptool);
private:
using EpollPtr = std::unique_ptr<Epoll>;
using ListenTcpToolPtr = std::unique_ptr<Tcptool>;
private:
int _port;
int _listenFd;
ListenTcpToolPtr _listenTool;
EpollPtr _epoll;
};
#endif //REACTOR_TCPSERVER_H