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_server.c 5.7 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
  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 <netinet/in.h>
  29. #include <pthread.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <unistd.h>
  36. #include <libcore/string.h>
  37. #include "fix.h"
  38. #include "fix_server.h"
  39. #include "fix_session.h"
  40. #include "fix_session_manager.h"
  41. #define DEBUG 0
  42. #define DBG(...) \
  43. do { if(DEBUG) fprintf(stderr, __VA_ARGS__); } while(0)
  44. #define BUFSZ 256
  45. static pthread_t server_thread;
  46. static String *fix_server_id = NULL;
  47. static int server_socket = -1;
  48. static int server_done = 0;
  49. static void fix_server_read_logon(int socket)
  50. {
  51. unsigned long msg_start_idx, msg_end_idx;
  52. String *buffer, *fix_msg;
  53. FixSession *session;
  54. char buf[BUFSZ];
  55. ssize_t n;
  56. int done;
  57. session = NULL;
  58. done = 0;
  59. buffer = string_create();
  60. fix_msg = NULL;
  61. while(!done) {
  62. DBG("Server waiting for data from client\n");
  63. /* Read data onto the end of buffer */
  64. if((n = recv(socket, buf, BUFSZ, 0)) > 0) {
  65. string_append_buf(buffer, buf, n);
  66. /* Find BeginString tag and CheckSum tag */
  67. if((string_find(buffer, "8=", &msg_start_idx) == 0) &&
  68. (string_find(buffer, "\00110=", &msg_end_idx) == 0)) {
  69. /* Length of "<SOH>10=xxx<SOH>" is 8 characters */
  70. if((string_length(buffer) - msg_end_idx) >= 8) {
  71. /* Extract FIX message. Index of final SOH is
  72. * msg_end_idx + 7.
  73. */
  74. fix_msg = string_substring(buffer, msg_start_idx,
  75. (msg_end_idx + 7));
  76. DBG("New msg: '%s'\n", string_get_chars(fix_msg));
  77. /* Pass message to session object */
  78. if(NULL == session) {
  79. if(fix_session_manager_lookup_session(fix_msg,
  80. &session) < 0) {
  81. done = 1;
  82. } else {
  83. if(NULL == session) {
  84. done = 1;
  85. } else {
  86. if(!fix_session_is_active(session)) {
  87. fix_session_set_socket(session, socket);
  88. fix_session_activate(session);
  89. fix_session_receive_message(session, fix_msg);
  90. }
  91. done = 1;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. } else {
  98. /* Assume client disconnected.
  99. *
  100. * TODO Check more thoroughly the reason for
  101. * recv failure
  102. */
  103. done = 1;
  104. }
  105. }
  106. /* Free the buffer string since there should
  107. * be no other valid data in it. When a client
  108. * connects, the only thing they should send
  109. * us is a logon message.
  110. */
  111. string_free(buffer);
  112. DBG("Server is done waiting for data from client\n");
  113. }
  114. static void* _fix_server(void *data)
  115. {
  116. struct sockaddr_in addr;
  117. int c;
  118. server_done = 0;
  119. server_socket = socket(AF_INET, SOCK_STREAM, 0);
  120. memset(&addr, 0, sizeof(addr));
  121. addr.sin_family = AF_INET;
  122. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  123. addr.sin_port = htons(FIX_SERVER_PORT);
  124. bind(server_socket, (struct sockaddr *)&addr, sizeof(addr));
  125. /* Wait for clients to connect */
  126. listen(server_socket, SOMAXCONN);
  127. while(!server_done) {
  128. c = accept(server_socket, NULL, NULL);
  129. DBG("New client\n");
  130. fix_server_read_logon(c);
  131. }
  132. return NULL;
  133. }
  134. void fix_server_init(void)
  135. {
  136. printf("FIX Server init\n");
  137. fix_server_id = string_create_from_buf(FIX_SERVER_ID,
  138. strlen(FIX_SERVER_ID));
  139. pthread_create(&server_thread, NULL, _fix_server, NULL);
  140. }
  141. void fix_server_destroy(void)
  142. {
  143. printf("FIX Server destroy\n");
  144. server_done = 1;
  145. shutdown(server_socket, SHUT_RDWR);
  146. close(server_socket);
  147. pthread_join(server_thread, NULL);
  148. string_free(fix_server_id);
  149. }
  150. const String* fix_server_get_id(void)
  151. {
  152. return fix_server_id;
  153. }
Tip!

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

Comments

Loading...