-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_builder.h
251 lines (238 loc) · 7.15 KB
/
fix_builder.h
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
#pragma once
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <iostream>
#include <string_view>
#include "fix.h"
#include "fix_alloc.h"
#include "fixed.h"
class FixBuilder {
private:
const int maxMessageSize;
FixBuffer buffer;
// used for converting int to string
char tmp[32];
char tmpTime[22];
char *message;
char *cp;
char *itoa(int64_t val) { return itoa(val, tmp, sizeof(tmp)); }
// encode int to chars without ending null
static char *itoa(int64_t val, char *tmp, int len) {
bool neg = val < 0;
if (neg) {
val = val * -1;
}
int i = len - 1;
for (; val >= 10;) {
tmp[i] = val % 10 + '0';
i--;
val /= 10;
}
tmp[i] = val + '0';
if (neg) {
i--;
tmp[i] = '-';
}
return tmp + i;
}
char *bodyLenStart;
char *bodyLenEnd;
int bodyLength = 0;
void addCheckSum() {
unsigned int checksum = 0;
for (auto cpp = message; cpp != cp; cpp++) {
checksum += (unsigned int)*cpp;
}
memcpy(cp, "10=000\x01", 7);
itoa(checksum % 256, cp + 3, 3);
cp += 7;
}
// encode time as FIX UTC with millis. dst must be at least 22 bytes.
void encodeTime(timeval &time, char *dst) {
struct tm gmTime;
time_t secs = time.tv_sec;
gmtime_r(&secs, &gmTime);
memcpy(dst, "YYYY0000-00:00:00.000\x01", 22);
itoa(gmTime.tm_year + 1900, dst, 4);
dst += 4;
itoa(gmTime.tm_mon + 1, dst, 2);
dst += 2;
itoa(gmTime.tm_mday, dst, 2);
dst += 3;
itoa(gmTime.tm_hour, dst, 2);
dst += 3;
itoa(gmTime.tm_min, dst, 2);
dst += 3;
itoa(gmTime.tm_sec, dst, 2);
dst += 3;
itoa(time.tv_usec / 1000, dst, 3);
dst += 4;
}
public:
FixBuilder() : FixBuilder(8192) {}
FixBuilder(int maxMessageSize)
: maxMessageSize(maxMessageSize), buffer(maxMessageSize * 2) {
reset();
}
inline void addField(uint32_t tag, const std::string_view &value) {
bool isBodyLen = tag == Tag::BODY_LENGTH;
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
if (isBodyLen)
bodyLenStart = cp;
memcpy(cp, value.data(), value.size());
cp += value.size();
if (isBodyLen)
bodyLenEnd = cp;
*cp++ = '\x01';
if (!isBodyLen && bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
inline void addField(uint32_t tag, const int value) {
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
start = itoa(value);
len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '\x01';
if (bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
inline void addField(uint32_t tag, const char value) {
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
len = 1;
*cp++ = value;
*cp++ = '\x01';
if (bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
inline void addField(uint32_t tag, const long value) {
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
start = itoa(value);
len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '\x01';
if (bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
template <int nPlaces = 7>
inline void addField(uint32_t tag, Fixed<nPlaces> fixed) {
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
fixed.str(cp);
len = strlen(cp);
cp += len;
*cp++ = '\x01';
if (bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
// only a single cached time per message is supported
inline const std::string_view cacheTime(timeval &time) {
encodeTime(time, tmpTime);
return std::string_view(tmpTime, 21);
}
inline void addTimeNow(uint32_t tag) {
struct timeval time;
gettimeofday(&time, nullptr);
addTime(tag, time);
}
inline void addTime(uint32_t tag, timeval &time) {
char *cpBegin = cp;
auto start = itoa(tag);
auto len = sizeof(tmp) - (start - tmp);
memcpy(cp, start, len);
cp += len;
*cp++ = '=';
encodeTime(time, cp);
// include the SOH character
cp += 22;
if (bodyLenStart != nullptr) {
bodyLength += (cp - cpBegin);
}
}
// Add fields from the src FixBuilder to this, and reset the src. The src should not contain the standard header fields.
void addBuilder(FixBuilder &src) {
int len = src.cp - src.message;
memcpy(cp, src.message, len);
cp += len;
if (bodyLenStart != nullptr) bodyLength += len;
src.reset();
}
// write message to fd and reset
void writeTo(int fd) {
if (bodyLenStart) {
// write the body length into the already reserved space, must use
// addTag(9,"00000"); to reserve
itoa(bodyLength, bodyLenStart, bodyLenEnd - bodyLenStart);
}
addCheckSum();
int len = cp - message;
if (write(fd, message, len) != len) {
throw std::runtime_error("unable to write message on fd");
}
reset();
}
// write message to ostream and reset
void writeTo(std::ostream& out) {
if (bodyLenStart) {
// write the body length into the already reserved space, must use
// addTag(9,"00000"); to reserve
itoa(bodyLength, bodyLenStart, bodyLenEnd - bodyLenStart);
}
addCheckSum();
int len = cp - message;
out.write(message, len);
if (out.fail()) {
throw std::runtime_error("unable to write message on fd");
}
reset();
}
const std::string_view messageView() {
if (bodyLenStart) {
// write the body length into the already reserved space, must use
// addTag(9,"00000"); to reserve
itoa(bodyLength, bodyLenStart, bodyLenEnd - bodyLenStart);
}
addCheckSum();
return std::string_view(message, cp);
}
// resets internals for next message
void reset() {
buffer.reset();
message = (char *)buffer.allocate(maxMessageSize);
cp = message;
bodyLenStart = nullptr;
bodyLenEnd = nullptr;
bodyLength = 0;
}
};