-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathconev.h
134 lines (106 loc) · 2.71 KB
/
conev.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef CONEV_H
#define CONEV_H
#include <stdint.h>
#include <stdbool.h>
#ifndef __linux__
#define NOEPOLL
#endif
#ifdef _WIN32
#include <ws2tcpip.h>
#define poll(fds, cnt, to) WSAPoll(fds, cnt, to)
#define close(fd) closesocket(fd)
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#ifndef NOEPOLL
#include <sys/epoll.h>
#define POLLIN EPOLLIN
#define POLLOUT EPOLLOUT
#define POLLERR EPOLLERR
#define POLLHUP EPOLLHUP
#define POLLRDHUP EPOLLRDHUP
#else
#include <sys/poll.h>
#endif
#endif
#ifdef __APPLE__
#define POLLRDHUP POLLHUP
#elif !defined POLLRDHUP
#define POLLRDHUP 0
#endif
#define POLLTIMEOUT 0
struct poolhd;
struct eval;
typedef int (*evcb_t)(struct poolhd *, struct eval *, int);
union sockaddr_u {
struct sockaddr sa;
struct sockaddr_in in;
struct sockaddr_in6 in6;
};
#define FLAG_S4 1
#define FLAG_S5 2
#define FLAG_CONN 4
#define FLAG_HTTP 8
struct buffer {
size_t size;
unsigned int offset;
ssize_t lock;
struct buffer *next;
char data[];
};
struct eval {
int fd;
int index;
unsigned long long mod_iter;
evcb_t cb;
long tv_ms;
struct eval *tv_next, *tv_prev;
struct eval *pair;
struct buffer *buff, *sq_buff;
int flag;
union sockaddr_u addr;
ssize_t recv_count;
ssize_t round_sent;
unsigned int round_count;
int attempt;
bool cache;
bool mark; //
};
struct poolhd {
int max;
int count;
int efd;
struct eval **links;
struct eval *items;
#ifndef NOEPOLL
struct epoll_event *pevents;
#else
struct pollfd *pevents;
#endif
unsigned long long iters;
bool brk;
struct eval *tv_start, *tv_end;
struct buffer *root_buff;
};
struct poolhd *init_pool(int count);
struct eval *add_event(struct poolhd *pool, evcb_t cb, int fd, int e);
struct eval *add_pair(struct poolhd *pool, struct eval *val, int sfd, int e);
void del_event(struct poolhd *pool, struct eval *val);
void destroy_pool(struct poolhd *pool);
struct eval *next_event(struct poolhd *pool, int *offs, int *type, int ms);
int mod_etype(struct poolhd *pool, struct eval *val, int type);
void set_timer(struct poolhd *pool, struct eval *val, long ms);
void remove_timer(struct poolhd *pool, struct eval *val);
void loop_event(struct poolhd *pool);
struct buffer *buff_pop(struct poolhd *pool, size_t size);
void buff_push(struct poolhd *pool, struct buffer *buff);
void buff_destroy(struct buffer *root);
static struct buffer *buff_ppop(struct poolhd *pool, size_t size)
{
struct buffer *b = buff_pop(pool, size);
if (b) buff_push(pool, b);
return b;
}
#endif