-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathepollwapper.h
70 lines (61 loc) · 1.96 KB
/
epollwapper.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
65
66
67
68
69
70
//
// epollwapper.h
//
// Created by 吴怡生 on 12/03/2018.
// Copyright © 2018 yeshen.org. All rights reserved.
//
#ifndef EPOLL_WAPPER_H
#define EPOLL_WAPPER_H
#include <sys/epoll.h>
#include <inttypes.h>
#include "common.h"
#define MAX_CONNECTION 10
#define TYPE_WATCH '\x01'
#define TYPE_TCP '\x02'
#define TYPE_UDP '\x04'
enum NetStatus{
NET_SUCC = '\x00', //succeeded
NET_FAIL = '\x01', //general SOCKS server failure
NET_NOTALLOW = '\x02', //connection not allowed by ruleset
NET_NUREACH = '\x03', //Network unreachable
NET_HUREACH = '\x04', //Host unreachable
NET_CREFUSH = '\x05', //Connection refused
NET_TTL = '\x06', //TTL expired
NET_COMMAND = '\x07', //Command not supported
NET_ADRESS = '\x08' //Address type not supported
};
struct EpollData{
int fd;
uint8_t type;
void* ptr;
};
class EpollNotify{
public:
virtual void onAccept(int fd,int type,void* ptr) = 0;
virtual void onData(struct LinkBuff* buf,void* ptr,int fd) = 0;
virtual void onWritable(void* ptr,int fd) = 0;
virtual void onDestory(void* data,int fd) = 0;
};
class EpollWapper{
public:
EpollWapper();
int create(EpollNotify* listener);
NetStatus watchTcp(uint16_t port,int* fd,void* ptr);
NetStatus watchUdp(uint16_t port,int* fd,void* ptr);
NetStatus createTcp(char* target_ip, uint16_t target_port,int* fd,void* ptr);
NetStatus createUdp(char* target_ip, uint16_t target_port,int* fd,void* ptr);
bool remove(int fd);
void wait();
~EpollWapper();
private:
EpollNotify* listener_;
struct epoll_event* event_;
int epoll_fd_;
bool setNonblocking(int fd);
bool addIntoEpoll(int fd,void* ptr);
void handleTcpData(int fd,void* ptr);
void handleUdpData(int fd,void* ptr);
void acceptTcp(int fd,void* ptr);
void acceptUdp(int fd,void* ptr);
};
#endif