-
Notifications
You must be signed in to change notification settings - Fork 0
/
rev_server.h
146 lines (118 loc) · 3.08 KB
/
rev_server.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
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef REV_SERVER_H
#define REV_SERVER_H
#include "protocol.h"
#include "fifobuf.h"
#include <netinet/in.h>
enum
{
MAX_HTTP_IDLERS=4,
MAX_NETWORK_HANDLES=64,
};
struct rev_client
{
int sock;
int date;
/* tcp buffers */
struct FIFOBUF rev_in_buf;
struct FIFOBUF rev_out_buf;
char in_buf[NETWORK_BUF_SIZE];
char out_buf[NETWORK_BUF_SIZE];
};
enum
{
/* http idler states */
HTTP_IDLER_OFFLINE=0,
HTTP_IDLER_CONNECTING,
HTTP_IDLER_ONLINE,
};
struct http_idler
{
int state;
int sock;
time_t date; /* timestamp since online */
char http_host[128];
char http_uri[256];
struct sockaddr_in addr;
};
enum
{
/* network handle states */
NETW_HNDL_OFFLINE=0,
NETW_HNDL_TCP_INIT_CONNECT,
NETW_HNDL_TCP_CONNECT,
NETW_HNDL_TCP_ONLINE,
NETW_HNDL_TCP_DISC,
NETW_HNDL_TCP_FAIL,
};
struct network_handle
{
int state;
int id;
struct rev_client *cl;
void *user_ptr;
struct netaddr dst_addr;
/* tcp buffers */
struct FIFOBUF tcp_in_buf;
struct FIFOBUF tcp_out_buf;
char in_buf[NETWORK_BUF_SIZE];
char out_buf[NETWORK_BUF_SIZE];
int terminate;
};
struct rev_server
{
/*
this socket is used to send a http request
which initiates the php script
*/
struct http_idler http_idlers[MAX_HTTP_IDLERS];
/*
this socket is used to accept the connection
requested by the php script
*/
int rev_listen_sock;
/* we need two connections to rev sockssrv */
struct rev_client clients[2];
/* client that should be used */
struct rev_client *usable_cl;
/* url to php script */
char http_url[512];
/* php sockssrv will use these to establish a connection to us */
char bind_ip[128];
int bind_port;
/*
we expect the websrv to close the
connection after about 60 seconds.
the http idlers should close the connection
before this happens
*/
int http_timeout;
fd_set *read_fds;
fd_set *write_fds;
/* used to create http idlers periodically */
time_t new_idler_date;
int last_idler_lifetime;
struct network_handle netw_hndls[MAX_NETWORK_HANDLES];
};
int revsrv_init(struct rev_server *revsrv, const char *bind_ip,
int bind_port, const char *http_url, int http_timeout);
int revsrv_get_fds(struct rev_server *revsrv, fd_set *rfds, fd_set *wfds);
int revsrv_max_block_time(struct rev_server *revsrv);
void revsrv_tick(struct rev_server *revsrv, fd_set *rfds, fd_set *wfds);
struct rev_client *revsrv_usable_cl(struct rev_server *revsrv);
struct network_handle *revsrv_netw_hndl(struct rev_server *revsrv, int id);
/* network handle pool */
int revsrv_new_netw_hndl(struct rev_server *revsrv);
void revsrv_free_netw_hndl(struct rev_server *revsrv, int id);
/* initiate a tcp connection (returns: connection handle id) */
int revsrv_init_conn(struct rev_server *revsrv, struct netaddr *addr, void *user_ptr);
int revsrv_cl_send(struct rev_server *revsrv, int netw_handle, const char *data, int size);
void revsrv_cl_close(struct rev_server *revsrv, int netw_hndl);
int revsrv_cl_recv(struct rev_server *revsrv, int netw_hndl, char *dst, int size);
enum
{
REV_CONN_PENDING=0,
REV_CONN_ONLINE,
REV_CONN_FAILED,
};
int revsrv_conn_state(struct rev_server *revsrv, int netw_hndl);
#endif