forked from ideawu/libfast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
274 lines (229 loc) · 5.87 KB
/
test.cpp
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
#include <inttypes.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <string>
#include <map>
static const unsigned char SIGN_BIT = (unsigned char)('\x40');
static const unsigned char STOP_BIT = (unsigned char)('\x80');
static const unsigned char DATA_BITS = (unsigned char)('\x7F');
static const unsigned char EMPTY_STRING = (unsigned char)('\x80');
static std::string encode_num(int64_t val){
std::string buffer;
int64_t until = 0LL;
unsigned char sign = 0;
if(val < 0){
until = -1LL;
sign = SIGN_BIT;
}
// force at least one byte to be stored
unsigned char prevByte = ~sign;
unsigned char byte = 0x80;
while(val != until || (prevByte & SIGN_BIT) != sign){
byte |= (unsigned char)(val & DATA_BITS);
val >>= 7;
buffer.push_back(byte);
prevByte = byte;
byte = 0;
}
return buffer;
}
static std::string encode_num(uint64_t val){
std::string buffer;
uint64_t until = 0ULL;
unsigned char byte = 0x80;
while(val != until || byte != 0){
byte |= (unsigned char)(val & DATA_BITS);
val >>= 7;
buffer.push_back(byte);
byte = 0;
}
return buffer;
}
static inline std::string encode_num(int32_t val){
return encode_num((int64_t)val);
}
static inline std::string encode_num(uint32_t val){
return encode_num((uint64_t)val);
}
class FastMessage
{
private:
int template_id_;
std::map<int, std::string> fields_;
public:
FastMessage();
~FastMessage();
int set_template(int template_id);
int add(int32_t val, int index=-1);
int add(int64_t val, int index=-1);
int add(uint32_t val, int index=-1);
int add(uint64_t val, int index=-1);
int add(const char *val, int index=-1);
// set field
// int set_field(int index, int32_t val);
// ...
std::string encode();
};
FastMessage::FastMessage(){
template_id_ = 0;
}
FastMessage::~FastMessage(){
}
int FastMessage::set_template(int template_id){
template_id_ = template_id;
return template_id;
}
int FastMessage::add(int32_t val, int index){
return add((int64_t)val, index);
}
int FastMessage::add(int64_t val, int index){
if(index == -1){
index = (int)fields_.size();
}
std::string enc = encode_num(val);
fields_[index] = enc;
return 0;
}
int FastMessage::add(uint32_t val, int index){
return add((uint64_t)val, index);
}
int FastMessage::add(uint64_t val, int index){
if(index == -1){
index = (int)fields_.size();
}
std::string enc = encode_num(val);
fields_[index] = enc;
return 0;
}
int FastMessage::add(const char *val, int index){
// printf("%d\n", __LINE__);
if(index == -1){
index = (int)fields_.size();
}
std::string enc;
int ret = 0;
int len = strlen(val);
if(len == 0){
printf("%d\n", __LINE__);
enc.push_back(EMPTY_STRING);
}else{
for(int pos = 0; pos < len - 1; ++pos){
enc.push_back((unsigned char)(val[pos]));
ret ++;
}
enc.push_back(val[len - 1] | STOP_BIT);
ret ++;
}
fields_[index] = enc;
return ret;
}
std::string FastMessage::encode(){
int num_fields = 0; // TODO: 根据模板获得
if(!fields_.empty()){
std::map<int, std::string>::reverse_iterator it = fields_.rbegin();
num_fields = it->first;
}
std::string buffer;
int num = ceil(num_fields / 7.0);
buffer.append(num, '\0');
for(std::map<int, std::string>::iterator it = fields_.begin(); it != fields_.end(); it++){
int i = it->first;
// template_id is always set, so i+1
int byte_offset = (i + 1) / 7;
int bit_offset = 7 - (i + 1) % 7 - 1;
buffer[byte_offset] |= (1 << bit_offset);
printf("%d:%d = 1, %02x\n", byte_offset, bit_offset, buffer[byte_offset]);
}
buffer[buffer.size() - 1] |= STOP_BIT;
buffer[0] |= (1 << 6); // template_id is set
buffer.append(encode_num(template_id_));
for(std::map<int, std::string>::iterator it = fields_.begin(); it != fields_.end(); it++){
printf("%d\n", it->first);
buffer.append(it->second);
}
return buffer;
}
#pragma pack(push, 1)
struct tagFASTHeader
{
char magic[4]; // equ g_pszMagic
char type[4];
char space[2];
unsigned int len;
};
#pragma pack(pop)
int main(int argc, char **argv){
{
// 8=FIX.4.2|9=117|35=A|49=CLIENT08|56=SERVER|34=1|52=20150421-21:20:56.000|98=0|108=1600|96=H::135790:|95=10|141=Y|553=|554=|383=99999|10=068|
FastMessage msg;
// pmap
unsigned char pmap[4] = {0x7f, 0xf0, 0x50, '\0'};
#if 1
msg.set_template(90001);
msg.add("FIX.4.2");
msg.add((uint32_t)117);
msg.add((uint32_t)1);
msg.add("A");
msg.add("CLIENT08");
msg.add("20150421-21:14:34.000");
msg.add("SERVER");
msg.add((uint32_t)68);
msg.add("H::135790:");
msg.add("10");
msg.add("");
msg.add("");
msg.add((uint32_t)91);
msg.add("Y");
msg.add((uint32_t)1600);
msg.add((uint32_t)99999);
#else
msg.set_template(1);
msg.add("HelloWorld");
#endif
std::string fast_msg = msg.encode();
for(int i=0; i<(int)(fast_msg.size()); i++){
printf("%02x ", (unsigned char)fast_msg[i]);
if(i == (int)(fast_msg.size()) - 1){
printf("\n");
}
}
//exit(0);
int sock;
const char *ip = "127.0.0.1";
int port = 8876;
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons((short)port);
inet_pton(AF_INET, ip, &addr.sin_addr);
if((sock = ::socket(AF_INET, SOCK_STREAM, 0)) == -1){
printf("%d: error\n", __LINE__);
}
if(::connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1){
printf("%d: error\n", __LINE__);
}
tagFASTHeader header;
strcpy(header.magic, "FaSt");
strcpy(header.type, "A");
strcpy(header.space, "ki");
header.len = fast_msg.size();
int ret;
ret = write(sock, &header, sizeof(header));
printf("send: %d\n", ret);
ret = write(sock, fast_msg.data(), fast_msg.size());
printf("send: %d\n", ret);
char recv_buf[8192];
ret = ::read(sock, recv_buf, 8192);
printf("recv: %d\n", ret);
}
return 0;
}