-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
1386 lines (1208 loc) · 33.3 KB
/
client.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
// Copyright (C) 2021 Marcelo Diop-Gonzalez
#include <glib.h>
#include <jsmn/jsmn.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <sys/queue.h>
#include "auth.h"
#include "buf.h"
#include "compiler.h"
#include "exchg/currency.h"
#include "client.h"
#include "exchg/exchg.h"
#include "net-backend.h"
#include "order-book.h"
#include "time-helpers.h"
#include "exchanges/bitstamp.h"
#include "exchanges/coinbase.h"
#include "exchanges/gemini.h"
#include "exchanges/kraken.h"
struct json {
jsmn_parser parser;
jsmntok_t *tokens;
int num_tokens;
char *buf;
int buf_size;
int buf_pos;
};
struct retry {
struct timer *timer;
struct timespec last_connected;
int seconds_idx;
};
struct websocket {
char *host;
char *path;
struct json json;
struct exchg_client *cl;
struct websocket_conn *conn;
const struct exchg_websocket_ops *ops;
bool established;
bool disconnecting;
struct retry retry;
LIST_ENTRY(websocket) list;
char private[];
};
struct http {
char *host;
char *path;
char *method;
struct buf body;
struct json json;
struct exchg_client *cl;
struct http_conn *conn;
const struct exchg_http_ops *ops;
bool print_data;
bool want_retry;
struct retry retry;
LIST_ENTRY(http) list;
char private[];
};
bool websocket_disconnecting(struct websocket *w) {
return w->disconnecting;
}
bool websocket_established(struct websocket *w) {
if (!w)
return false;
return w->established;
}
const char *websocket_host(struct websocket *w) {
return w->host;
}
const char *websocket_path(struct websocket *w) {
return w->path;
}
const char *http_method(struct http *h) {
return h->method;
}
const char *http_host(struct http *h) {
return h->host;
}
const char *http_path(struct http *h) {
return h->path;
}
void for_each_websocket(struct exchg_client *cl,
int (*func)(struct websocket *w, void *private),
void *private) {
struct websocket *w;
LIST_FOREACH(w, &cl->websocket_list, list) {
if (func(w, private))
return;
}
}
static int websocket_buf_write(struct websocket *ws, const char *fmt,
va_list ap, int len) {
struct buf b;
if (buf_alloc(&b, len+1, _net_write_buf_padding))
return -1;
len = buf_vsprintf(&b, fmt, ap);
if (len < 0) {
free(b.buf);
return len;
}
len = ws_conn_write(ws->conn, buf_start(&b), b.len);
free(b.buf);
return len;
}
int websocket_printf(struct websocket *ws, const char *fmt, ...) {
va_list a, ap;
int len;
char buf[1024];
va_start(ap, fmt);
va_copy(a, ap);
// paranoid...
if (unlikely(_net_write_buf_padding >= sizeof(buf))) {
len = websocket_buf_write(ws, fmt, ap, 1024);
va_end(ap);
va_end(a);
return len;
}
len = vsnprintf(&buf[_net_write_buf_padding],
sizeof(buf)-_net_write_buf_padding, fmt, ap);
if (len < sizeof(buf)-_net_write_buf_padding)
len = ws_conn_write(ws->conn, &buf[_net_write_buf_padding], len);
else
len = websocket_buf_write(ws, fmt, a, len);
va_end(ap);
va_end(a);
return len;
}
static void conn_offline(struct exchg_context *ctx) {
ctx->online = false;
for (enum exchg_id id = 0; id < EXCHG_ALL_EXCHANGES; id++) {
struct exchg_client *cl = ctx->clients[id];
if (cl && (!LIST_EMPTY(&cl->websocket_list) ||
!LIST_EMPTY(&cl->http_list))) {
ctx->online = true;
return;
}
}
if (ctx->running) {
net_stop(ctx->net_context);
ctx->running = false;
}
}
static void json_free(struct json *json) {
free(json->buf);
free(json->tokens);
}
static void __http_free(struct http *h) {
if (h->ops->on_free)
h->ops->on_free(h->cl, h);
json_free(&h->json);
free(h->method);
free(h->host);
free(h->path);
free(h->body.buf);
free(h);
}
static void http_free(struct http *h) {
LIST_REMOVE(h, list);
conn_offline(h->cl->ctx);
__http_free(h);
}
static void __websocket_free(struct websocket *w) {
json_free(&w->json);
free(w->host);
free(w->path);
free(w);
}
static void websocket_free(struct websocket *w) {
LIST_REMOVE(w, list);
conn_offline(w->cl->ctx);
__websocket_free(w);
}
void websocket_close(struct websocket *w) {
w->disconnecting = true;
if (w->conn) {
ws_conn_close(w->conn);
} else if (w->retry.timer) {
timer_cancel(w->retry.timer);
websocket_free(w);
}
// else { we're inside the on_l2_disconnect() callback }
}
void http_close(struct http *h) {
if (h->conn)
http_conn_close(h->conn);
else {
timer_cancel(h->retry.timer);
http_free(h);
}
}
#ifndef LIST_FOREACH_SAFE
#define LIST_FOREACH_SAFE(var, head, field, tmp) \
for ((var) = ((head)->lh_first); \
(var) && ((tmp) = (var)->field.le_next, 1); \
(var) = (tmp))
#endif
void exchg_teardown(struct exchg_client *cl) {
// TODO: add a callback and call it here,
// send order cancels, etc. Set a bit and
// look at that bit in the main callback
struct websocket *w, *wtmp;
struct http *h, *htmp;
LIST_FOREACH_SAFE(w, &cl->websocket_list, list, wtmp)
websocket_close(w);
LIST_FOREACH_SAFE(h, &cl->http_list, list, htmp)
http_close(h);
}
static void put_response(char *in, size_t len) {
fwrite(in, 1, len, stderr);
fputc('\n', stderr);
}
static int json_buf_add(struct json *json, char *in, size_t len) {
if (len + json->buf_pos > json->buf_size) {
size_t new_sz = 2 * (len+json->buf_pos);
char *buf = realloc(json->buf, new_sz);
if (!buf) {
exchg_log("%s: OOM\n", __func__);
return -ENOMEM;
}
json->buf = buf;
json->buf_size = new_sz;
}
memcpy(json->buf + json->buf_pos, in, len);
json->buf_pos += len;
return 0;
}
static void json_init(struct json *json) {
jsmn_init(&json->parser);
json->buf_pos = 0;
}
static int json_parse(struct json *j, char *in,
size_t len, char **json, size_t *json_len) {
char *data = in;
size_t data_len = len;
if (j->buf_pos > 0) {
if (json_buf_add(j, in, len))
return -ENOMEM;
data = j->buf;
data_len = j->buf_pos;
}
int numtoks;
while ((numtoks = jsmn_parse(&j->parser, data, data_len,
j->tokens, j->num_tokens)) == JSMN_ERROR_NOMEM) {
int n = 2 * j->num_tokens;
jsmntok_t *toks = realloc(j->tokens, n * sizeof(jsmntok_t));
if (!toks) {
exchg_log("%s: OOM\n", __func__);
return -ENOMEM;
}
j->tokens = toks;
j->num_tokens = n;
}
if (numtoks == JSMN_ERROR_PART) {
if (j->buf_pos == 0)
return json_buf_add(j, in, len);
return 0;
}
*json = data;
*json_len = data_len;
if (unlikely(numtoks < 0))
return -EINVAL;
json_init(j);
return numtoks;
}
static void ws_on_error(void *p) {
struct websocket *w = p;
// TODO: websocket_log() and http_log() helpers
exchg_log("wss://%s%s error\n", w->host, w->path);
w->established = false;
w->disconnecting = true;
if (w->ops->on_disconnect)
w->ops->on_disconnect(w->cl, w, -1);
websocket_free(w);
}
static void ws_on_established(void *p) {
struct websocket *w = p;
exchg_log("wss://%s%s established\n", w->host, w->path);
w->established = true;
json_init(&w->json);
if (w->ops->on_conn_established)
w->ops->on_conn_established(w->cl, w);
}
static int ws_add_headers(void *p, struct websocket_conn *ws) {
struct websocket *w = p;
if (w->ops->add_headers)
return w->ops->add_headers(w->cl, w);
return 0;
}
static int ws_recv(void *p, char *in, size_t len) {
struct websocket *w = p;
char *json;
size_t json_len;
int numtoks = json_parse(&w->json, in, len, &json, &json_len);
if (numtoks < 0) {
if (numtoks == -EINVAL) {
exchg_log("%s%s sent data that doesn't parse as JSON:\n",
w->host, w->path);
put_response(json, json_len);
}
return -1;
}
if (numtoks == 0)
return 0;
// TODO: return code that says whether to try reconnecting or not
if (w->ops->recv(w->cl, w, json, numtoks, w->json.tokens)) {
w->disconnecting = true;
return -1;
}
return 0;
}
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
static const int _retry_seconds[] = {0, 1, 3, 10, 60, 300};
static int retry_seconds(struct retry *r) {
return _retry_seconds[r->seconds_idx];
}
static void time_since(struct timespec *dst, const struct timespec *ts) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
time_t carry = now.tv_nsec < ts->tv_nsec ? 1 : 0;
dst->tv_nsec = now.tv_nsec - ts->tv_nsec;
if (carry)
dst->tv_nsec += 1000000000;
dst->tv_sec = now.tv_sec - ts->tv_sec - carry;
}
static inline void retry_connected(struct retry *r) {
int last_idx = ARRAY_SIZE(_retry_seconds)-1;
struct timespec since_last_connected;
time_since(&since_last_connected, &r->last_connected);
if (since_last_connected.tv_sec > _retry_seconds[last_idx])
r->seconds_idx = 0;
else if (r->seconds_idx < last_idx)
r->seconds_idx++;
clock_gettime(CLOCK_MONOTONIC, &r->last_connected);
}
static inline int ws_retry_seconds(struct websocket *w) {
if (w->disconnecting)
return -1;
return retry_seconds(&w->retry);
}
static void ws_reconnect(struct websocket *w);
static void ws_retry_timer(void *p) {
struct websocket *w = p;
w->retry.timer = NULL;
ws_reconnect(w);
}
static void ws_add_retry_timer(struct websocket *w) {
w->retry.timer = timer_new(w->cl->ctx->net_context, ws_retry_timer, w,
retry_seconds(&w->retry));
}
void exchg_data_disconnect(struct exchg_client *cl, struct websocket *w,
int num_pairs_gone, enum exchg_pair *pairs_gone)
{
if (cl->ctx->callbacks.on_l2_disconnect)
cl->ctx->callbacks.on_l2_disconnect(cl, ws_retry_seconds(w),
num_pairs_gone, pairs_gone,
cl->ctx->user);
}
static void ws_reconnect(struct websocket *w) {
do {
retry_connected(&w->retry);
w->conn = ws_dial(w->cl->ctx->net_context, w->host, w->path, w);
if (w->conn)
return;
} while (retry_seconds(&w->retry) == 0);
ws_add_retry_timer(w);
}
static void ws_on_closed(void *p) {
struct websocket *w = p;
w->established = false;
w->conn = NULL;
if (w->ops->on_disconnect &&
w->ops->on_disconnect(w->cl, w, ws_retry_seconds(w)))
w->disconnecting = true;
if (!w->disconnecting) {
if (retry_seconds(&w->retry) == 0) {
exchg_log("wss://%s%s closed. Reconnecting now\n",
w->host, w->path);
ws_reconnect(w);
} else {
exchg_log("wss://%s%s closed. Reconnecting in %d second%s\n",
w->host, w->path, retry_seconds(&w->retry),
retry_seconds(&w->retry) > 1 ? "s" : "");
ws_add_retry_timer(w);
}
} else {
exchg_log("wss://%s%s closed\n", w->host, w->path);
websocket_free(w);
}
}
int http_add_header(struct http *h, const unsigned char *name,
const unsigned char *val, size_t len) {
return http_conn_add_header(h->conn, name, val, len);
}
int websocket_add_header(struct websocket *w, const unsigned char *name,
const unsigned char *val, size_t len) {
return ws_conn_add_header(w->conn, name, val, len);
}
static void http_reconnect(struct http *h);
static void http_retry_timer(void *p) {
struct http *h = p;
h->retry.timer = NULL;
http_reconnect(h);
}
static void http_add_retry_timer(struct http *h) {
h->retry.timer = timer_new(h->cl->ctx->net_context, http_retry_timer, h,
retry_seconds(&h->retry));
}
static void http_reconnect(struct http *h) {
do {
retry_connected(&h->retry);
h->conn = http_dial(h->cl->ctx->net_context, h->host,
h->path, h->method, h);
if (h->conn) {
if (h->body.len > 0)
http_conn_want_write(h->conn);
return;
}
} while (retry_seconds(&h->retry) == 0);
http_add_retry_timer(h);
}
void http_retry(struct http *h) {
h->want_retry = true;
}
static void http_on_error(void *p, const char *err) {
struct http *h = p;
if (h->ops->on_error)
h->ops->on_error(h->cl, h, err);
http_free(h);
}
static void http_on_established(void *p, int status) {
struct http *h = p;
if (status != 200)
exchg_log("https://%s%s established (status %d)\n",
h->host, h->path, status);
else
exchg_log("https://%s%s established\n",
h->host, h->path);
json_init(&h->json);
if (h->ops->on_established)
h->ops->on_established(h->cl, h, status);
}
static int http_add_headers(void *p, struct http_conn *req) {
struct http *h = p;
if (h->ops->add_headers)
return h->ops->add_headers(h->cl, h);
return 0;
}
int http_body_vsprintf(struct http *h, const char *fmt, va_list args) {
if (!h->body.buf && buf_alloc(&h->body, 200, _net_write_buf_padding))
return -1;
int len = buf_vsprintf(&h->body, fmt, args);
if (len > 0)
http_conn_want_write(h->conn);
return len;
}
int http_body_sprintf(struct http *h, const char *fmt, ...) {
va_list ap;
int len;
va_start(ap, fmt);
len = http_body_vsprintf(h, fmt, ap);
va_end(ap);
return len;
}
char *http_body(struct http *h) {
return buf_start(&h->body);
}
size_t http_body_len(struct http *h) {
return h->body.len;
}
void http_body_trunc(struct http *h, size_t len) {
if (len < h->body.len)
h->body.len = len;
}
static int http_recv(void *p, char *in, size_t len) {
struct http *h = p;
char *json;
size_t json_len;
int numtoks;
if (unlikely(h->print_data)) {
put_response(in, len);
return 0;
}
numtoks = json_parse(&h->json, in, len, &json, &json_len);
if (numtoks < 0) {
if (numtoks == -EINVAL) {
exchg_log("%s%s sent data that doesn't parse as JSON:\n",
h->host, h->path);
put_response(json, json_len);
h->print_data = true;
}
return -1;
}
if (numtoks == 0)
return 0;
return h->ops->recv(h->cl, h, http_conn_status(h->conn),
json, numtoks, h->json.tokens);
}
static void http_write(void *p, char **buf, size_t *len) {
struct http *h = p;
*buf = buf_start(&h->body);
*len = h->body.len;
}
static void http_on_closed(void *p) {
struct http *h = p;
exchg_log("https://%s%s closed\n", h->host, h->path);
if (h->ops->on_closed)
h->ops->on_closed(h->cl, h);
h->conn = NULL;
if (h->want_retry) {
if (retry_seconds(&h->retry) == 0) {
exchg_log("Retrying https://%s%s %s now.\n", h->host, h->path, h->method);
http_reconnect(h);
} else {
exchg_log("Retrying https://%s%s %s in %d seconds.\n", h->host, h->path,
h->method, retry_seconds(&h->retry));
http_add_retry_timer(h);
}
h->want_retry = false;
} else {
http_free(h);
}
}
static int json_alloc(struct json *json) {
json->tokens = malloc(sizeof(jsmntok_t) * 500);
if (!json->tokens)
return -1;
json->num_tokens = 500;
json_init(json);
return 0;
}
static struct http *exchg_http_dial(const char *host, const char *path,
const struct exchg_http_ops *ops,
struct exchg_client *cl, const char *method) {
struct http *h = malloc(sizeof(*h) + ops->conn_data_size);
if (!h) {
exchg_log("%s: OOM\n", __func__);
return NULL;
}
struct http_conn *conn = http_dial(cl->ctx->net_context, host,
path, method, h);
if (!conn) {
free(h);
return NULL;
}
cl->ctx->online = true;
memset(h, 0, sizeof(*h) + ops->conn_data_size);
retry_connected(&h->retry);
h->ops = ops;
h->conn = conn;
LIST_INSERT_HEAD(&cl->http_list, h, list);
h->cl = cl;
h->method = strdup(method);
if (!h->method)
goto oom;
h->host = strdup(host);
if (!h->host)
goto oom;
h->path = strdup(path);
if (!h->path)
goto oom;
if (json_alloc(&h->json))
goto oom;
return h;
oom:
// important to note that here conn was at least partially
// initialized even though it failed, so the eventual call to http_free()
// will not give nonsense. same goes for exchg_websocket_connect()
http_conn_close(h->conn);
return NULL;
}
struct http *exchg_http_get(const char *host, const char *path,
const struct exchg_http_ops *ops,
struct exchg_client *cl) {
return exchg_http_dial(host, path, ops, cl, "GET");
}
struct http *exchg_http_post(const char *host, const char *path,
const struct exchg_http_ops *ops,
struct exchg_client *cl) {
return exchg_http_dial(host, path, ops, cl, "POST");
}
struct http *exchg_http_delete(const char *host, const char *path,
const struct exchg_http_ops *ops,
struct exchg_client *cl) {
return exchg_http_dial(host, path, ops, cl, "DELETE");
}
struct websocket *exchg_websocket_connect(struct exchg_client *cl,
const char *host, const char *path,
const struct exchg_websocket_ops *ops) {
struct websocket *w = malloc(sizeof(*w) + ops->conn_data_size);
if (!w) {
exchg_log("%s: OOM\n", __func__);
return NULL;
}
struct websocket_conn *conn = ws_dial(cl->ctx->net_context, host, path, w);
if (!conn) {
free(w);
return NULL;
}
cl->ctx->online = true;
memset(w, 0, sizeof(*w) + ops->conn_data_size);
retry_connected(&w->retry);
w->ops = ops;
w->conn = conn;
LIST_INSERT_HEAD(&cl->websocket_list, w, list);
w->cl = cl;
w->host = strdup(host);
if (!w->host)
goto oom;
w->path = strdup(path);
if (!w->path)
goto oom;
if (json_alloc(&w->json))
goto oom;
return w;
oom:
ws_conn_close(w->conn);
return NULL;
}
int exchg_parse_info_on_established(struct exchg_client *cl,
struct http *h, int status) {
if (status != 200)
cl->get_info_error = -1;
return 0;
}
void exchg_parse_info_on_error(struct exchg_client *cl, struct http *h,
const char *err) {
cl->get_info_error = -1;
}
void exchg_parse_info_on_closed(struct exchg_client *cl, struct http *h) {
cl->getting_info = false;
}
const struct exchg_pair_info *exchg_pair_info(struct exchg_client *cl,
enum exchg_pair pair) {
if (!cl->pair_info_current)
return NULL;
return &cl->pair_info[pair];
}
bool exchg_pair_info_current(struct exchg_client *cl) {
return cl->pair_info_current;
}
int exchg_get_balances(struct exchg_client *cl, void *req_private) {
return cl->get_balances(cl, req_private);
}
void *websocket_private(struct websocket *w) {
return w->private;
}
void *http_private(struct http *h) {
return h->private;
}
struct order_info *__exchg_new_order(struct exchg_client *cl, const struct exchg_order *order,
const struct exchg_place_order_opts *opts,
void *req_private, size_t private_size, int64_t id) {
struct order_info *info = malloc(sizeof(*info) + private_size);
if (!info) {
exchg_log("%s: OOM\n", __func__);
return NULL;
}
info->req_private = req_private;
info->info.id = id;
memcpy(&info->info.order, order, sizeof(*order));
if (opts)
memcpy(&info->info.opts, opts, sizeof(info->info.opts));
else
memset(&info->info.opts, 0, sizeof(info->info.opts));
info->info.status = EXCHG_ORDER_UNSUBMITTED;
info->info.cancelation_failed = false;
memset(&info->info.filled_size, 0, sizeof(decimal_t));
memset(&info->info.avg_price, 0, sizeof(decimal_t));
info->info.err[0] = 0;
g_hash_table_insert(cl->orders, &info->info.id, info);
return info;
}
struct order_info *exchg_new_order(struct exchg_client *cl, const struct exchg_order *order,
const struct exchg_place_order_opts *opts, void *req_private,
size_t private_size) {
// TODO: current_micros() is fine for now but a collision is not absolutely
// out of the question. should fix that
return __exchg_new_order(cl, order, opts, req_private,
private_size, current_micros());
}
struct order_info *exchg_order_lookup(struct exchg_client *cl, int64_t id) {
return g_hash_table_lookup(cl->orders, &id);
}
void order_info_free(struct exchg_client *cl, struct order_info *info) {
g_hash_table_remove(cl->orders, &info->info.id);
}
// We get order updates possibly on several channels.
// So only give an order update if it seems that the new
// message has more recent info than what was has been gotten so far
void exchg_order_update(struct exchg_client *cl, struct order_info *oi,
enum exchg_order_status new_status, const decimal_t *new_size, bool cancel_failed) {
struct exchg_order_info *info = &oi->info;
bool update = false;
if (info->opts.immediate_or_cancel && new_status == EXCHG_ORDER_OPEN)
new_status = EXCHG_ORDER_PENDING;
switch (info->status) {
case EXCHG_ORDER_FINISHED:
case EXCHG_ORDER_CANCELED:
case EXCHG_ORDER_ERROR:
return;
case EXCHG_ORDER_UNSUBMITTED:
update |= new_status == EXCHG_ORDER_SUBMITTED;
case EXCHG_ORDER_SUBMITTED:
update |= new_status == EXCHG_ORDER_PENDING;
case EXCHG_ORDER_PENDING:
update |= new_status == EXCHG_ORDER_OPEN;
case EXCHG_ORDER_OPEN:
update |= new_status == EXCHG_ORDER_FINISHED ||
new_status == EXCHG_ORDER_CANCELED ||
new_status == EXCHG_ORDER_ERROR;
}
if (update)
info->status = new_status;
if (new_size && decimal_cmp(&info->filled_size, new_size) < 0) {
update = true;
info->filled_size = *new_size;
}
if (!info->cancelation_failed && cancel_failed) {
update = true;
info->cancelation_failed = true;
}
if (likely(update && cl->ctx->callbacks.on_order_update))
cl->ctx->callbacks.on_order_update(cl, info, cl->ctx->user, oi->private);
if (order_status_done(info->status))
order_info_free(cl, oi);
}
int64_t exchg_place_order(struct exchg_client *cl, const struct exchg_order *order,
const struct exchg_place_order_opts *opts, void *priv) {
if (unlikely(cl->ctx->opts.dry_run)) {
const char *action;
const char *atfor;
char sz[30], px[30];
decimal_to_str(sz, &order->size);
decimal_to_str(px, &order->price);
if (order->side == EXCHG_SIDE_BUY) {
action = "buy";
atfor = "for";
} else {
action = "sell";
atfor = "at";
}
printf("%s %s %s %s %s %s\n", cl->name, action,
sz, exchg_pair_to_str(order->pair), atfor, px);
return 0;
}
return cl->place_order(cl, order, opts, priv);
}
int exchg_cancel_order(struct exchg_client *cl, int64_t id) {
if (unlikely(cl->ctx->opts.dry_run)) {
// kind of tough, but what should dry run do? remember
// the orders placed and remove them here? maybe not worth it...
printf("CANCEL order\n");
return 0;
}
struct order_info *info = exchg_order_lookup(cl, id);
if (unlikely(!info)) {
exchg_log("Can't cancel %s order %"PRId64". ID not recognized\n",
cl->name, id);
return -1;
}
return cl->cancel_order(cl, info);
}
int exchg_realloc_order_bufs(struct exchg_client *cl, int n) {
struct exchg_limit_order *bids, *asks;
bids = realloc(cl->update.bids, n * sizeof(struct exchg_limit_order));
if (!bids) {
exchg_log("%s(%d): OOM\n", __func__, n);
return -1;
}
cl->update.bids = bids;
asks = realloc(cl->update.asks, n * sizeof(struct exchg_limit_order));
if (!asks) {
exchg_log("%s(%d): OOM\n", __func__, n);
return -1;
}
cl->update.asks = asks;
cl->l2_update_size = n;
return 0;
}
enum exchg_id exchg_id(struct exchg_client *cl) {
return cl->id;
}
const char *exchg_name(struct exchg_client *cl) {
return cl->name;
}
struct exchg_client *alloc_exchg_client(struct exchg_context *ctx,
enum exchg_id id, int l2_update_size, size_t private_size) {
if (ctx->clients[id]) {
exchg_log("%s client already allocated\n", exchg_id_to_name(id));
return NULL;
}
struct exchg_client *ret = malloc(sizeof(*ret) + private_size);
if (!ret) {
fprintf(stderr, "%s OOM\n", __func__);
return NULL;
}
memset(ret, 0, sizeof(*ret) + private_size);
ret->hmac_ctx = HMAC_CTX_new();
if (!ret->hmac_ctx) {
exchg_log("OOM\n");
free(ret);
return NULL;
}
ret->id = id;
ret->ctx = ctx;
// TODO: only really need this many for the first update. Should shrink the buffers after that.
ret->l2_update_size = l2_update_size;
ret->update.exchange_id = id;
ret->update.bids = malloc(l2_update_size * sizeof(struct exchg_limit_order));
ret->update.asks = malloc(l2_update_size * sizeof(struct exchg_limit_order));
if (!ret->update.bids || !ret->update.asks) {
exchg_log("%s: OOM\n", __func__);
free(ret->update.bids);
free(ret->update.asks);
HMAC_CTX_free(ret->hmac_ctx);
free(ret);
return NULL;
}
ret->orders = g_hash_table_new_full(g_int64_hash, g_int64_equal,
NULL, free);
LIST_INIT(&ret->websocket_list);
LIST_INIT(&ret->http_list);
LIST_INIT(&ret->work);
ctx->clients[id] = ret;
return ret;
}
void free_exchg_client(struct exchg_client *cl) {
struct work *w, *tmp_w;
struct websocket *ws, *tmp_ws;
struct http *h, *tmp_h;
cl->ctx->clients[cl->id] = NULL;
HMAC_CTX_free(cl->hmac_ctx);
LIST_FOREACH_SAFE(ws, &cl->websocket_list, list, tmp_ws) {
LIST_REMOVE(ws, list);
__websocket_free(ws);
}
LIST_FOREACH_SAFE(h, &cl->http_list, list, tmp_h) {
LIST_REMOVE(h, list);
__http_free(h);
}
LIST_FOREACH_SAFE(w, &cl->work, list, tmp_w) {
LIST_REMOVE(w, list);
free(w);
}
free(cl->apikey_public);
OPENSSL_cleanse(cl->password, cl->password_len);
free(cl->password);
free(cl->update.bids);
free(cl->update.asks);
free(cl);
}
int exchg_num_bids(struct exchg_context *ctx, enum exchg_pair pair) {
struct order_book *book = ctx->books[pair];
if (!book)
return 0;
return order_book_num_bids(book);
}
int exchg_num_asks(struct exchg_context *ctx, enum exchg_pair pair) {