-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathclient.h
182 lines (152 loc) Β· 4.95 KB
/
client.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef SW_CLIENT_H_
#define SW_CLIENT_H_
SW_EXTERN_C_BEGIN
#include "buffer.h"
#include "connection.h"
#define SW_SOCK_ASYNC 1
#define SW_SOCK_SYNC 0
#define SW_HTTPS_PROXY_HANDSHAKE_RESPONSE "HTTP/1.1 200 Connection established"
enum swClient_pipe_flag
{
SW_CLIENT_PIPE_TCP_SESSION = 1,
};
enum swHttp_proxy_state
{
SW_HTTP_PROXY_STATE_WAIT = 0,
SW_HTTP_PROXY_STATE_HANDSHAKE,
SW_HTTP_PROXY_STATE_READY,
};
struct _http_proxy
{
uint8_t state;
uint8_t dont_handshake;
int proxy_port;
const char *proxy_host;
const char *user;
const char *password;
int l_user;
int l_password;
const char *target_host;
int l_target_host;
int target_port;
char buf[512];
};
typedef struct _swClient
{
int id;
int type;
long timeout_id; //timeout node id
int _sock_type;
int _sock_domain;
int _protocol;
int reactor_fdtype;
uchar active :1;
uchar async :1;
uchar keep :1;
uchar destroyed :1;
uchar http2 :1;
uchar sleep :1;
uchar wait_dns :1;
uchar shutdow_rw :1;
uchar shutdown_read :1;
uchar shutdown_write :1;
uchar remove_delay :1;
uchar closed :1;
uchar high_watermark :1;
/**
* one package: length check
*/
uchar open_length_check :1;
uchar open_eof_check :1;
swProtocol protocol;
struct _swSocks5 *socks5_proxy;
struct _http_proxy* http_proxy;
uint32_t reuse_count;
const char *server_str;
const char *server_host;
int server_port;
void *ptr;
void *params;
uint8_t server_strlen;
double timeout;
swTimer_node *timer;
/**
* signal interruption
*/
double interrupt_time;
/**
* sendto, read only.
*/
swSocketAddress server_addr;
/**
* recvfrom
*/
swSocketAddress remote_addr;
swSocket *socket;
void *object;
swString *buffer;
uint32_t wait_length;
uint32_t buffer_input_size;
uint32_t buffer_high_watermark;
uint32_t buffer_low_watermark;
#ifdef SW_USE_OPENSSL
uchar open_ssl :1;
uchar ssl_wait_handshake :1;
SSL_CTX *ssl_context;
swSSL_option ssl_option;
#endif
void (*onConnect)(struct _swClient *cli);
void (*onError)(struct _swClient *cli);
void (*onReceive)(struct _swClient *cli, char *data, uint32_t length);
void (*onClose)(struct _swClient *cli);
void (*onBufferFull)(struct _swClient *cli);
void (*onBufferEmpty)(struct _swClient *cli);
int (*connect)(struct _swClient *cli, const char *host, int port, double _timeout, int sock_flag);
int (*send)(struct _swClient *cli, const char *data, int length, int flags);
int (*sendfile)(struct _swClient *cli, const char *filename, off_t offset, size_t length);
int (*recv)(struct _swClient *cli, char *data, int len, int flags);
int (*close)(struct _swClient *cli);
} swClient;
void swClient_init_reactor(swReactor *reactor);
int swClient_create(swClient *cli, int type, int async);
int swClient_sleep(swClient *cli);
int swClient_wakeup(swClient *cli);
int swClient_shutdown(swClient *cli, int __how);
#ifdef SW_USE_OPENSSL
int swClient_enable_ssl_encrypt(swClient *cli);
int swClient_ssl_handshake(swClient *cli);
int swClient_ssl_verify(swClient *cli, int allow_self_signed);
#endif
void swClient_free(swClient *cli);
//----------------------------------------Stream---------------------------------------
typedef struct _swStream
{
swString *buffer;
uint8_t cancel;
void *private_data;
void (*response)(struct _swStream *stream, char *data, uint32_t length);
swClient client;
} swStream;
swStream* swStream_new(char *dst_host, int dst_port, int type);
int swStream_send(swStream *stream, char *data, size_t length);
void swStream_set_protocol(swProtocol *protocol);
void swStream_set_max_length(swStream *stream, uint32_t max_length);
int swStream_recv_blocking(int fd, void *__buf, size_t __len);
//----------------------------------------Stream End------------------------------------
SW_EXTERN_C_END
#endif /* SW_CLIENT_H_ */