-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.h
executable file
·107 lines (89 loc) · 2.07 KB
/
net.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
#ifndef ASYNC_SERVER_NET_H_
#define ASYNC_SERVER_NET_H_
#ifdef __cplusplus
extern "C" {
#endif
// C89
#include <time.h>
// C99
#include <stdint.h>
// Posix
#include <netinet/in.h>
#include <sys/socket.h>
// Linux
#include <sys/epoll.h>
#include <libtaomee/list.h>
#define CN_NEED_CLOSE 0x01
#define CN_NEED_POLLIN 0x02
enum {
//page_size = 8192
def_page_size = 8192
};
typedef struct conn_buf {
uint32_t rcvprotlen;
uint32_t recvlen;
uint32_t sendlen;
uint32_t sndbufsz;
uint8_t* recvptr;
uint8_t* sendptr;
} conn_buf_t;
typedef struct skinfo {
uint16_t remote_port;
uint32_t remote_ip;
uint32_t last_tm;
} __attribute__((packed)) skinfo_t;
enum {
fd_type_unused = 0,
fd_type_listen,
fd_type_pipe,
fd_type_remote,
fd_type_mcast,
fd_type_addr_mcast,
fd_type_udp,
fd_type_asyn_connect
};
struct bind_config_elem;
typedef struct fdinfo {
uint32_t id;
int sockfd;
uint8_t type;
uint8_t flag;
conn_buf_t cb;
skinfo_t sk;
struct bind_config_elem* bc_elem;
// callback for asynchronous connect
void (*callback)(int fd, void* arg);
void* arg;
list_head_t list;
} fdinfo_t;
struct epinfo {
fdinfo_t* fds;
struct epoll_event* evs;
list_head_t close_head;
list_head_t etin_head;
int epfd;
int maxfd;
int max_ev_num;
int count;
};
extern struct epinfo epi;
extern time_t socket_timeout;
extern int page_size;
extern int g_listen_port;
extern char g_listen_ip[16];
extern uint32_t g_send_buf_limit_size;
int net_init(int size, int maxevents);
int net_loop(int timeout, int max_len, int is_conn);
int net_start(const char *listen_ip, uint16_t listen_port, struct bind_config_elem* bc_elem);
void net_exit();
int mod_events(int epfd, int fd, uint32_t flag);
int do_write_conn(int fd);
int do_add_conn(int fd, uint8_t type, struct sockaddr_in *peer, struct bind_config_elem* bc_elem);
void do_del_conn(int fd, int is_conn);
//使得work进程可以支持udp.
//bind udp 端口,然后在proc_udp_pkg 中处理得到的数据
int bind_udp_socket( const char* ipaddr, in_port_t port );
#ifdef __cplusplus
} // end of extern "C"
#endif
#endif // ASYNC_SERVER_NET_H_