Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

fix_session.c 14 KB

You have to be logged in to leave a comment. Sign In
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
  1. /* Copyright (c) 2012, Chris Winter <wintercni@gmail.com>
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the copyright holder nor the
  13. * names of contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <sys/socket.h>
  31. #include <pthread.h>
  32. #include <unistd.h>
  33. #include <libcore/string.h>
  34. #include <libcore/queue.h>
  35. #include "fix_session.h"
  36. #include "fix_parser.h"
  37. #include "fix_server.h"
  38. #include "order.h"
  39. #include "market.h"
  40. #define DEBUG 0
  41. #define DBG(...) \
  42. do { if(DEBUG) fprintf(stderr, __VA_ARGS__); } while(0)
  43. struct _fix_session {
  44. String *SenderCompId;
  45. int socket;
  46. int is_active;
  47. Queue *rx_queue;
  48. Queue *tx_queue;
  49. pthread_t socket_thread;
  50. pthread_t rx_thread;
  51. pthread_t tx_thread;
  52. pthread_mutex_t mutex;
  53. pthread_cond_t rx_cond;
  54. pthread_cond_t tx_cond;
  55. unsigned long rx_seq_num;
  56. unsigned long tx_seq_num;
  57. };
  58. static void _fix_session_message_process(FixSession *session, String *msg)
  59. {
  60. Order *o;
  61. DBG("Processing message: '%s'\n", string_get_chars(msg));
  62. if(fix_parse_is_msg_valid(msg)) {
  63. /* Validate the RX sequence number */
  64. if(session->rx_seq_num != fix_parse_MsgSeqNum(msg)) {
  65. fprintf(stderr, "Sequence number doesn't match: expect %lu got %lu\n",
  66. session->rx_seq_num, fix_parse_MsgSeqNum(msg));
  67. /* TODO Should do a Sequence Reset-Gap Fill/Reset here */
  68. fix_session_deactivate(session);
  69. return;
  70. }
  71. /* Increment RX sequence number */
  72. session->rx_seq_num++;
  73. switch(fix_parse_MsgType(msg)) {
  74. /* Session Messages */
  75. case FIX_MSG_TYPE_LOGON:
  76. DBG("Received logon message\n");
  77. fix_session_send_message(session, FIX_MSG_TYPE_LOGON,
  78. fix_message_generate_logon(FIX_ENCRYPT_METHOD_NONE, 0));
  79. break;
  80. case FIX_MSG_TYPE_LOGOUT:
  81. DBG("Received logout message\n");
  82. fix_session_send_message(session, FIX_MSG_TYPE_LOGOUT, NULL);
  83. break;
  84. /* TODO Other session-level messages here */
  85. /* Administrative and Application Messages */
  86. case FIX_MSG_TYPE_NEW_ORDER_SINGLE:
  87. DBG("Parsing new order\n");
  88. o = fix_parse_order(msg);
  89. if(NULL != o) {
  90. DBG("Sending order into the market\n");
  91. /* Send the order into the market */
  92. market_process_order(o);
  93. }
  94. break;
  95. default:
  96. fprintf(stderr, "Received unsupported message\n");
  97. fix_session_deactivate(session);
  98. break;
  99. }
  100. } else {
  101. fprintf(stderr, "Received invalid message\n");
  102. fix_session_deactivate(session);
  103. }
  104. }
  105. static void _fix_session_message_send(FixSession *session, String *msg)
  106. {
  107. DBG("Sending message: '%s'\n", string_get_chars(msg));
  108. send(session->socket, string_get_chars(msg), string_length(msg), 0);
  109. }
  110. #define BUFSZ 256
  111. static void* _fix_session_socket_thread(void *data)
  112. {
  113. unsigned long msg_start_idx, msg_end_idx;
  114. FixSession *session = (FixSession *)data;
  115. String *buffer, *fix_msg;
  116. char buf[BUFSZ];
  117. ssize_t n;
  118. if(NULL == session) {
  119. /* TODO Proper error log message */
  120. fprintf(stderr, "Valid session object not passed to _fix_session_thread\n");
  121. return NULL;
  122. }
  123. if(fix_session_get_socket(session) < 0) {
  124. fprintf(stderr, "Session has invalid socket\n");
  125. return NULL;
  126. }
  127. buffer = string_create();
  128. fix_msg = NULL;
  129. while(fix_session_is_active(session)) {
  130. if((n = recv(fix_session_get_socket(session), buf, BUFSZ, 0)) > 0) {
  131. string_append_buf(buffer, buf, n);
  132. /* Find BeginString tag and CheckSum tag */
  133. if((string_find(buffer, "8=", &msg_start_idx) == 0) &&
  134. (string_find(buffer, "\00110=", &msg_end_idx) == 0)) {
  135. /* Length of "<SOH>10=xxx<SOH>" is 8 characters */
  136. if((string_length(buffer) - msg_end_idx) >= 8) {
  137. /* Extract FIX message. Index of final SOH is
  138. * msg_end_idx + 7.
  139. */
  140. fix_msg = string_remove_substring(buffer, msg_start_idx,
  141. (msg_end_idx + 7));
  142. DBG("New msg: '%s'\n", string_get_chars(fix_msg));
  143. fix_session_receive_message(session, fix_msg);
  144. /* Clear out buffer for new message */
  145. if(msg_start_idx > 0) {
  146. string_delete_substring(buffer, 0, msg_start_idx - 1);
  147. }
  148. }
  149. }
  150. } else {
  151. /* Assume client disconnected.
  152. *
  153. * TODO Check more thoroughly the reason for
  154. * recv failure
  155. */
  156. DBG("Client disconnected\n");
  157. fix_session_deactivate(session);
  158. }
  159. }
  160. DBG("socket thread exiting\n");
  161. return NULL;
  162. }
  163. void* _fix_session_rx_thread(void *data)
  164. {
  165. FixSession *session = (FixSession *)data;
  166. String *msg;
  167. if(NULL == session) {
  168. /* TODO Proper error log message */
  169. fprintf(stderr, "Invalid session object passed to _fix_session_tx_thread\n");
  170. return NULL;
  171. }
  172. pthread_mutex_lock(&session->mutex);
  173. while(session->is_active) {
  174. while(!queue_is_empty(session->rx_queue)) {
  175. msg = (String *)queue_dequeue(session->rx_queue);
  176. _fix_session_message_process(session, msg);
  177. string_free(msg);
  178. }
  179. pthread_cond_wait(&session->rx_cond, &session->mutex);
  180. }
  181. pthread_mutex_unlock(&session->mutex);
  182. DBG("Rx Thread: Exiting\n");
  183. return NULL;
  184. }
  185. void* _fix_session_tx_thread(void *data)
  186. {
  187. FixSession *session = (FixSession *)data;
  188. String *msg;
  189. if(NULL == session) {
  190. /* TODO Proper error log message */
  191. fprintf(stderr, "Invalid session object passed to _fix_session_rx_thread\n");
  192. return NULL;
  193. }
  194. pthread_mutex_lock(&session->mutex);
  195. while(session->is_active) {
  196. while(!queue_is_empty(session->tx_queue)) {
  197. msg = (String *)queue_dequeue(session->tx_queue);
  198. _fix_session_message_send(session, msg);
  199. string_free(msg);
  200. }
  201. pthread_cond_wait(&session->tx_cond, &session->mutex);
  202. }
  203. pthread_mutex_unlock(&session->mutex);
  204. DBG("Tx Thread: Exiting\n");
  205. return NULL;
  206. }
  207. FixSession* fix_session_create(String *SenderCompId,
  208. unsigned long client_seq_start)
  209. {
  210. FixSession *session;
  211. pthread_mutexattr_t mutex_attr;
  212. assert(SenderCompId != NULL);
  213. session = malloc(sizeof(FixSession));
  214. if(NULL == session) {
  215. /* TODO Log and error */
  216. return NULL;
  217. }
  218. session->SenderCompId = SenderCompId;
  219. session->socket = -1;
  220. session->is_active = 0;
  221. session->rx_queue = queue_create();
  222. session->tx_queue = queue_create();
  223. pthread_mutexattr_init(&mutex_attr);
  224. pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
  225. pthread_mutex_init(&session->mutex, &mutex_attr);
  226. pthread_mutexattr_destroy(&mutex_attr);
  227. pthread_cond_init(&session->rx_cond, NULL);
  228. pthread_cond_init(&session->tx_cond, NULL);
  229. session->rx_seq_num = 1;
  230. session->tx_seq_num = 1;
  231. return session;
  232. }
  233. void fix_session_free(FixSession *session)
  234. {
  235. assert(session != NULL);
  236. fix_session_deactivate(session);
  237. pthread_cond_destroy(&session->rx_cond);
  238. pthread_cond_destroy(&session->tx_cond);
  239. pthread_mutex_destroy(&session->mutex);
  240. string_free(session->SenderCompId);
  241. queue_free_all(session->rx_queue, (FreeFn)string_free);
  242. queue_free_all(session->tx_queue, (FreeFn)string_free);
  243. free(session);
  244. }
  245. int fix_session_set_socket(FixSession *session, int socket)
  246. {
  247. assert(session != NULL);
  248. assert(socket >= 0);
  249. pthread_mutex_lock(&session->mutex);
  250. session->socket = socket;
  251. pthread_mutex_unlock(&session->mutex);
  252. return 0;
  253. }
  254. int fix_session_activate(FixSession *session)
  255. {
  256. assert(session != NULL);
  257. pthread_mutex_lock(&session->mutex);
  258. if(!session->is_active) {
  259. session->is_active = 1;
  260. printf("FIX Session: Activating session for '%s'\n",
  261. string_get_chars(session->SenderCompId));
  262. if(pthread_create(&session->socket_thread, NULL,
  263. &_fix_session_socket_thread, (void *)session) != 0) {
  264. session->is_active = 0;
  265. pthread_mutex_unlock(&session->mutex);
  266. return -1;
  267. }
  268. if(pthread_create(&session->tx_thread, NULL,
  269. &_fix_session_tx_thread, (void *)session) != 0) {
  270. /* FIXME Need to kill socket thread */
  271. session->is_active = 0;
  272. pthread_mutex_unlock(&session->mutex);
  273. return -1;
  274. }
  275. if(pthread_create(&session->rx_thread, NULL,
  276. &_fix_session_rx_thread, (void *)session) != 0) {
  277. /* FIXME Need to kill socket and rx threads */
  278. session->is_active = 0;
  279. pthread_mutex_unlock(&session->mutex);
  280. return -1;
  281. }
  282. }
  283. pthread_mutex_unlock(&session->mutex);
  284. return 0;
  285. }
  286. int fix_session_deactivate(FixSession *session)
  287. {
  288. assert(session != NULL);
  289. pthread_mutex_lock(&session->mutex);
  290. if(session->is_active) {
  291. session->is_active = 0;
  292. printf("FIX Session: Deactivating session for '%s'\n",
  293. string_get_chars(session->SenderCompId));
  294. pthread_cond_signal(&session->tx_cond);
  295. pthread_cond_signal(&session->rx_cond);
  296. pthread_mutex_unlock(&session->mutex);
  297. if(pthread_equal(pthread_self(), session->rx_thread) == 0) {
  298. pthread_join(session->rx_thread, NULL);
  299. }
  300. if(pthread_equal(pthread_self(), session->tx_thread) == 0) {
  301. pthread_join(session->tx_thread, NULL);
  302. }
  303. shutdown(session->socket, SHUT_RDWR);
  304. close(session->socket);
  305. session->socket = -1;
  306. if(pthread_equal(pthread_self(), session->socket_thread) == 0) {
  307. pthread_join(session->socket_thread, NULL);
  308. }
  309. } else {
  310. pthread_mutex_unlock(&session->mutex);
  311. }
  312. return 0;
  313. }
  314. int fix_session_receive_message(FixSession *session, String *message)
  315. {
  316. if((NULL == session) ||
  317. (NULL == message) ||
  318. string_is_empty(message)) {
  319. return -1;
  320. }
  321. pthread_mutex_lock(&session->mutex);
  322. queue_enqueue(session->rx_queue, message);
  323. pthread_cond_signal(&session->rx_cond);
  324. pthread_mutex_unlock(&session->mutex);
  325. return 0;
  326. }
  327. int fix_session_send_message(FixSession *session,
  328. FIX_MSG_TYPE type, String *payload)
  329. {
  330. String *header_and_payload, *trailer;
  331. String *fix_msg, *header;
  332. DBG("Sending message\n");
  333. if((NULL == session) ||
  334. (type >= FIX_MSG_TYPE_LAST)) {
  335. return -1;
  336. }
  337. fix_msg = NULL;
  338. pthread_mutex_lock(&session->mutex);
  339. if((NULL == payload) || (string_length(payload) == 0)) {
  340. header = fix_message_generate_header(type,
  341. 0, /* payload length */
  342. fix_server_get_id(),
  343. session->SenderCompId,
  344. session->tx_seq_num++);
  345. header_and_payload = string_duplicate(header);
  346. } else {
  347. header = fix_message_generate_header(type,
  348. string_length(payload),
  349. fix_server_get_id(),
  350. session->SenderCompId,
  351. session->tx_seq_num++);
  352. header_and_payload = string_concat(header, payload);
  353. }
  354. trailer = fix_message_generate_trailer(header_and_payload);
  355. fix_msg = string_concat(header_and_payload, trailer);
  356. queue_enqueue(session->tx_queue, fix_msg);
  357. pthread_cond_signal(&session->tx_cond);
  358. pthread_mutex_unlock(&session->mutex);
  359. string_free(header);
  360. string_free(payload);
  361. string_free(header_and_payload);
  362. string_free(trailer);
  363. return 0;
  364. }
  365. const String* fix_session_get_SenderCompId(FixSession *session)
  366. {
  367. String *ret;
  368. assert(session != NULL);
  369. pthread_mutex_lock(&session->mutex);
  370. ret = session->SenderCompId;
  371. pthread_mutex_unlock(&session->mutex);
  372. return ret;
  373. }
  374. int fix_session_is_active(FixSession *session)
  375. {
  376. int ret;
  377. assert(session != NULL);
  378. pthread_mutex_lock(&session->mutex);
  379. ret = session->is_active;
  380. pthread_mutex_unlock(&session->mutex);
  381. return ret;
  382. }
  383. int fix_session_get_socket(FixSession *session)
  384. {
  385. int ret;
  386. assert(session != NULL);
  387. pthread_mutex_lock(&session->mutex);
  388. ret = session->socket;
  389. pthread_mutex_unlock(&session->mutex);
  390. return ret;
  391. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...