-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
binance-ss.c
221 lines (174 loc) · 5 KB
/
binance-ss.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
/*
* pico-sspc-binance
*
* Written in 2010-2021 by Andy Green <[email protected]>
* Kutoga <[email protected]>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
*
* This is a version of minimal-secure-streams-binance that uses a custom
* SS Serialization transport.
*/
#include <libwebsockets.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
extern uint64_t
get_us_timeofday(void);
typedef struct range {
uint64_t sum;
uint64_t lowest;
uint64_t highest;
unsigned int samples;
} range_t;
LWS_SS_USER_TYPEDEF
uint64_t data_in;
uint64_t data_in_last_sec;
lws_sorted_usec_list_t sul_hz; /* 1hz summary dump */
char msgbuf[10240];
size_t msg_len;
range_t e_lat_range;
range_t price_range;
} binance_t;
/*
* Rest of the file is Binance application SS processing (UNCHANGED from
* minimal-secure-streams-binance)
*/
static void
range_reset(range_t *r)
{
r->sum = r->highest = 0;
r->lowest = 999999999999ull;
r->samples = 0;
}
static uint64_t
pennies(const char *s)
{
uint64_t price = (uint64_t)atoll(s) * 100;
s = strchr(s, '.');
if (s && isdigit(s[1]) && isdigit(s[2]))
price = price + (uint64_t)((10 * (s[1] - '0')) + (s[2] - '0'));
return price;
}
static void
sul_hz_cb(lws_sorted_usec_list_t *sul)
{
binance_t *bin = lws_container_of(sul, binance_t, sul_hz);
/*
* We are called once a second to dump statistics on the connection
*/
lws_sul_schedule(lws_ss_get_context(bin->ss), 0, &bin->sul_hz,
sul_hz_cb, LWS_US_PER_SEC);
if (bin->price_range.samples)
lwsl_user("%s: price: min: %llu¢, max: %llu¢, avg: %llu¢, "
"(%d prices/s)\n", __func__,
(unsigned long long)bin->price_range.lowest,
(unsigned long long)bin->price_range.highest,
(unsigned long long)(bin->price_range.sum /
bin->price_range.samples),
bin->price_range.samples);
if (bin->e_lat_range.samples)
lwsl_user("%s: elatency: min: %lums, max: %lums, "
"avg: %lums, (%d msg/s, %lu KiBytes/s SS RX)\n",
__func__,
(unsigned long)(bin->e_lat_range.lowest / 1000),
(unsigned long)(bin->e_lat_range.highest / 1000),
(unsigned long)((bin->e_lat_range.sum /
bin->e_lat_range.samples) / 1000),
bin->e_lat_range.samples,
(unsigned long)((bin->data_in -
bin->data_in_last_sec) / 1024));
range_reset(&bin->e_lat_range);
range_reset(&bin->price_range);
bin->data_in_last_sec = bin->data_in;
}
static lws_ss_state_return_t
binance_rx(void *userobj, const uint8_t *in, size_t len, int flags)
{
binance_t *bin = (binance_t *)userobj;
uint64_t latency_us, now_us, l1;
const uint8_t *msg;
char numbuf[20];
uint64_t price;
const char *p;
size_t alen;
bin->data_in += len;
msg = bin->msgbuf;
if (flags & LWSSS_FLAG_SOM) {
bin->msg_len = 0;
if (flags & LWSSS_FLAG_EOM) {
msg = in;
bin->msg_len = len;
goto handle;
}
}
if (bin->msg_len + len < sizeof(bin->msgbuf)) {
memcpy(bin->msgbuf + bin->msg_len, in, len);
bin->msg_len += len;
}
/* assemble a full message */
if (!(flags & LWSSS_FLAG_EOM))
return LWSSSSRET_OK;
handle:
//lwsl_notice("%s: chunk len %d\n", __func__, (int)len);
now_us = (uint64_t)get_us_timeofday();
//lwsl_notice("%s: now_us adjusted %llu\n", __func__, now_us);
p = lws_json_simple_find(msg, bin->msg_len, "\"depthUpdate\"",
&alen);
if (!p)
return LWSSSSRET_OK;
p = lws_json_simple_find(msg, bin->msg_len, "\"E\":", &alen);
if (!p) {
lwsl_err("%s: no E JSON\n", __func__);
return LWSSSSRET_OK;
}
lws_strnncpy(numbuf, p, alen, sizeof(numbuf));
l1 = ((uint64_t)atoll(numbuf) * LWS_US_PER_MS);
latency_us = now_us - l1;
if (latency_us < bin->e_lat_range.lowest)
bin->e_lat_range.lowest = latency_us;
if (latency_us > bin->e_lat_range.highest)
bin->e_lat_range.highest = latency_us;
bin->e_lat_range.sum += latency_us;
bin->e_lat_range.samples++;
p = lws_json_simple_find(msg, bin->msg_len, "\"a\":[[\"", &alen);
if (!p)
return LWSSSSRET_OK;
lws_strnncpy(numbuf, p, alen, sizeof(numbuf));
price = pennies(numbuf);
if (price < bin->price_range.lowest)
bin->price_range.lowest = price;
if (price > bin->price_range.highest)
bin->price_range.highest = price;
bin->price_range.sum += price;
bin->price_range.samples++;
return LWSSSSRET_OK;
}
static lws_ss_state_return_t
binance_state(void *userobj, void *h_src, lws_ss_constate_t state,
lws_ss_tx_ordinal_t ack)
{
binance_t *bin = (binance_t *)userobj;
lwsl_ss_info(bin->ss, "%s (%d), ord 0x%x",
lws_ss_state_name((int)state), state, (unsigned int)ack);
switch (state) {
case LWSSSCS_CONNECTED:
lws_sul_schedule(lws_ss_get_context(bin->ss), 0, &bin->sul_hz,
sul_hz_cb, LWS_US_PER_SEC);
range_reset(&bin->e_lat_range);
range_reset(&bin->price_range);
return LWSSSSRET_OK;
case LWSSSCS_DISCONNECTED:
lws_sul_cancel(&bin->sul_hz);
break;
default:
break;
}
return LWSSSSRET_OK;
}
LWS_SS_INFO("binance", binance_t)
.rx = binance_rx,
.state = binance_state,
};