forked from swoole/php-cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpClientNet.c
executable file
·167 lines (152 loc) · 4.3 KB
/
cpClientNet.c
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
/*
+----------------------------------------------------------------------+
| common con pool |
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinhua Guo <woshiguo35@sina.com> |
+----------------------------------------------------------------------+
*/
#include "php_connect_pool.h"
#include <netdb.h>
int cpClient_close(cpClient *cli)
{
int ret, fd = cli->sock;
cli->sock = 0;
ret = close(fd);
if (ret < 0)
{
cpLog("client close fail. Error: %s[%d]", strerror(errno), errno);
}
CPGS = NULL;
return ret;
}
int cpClient_recv(int sock, void *data, int len, int waitall)
{
// int flag = 0, ret;
// if (waitall == 1) {
// flag = MSG_WAITALL;
// }
//
// ret = recv(cli->sock, data, len, flag);
//
// if (ret < 0) {
// if (errno == EINTR) {
// ret = recv(cli->sock, data, len, flag);
// } else {
// return SUCCESS;
// }
// }
return cpNetRead(sock, data, len);
}
int cpClient_send(int sock, char *data, int length, int flag)
{
int written = 0;
int n;
assert(length > 0);
assert(data != NULL);
//总超时,for循环中计时
while (written < length)
{
n = send(sock, data, length - written, flag);
if (n < 0)
{
//中断
if (errno == EINTR)
{
continue;
}//让出
else if (errno == EAGAIN)
{
usleep(1);
continue;
}
else
{
return SUCCESS;
}
}
written += n;
data += n;
}
return written;
}
int cpClient_create(cpClient *cli)
{
bzero(cli, sizeof (cpClient));
cli->sock = socket(AF_INET, SOCK_STREAM, 0);
int flag = 1;
setsockopt(cli->sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof (flag));
if (cli->sock < 0)
{
return FAILURE;
}
return SUCCESS;
}
static int swClient_inet_addr(struct sockaddr_in *sin, char *string)
{
struct in_addr tmp;
struct hostent *host_entry;
if (inet_aton(string, &tmp))
{
sin->sin_addr.s_addr = tmp.s_addr;
}
else
{
if (!(host_entry = gethostbyname(string)))
{
cpLog("Host lookup failed. Error: %s[%d] ", strerror(errno), errno);
return SUCCESS;
}
if (host_entry->h_addrtype != AF_INET)
{
cpLog("Host lookup failed: Non AF_INET domain returned on AF_INET socket");
return 0;
}
memcpy(&(sin->sin_addr.s_addr), host_entry->h_addr_list[0], host_entry->h_length);
}
return SUCCESS;
}
int cpClient_connect(cpClient *cli, char *host, int port, double timeout, int nonblock)
{
int ret;
cli->serv_addr.sin_family = AF_INET;
cli->serv_addr.sin_port = htons(port);
if (swClient_inet_addr(&cli->serv_addr, host) < 0)
{
return SUCCESS;
}
cli->timeout = timeout;
if (nonblock == 1)
{
cpSetNonBlock(cli->sock);
}
else
{
cpSetTimeout(cli->sock, timeout);
}
// int count = 0;
while (1)
{
ret = connect(cli->sock, (struct sockaddr *) (&cli->serv_addr), sizeof (cli->serv_addr));
if (ret < 0)
{
if (errno == EINTR)
{
continue;
}
// if (++count <= 15) {//防止重启代理导致的con refused
// usleep(100000);
// continue;
// }
}
break;
}
return ret;
}