-
Notifications
You must be signed in to change notification settings - Fork 9
/
rpc.cpp
293 lines (220 loc) · 5.78 KB
/
rpc.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "stratum.h"
//#define RPC_DEBUGLOG_
bool rpc_connected(YAAMP_RPC *rpc)
{
return rpc->sock > 0;
}
bool rpc_connect(YAAMP_RPC *rpc)
{
rpc_close(rpc);
if(g_exiting) return false;
struct hostent *ent = gethostbyname(rpc->host);
if(!ent) return false;
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_port = htons(rpc->port);
bcopy((char *)ent->h_addr, (char *)&serv.sin_addr.s_addr, ent->h_length);
rpc->sock = socket(AF_INET, SOCK_STREAM, 0);
if(rpc->sock <= 0) return false;
int res = connect(rpc->sock, (struct sockaddr *)&serv, sizeof(serv));
if(res < 0)
{
rpc_close(rpc);
return false;
}
yaamp_create_mutex(&rpc->mutex);
rpc->id = 0;
rpc->bufpos = 0;
if (g_debuglog_rpc) {
debuglog("connected to %s:%d\n", rpc->host, rpc->port);
}
return true;
}
void rpc_close(YAAMP_RPC *rpc)
{
if(!rpc_connected(rpc)) return;
pthread_mutex_destroy(&rpc->mutex);
close(rpc->sock);
rpc->sock = 0;
if (g_debuglog_rpc) {
debuglog("disconnected from %s:%d\n", rpc->host, rpc->port);
}
}
///////////////////////////////////////////////////////////////////
int rpc_send_raw(YAAMP_RPC *rpc, const char *buffer, int bytes)
{
if(!rpc_connected(rpc)) return -1;
int res = send(rpc->sock, buffer, bytes, MSG_NOSIGNAL);
if(res <= 0) return res;
if (g_debuglog_rpc) {
debuglog("sending >%s<\n", buffer);
}
return res;
}
int rpc_flush_soft(YAAMP_RPC *rpc)
{
if(!rpc_connected(rpc)) return -1;
int res = send(rpc->sock, rpc->buffer, rpc->bufpos, MSG_MORE);
rpc->bufpos = 0;
return res;
}
int rpc_flush(YAAMP_RPC *rpc)
{
if(!rpc_connected(rpc)) return -1;
int res = rpc_send_raw(rpc, rpc->buffer, rpc->bufpos);
rpc->bufpos = 0;
return res;
}
int rpc_send(YAAMP_RPC *rpc, const char *format, ...)
{
if(!rpc_connected(rpc)) return -1;
char buffer[YAAMP_SMALLBUFSIZE] = { 0 };
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
int bytes = strlen(buffer);
if(bytes + rpc->bufpos > YAAMP_SMALLBUFSIZE)
return -1;
memcpy(rpc->buffer + rpc->bufpos, buffer, bytes);
rpc->bufpos += bytes;
return bytes;
}
/////////////////////////////////////////////////////////////////////////////////
char *rpc_do_call(YAAMP_RPC *rpc, char const *data)
{
CommonLock(&rpc->mutex);
// HTTP 1.1 accepts chunked data, and keep the connection
rpc_send(rpc, "POST / HTTP/1.1\r\n");
rpc_send(rpc, "Authorization: Basic %s\r\n", rpc->credential);
rpc_send(rpc, "Host: %s:%d\n", rpc->host, rpc->port);
rpc_send(rpc, "Accept: */*\r\n");
rpc_send(rpc, "Content-Type: application/json\r\n");
rpc_send(rpc, "Content-Length: %d\r\n\r\n", strlen(data));
int res = rpc_flush(rpc);
if(res <= 0)
{
CommonUnlock(&rpc->mutex);
return NULL;
}
res = rpc_send_raw(rpc, data, strlen(data));
if(res <= 0)
{
CommonUnlock(&rpc->mutex);
return NULL;
}
int bufpos = 0;
char buffer[YAAMP_SMALLBUFSIZE] = { 0 };
while(!g_exiting)
{
int bytes = recv(rpc->sock, buffer+bufpos, YAAMP_SMALLBUFSIZE-bufpos-1, 0);
if (g_debuglog_rpc) {
debuglog("got %s\n", buffer+bufpos);
}
if(bytes <= 0)
{
debuglog("ERROR: recv1, %d, %d, %s, %s\n", bytes, errno, data, buffer);
CommonUnlock(&rpc->mutex);
return NULL;
}
bufpos += bytes;
buffer[bufpos] = 0;
if(strstr(buffer, "\r\n\r\n")) break;
}
///////////////////////////////////////////////////
const char *p = strchr(buffer, ' ');
if(!p)
{
CommonUnlock(&rpc->mutex);
return NULL;
}
int status = atoi(p+1);
if(status != 200)
debuglog("ERROR: rpc_do_call: %s:%d %d\n", rpc->host, rpc->port, status);
char tmp[1024];
header_value(buffer, "Transfer-Encoding:", tmp);
if (!strcmp(tmp, "chunked")) {
#ifdef HAVE_CURL
if (!rpc->curl) debuglog("%s chunked transfer detected, switching to curl!\n",
rpc->coind->symbol);
rpc->curl = 1;
#endif
CommonUnlock(&rpc->mutex);
rpc_connect(rpc);
return NULL;
}
int datalen = atoi(header_value(buffer, "Content-Length:", tmp));
if(!datalen)
{
debuglog("ERROR: rpc No Content-Length header!\n");
CommonUnlock(&rpc->mutex);
return NULL;
}
p = strstr(buffer, "\r\n\r\n");
bufpos = strlen(p+4);
char *databuf = (char *)malloc(datalen+1);
if(!databuf)
{
CommonUnlock(&rpc->mutex);
return NULL;
}
memcpy(databuf, p+4, bufpos+1);
while(bufpos < datalen)
{
int bytes = recv(rpc->sock, databuf+bufpos, datalen-bufpos, 0);
if(bytes <= 0)
{
debuglog("ERROR: recv2, %d, %d, %s\n", bytes, errno, data);
rpc_connect(rpc);
free(databuf);
CommonUnlock(&rpc->mutex);
return NULL;
}
bufpos += bytes;
databuf[bufpos] = 0;
}
CommonUnlock(&rpc->mutex);
header_value(buffer, "Connection:", tmp);
if(strcmp(tmp, "close") == 0)
{
// debuglog("closing connection from %s:%d\n", rpc->host, rpc->port);
rpc_connect(rpc);
}
return databuf;
}
json_value *rpc_call(YAAMP_RPC *rpc, char const *method, char const *params)
{
// debuglog("rpc_call :%d %s\n", rpc->port, method);
#ifdef HAVE_CURL
if (rpc->ssl || rpc->curl)
return rpc_curl_call(rpc, method, params);
#endif
int s1 = current_timestamp();
if(!rpc_connected(rpc)) return NULL;
int paramlen = params? strlen(params): 0;
char *message = (char *)malloc(paramlen+1024);
if(!message) return NULL;
if(params)
sprintf(message, "{\"method\":\"%s\",\"params\":%s,\"id\":\"%d\"}", method, params, ++rpc->id);
else
sprintf(message, "{\"method\":\"%s\",\"id\":\"%d\"}", method, ++rpc->id);
char *buffer = rpc_do_call(rpc, message);
free(message);
if(!buffer) return NULL;
json_value *json = json_parse(buffer, strlen(buffer));
if(!json) {
debuglog("invalid json: %s", buffer);
free(buffer);
return NULL;
}
free(buffer);
int s2 = current_timestamp();
if(s2-s1 > 2000)
debuglog("delay rpc_call %s:%d %s in %d ms\n", rpc->host, rpc->port, method, s2-s1);
if(json->type != json_object)
{
json_value_free(json);
return NULL;
}
return json;
}