-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.cpp
366 lines (343 loc) · 14.9 KB
/
read.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
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
/*
* @Author: Rex
* @Date: 2018-09-25 10:58:51
* @Last Modified by:
* @Last Modified time: 2018-09-28 16:42:05
*/
#include "test.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <linux/if_packet.h>
#include <netinet/if_ether.h>
#include <iostream>
#include "OrderbookData.h"
#include "TradeData.h"
#include "ReferenceData.h"
#define BUFF_SIZE 2048
static const char *g_szIfName = "ens3f1np1"; // 网卡接口
typedef struct XdpPacketHeader
{
unsigned short mPktSize;
unsigned char mMsgCount;
unsigned char mFiller;
unsigned int mSeqNum;
unsigned long long mSendTime;
} PacketHeader;
typedef struct XdpMessageHeader
{
unsigned short mMsgSize;
unsigned short mMsgType;
} MessageHeader;
void ProcessMessageHeader(char *buf, int msgCount);
void AddOrder(char *buf, uint16_t offset, uint16_t len);
void ModifyOrder(char *buf, uint16_t offset, uint16_t len);
void DeleteOrder(char *buf, uint16_t offset, uint16_t len);
void ClearOrder(char *buf, uint16_t offset, uint16_t len);
void Trade(char *buf, uint16_t offset, uint16_t len);
void CommodityDefinition(char *buf, uint16_t offset, uint16_t len);
void SeriesDefinitionExtended(char *buf, uint16_t offset, uint16_t len);
void SeriesDefinitionBase(char *buf, uint16_t offset, uint16_t len);
void ClassDefinition(char *buf, uint16_t offset, uint16_t len);
std::string trim(std::string s);
int main(int argc, char *argv[])
{
int n;
char buf[BUFF_SIZE];
struct sockaddr_ll sll;
struct ifreq ifr;
struct ethhdr *eth;
struct iphdr *iph;
struct udphdr *udph;
FILE *fp;
fp = fopen(argv[1], "rb+");
fseek(fp, 0, SEEK_SET);
int i = 0;
while (fread(buf, sizeof(buf), 1, fp))
{
if (i++ == 100000)
break;
// buf[strlen(buf)] = '\0';
// 接收到的数据帧头6字节是目的MAC地址,紧接着6字节是源MAC地址。
eth = (struct ethhdr *)buf;
// printf("Dest MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_dest[0], eth->h_dest[1], eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
// printf("Source MAC addr:%02x:%02x:%02x:%02x:%02x:%02x\n", eth->h_source[0], eth->h_source[1], eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]);
iph = (struct iphdr *)(buf + sizeof(struct ethhdr));
// if (iph->version == 4 && iph->ihl == 5)
// {
// printf("version:%d,ihl:%d\n",iph->version,iph->ihl);
// printf("Source:%s\n", inet_ntoa(*(struct in_addr *) &iph->saddr));
// printf("Dest:%s\n", inet_ntoa(*(struct in_addr *) &iph->daddr));
// }
if (iph->protocol == IPPROTO_UDP)
{
udph = (struct udphdr *)(buf + sizeof(struct ethhdr) + sizeof(struct iphdr));
char *packetPtr = buf + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct udphdr);
PacketHeader *hdr = (PacketHeader *)(packetPtr);
char *msgPtr = packetPtr + 16; // packet header len = 16bytes
char *ip = inet_ntoa(*(struct in_addr *)&iph->daddr);
int port = ntohs(udph->dest);
if (hdr->mMsgCount > 0)
{
if (strcmp(ip, "239.1.127.155") == 0 && port == 51002)
{
// printf("\n==================================================channel id:121 239.1.127.130:51001======================================================\n");
// printf("PktSize:%hu,", hdr->mPktSize);
// printf("MsgCount:%hu,", hdr->mMsgCount);
// printf("SeqNum:%lu\n", hdr->mSeqNum);
ProcessMessageHeader(msgPtr, hdr->mMsgCount);
}
// if (strcmp(ip, "239.1.127.128") == 0 && port == 51003)
// {
// // printf("\n==================================================channel id:221 239.1.127.130:51002======================================================\n");
// // printf("PktSize:%hu,", hdr->mPktSize);
// // printf("MsgCount:%hu,", hdr->mMsgCount);
// // printf("SeqNum:%lu\n", hdr->mSeqNum);
// ProcessMessageHeader(msgPtr, hdr->mMsgCount);
// }
// if(strcmp(ip, "239.1.127.155") == 0 && port == 51001)
// {
// printf("\n==================================================239.1.127.155:51001======================================================\n");
// printf("PktSize:%hu,", hdr->mPktSize);
// printf("MsgCount:%hu,", hdr->mMsgCount);
// printf("SeqNum:%lu\n", hdr->mSeqNum);
// ProcessMessageHeader(msgPtr, hdr->mMsgCount);
// }
// if(strcmp(ip, "239.1.127.155") == 0 && port == 51002)
// {
// printf("\n==================================================239.1.127.155:51002======================================================\n");
// printf("PktSize:%hu,", hdr->mPktSize);
// printf("MsgCount:%hu,", hdr->mMsgCount);
// printf("SeqNum:%lu\n", hdr->mSeqNum);
// ProcessMessageHeader(msgPtr, hdr->mMsgCount);
// }
}
}
}
return 0;
}
std::string trim(std::string s)
{
if (!s.empty())
{
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
return s;
}
void ProcessMessageHeader(char *buf, int msgCount)
{
int n = 0;
int size = 0;
while (n < msgCount)
{
MessageHeader *msghdr = (MessageHeader *)buf;
// printf("msg %d: msgsize=%d, msgtype=%d\n", n, msghdr->mMsgSize, msghdr->mMsgType);
// if (msghdr->mMsgType == ADDORDER)
// {
// AddOrder(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == DELETEORDER)
// {
// DeleteOrder(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == MODIFYORDER)
// {
// ModifyOrder(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == CLEARORDER)
// {
// ClearOrder(buf, 4, msghdr->mMsgSize);
// }
if (msghdr->mMsgType == TRADE)
{
Trade(buf, 4, msghdr->mMsgSize);
}
// if (msghdr->mMsgType == COMMODITYDEFINITION)
// {
// CommodityDefinition(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == CLASSDEFINITION)
// {
// ClassDefinition(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == SERIESDEFINITIONBASE)
// {
// SeriesDefinitionBase(buf, 4, msghdr->mMsgSize);
// }
// if (msghdr->mMsgType == SERIESDEFINITIONEXTENDED)
// {
// SeriesDefinitionExtended(buf, 4, msghdr->mMsgSize);
// }
n++;
buf = buf + msghdr->mMsgSize;
size += msghdr->mMsgSize;
}
// printf("total size=%d\n", size + 16);
}
void AddOrder(char *buf, uint16_t offset, uint16_t len)
{
XdpAddOrder addOrder(buf, len, offset);
if (addOrder.orderbookId() == 56102818)
{
printf("add\n");
printf("OrderbookID:%u\n", addOrder.orderbookId());
printf("OrderID:%llu\n", addOrder.orderId());
printf("price:%d\n", addOrder.price());
printf("quantity:%u\n", addOrder.quantity());
printf("side:%hu\n", addOrder.side());
printf("lotType:%d\n", addOrder.lotType());
printf("OrderType:%d\n", addOrder.orderType());
printf("orderBookPosition:%u\n\n", addOrder.orderbookPosition());
std::cout << "===============================================================" << std::endl;
}
}
void ModifyOrder(char *buf, uint16_t offset, uint16_t len)
{
XdpModifyOrder modifyOrder(buf, len, offset);
// if (modifyOrder.orderbookId() == 56102818)
// {
printf("modify\n");
printf("OrderbookID:%u\n", modifyOrder.orderbookId());
printf("OrderID:%llu\n", modifyOrder.orderId());
printf("price:%d\n", modifyOrder.price());
printf("quantity:%u\n", modifyOrder.quantity());
printf("side:%hu\n", modifyOrder.side());
printf("OrderType:%d\n", modifyOrder.orderType());
printf("orderBookPosition:%u\n\n", modifyOrder.orderbookPosition());
std::cout << "===============================================================" << std::endl;
// }
}
void DeleteOrder(char *buf, uint16_t offset, uint16_t len)
{
XdpDeleteOrder deleteOrder(buf, len, offset);
if (deleteOrder.orderbookId() == 56102818)
{
printf("delete\n");
printf("OrderbookID:%u\n", deleteOrder.orderbookId());
printf("OrderID:%llu\n", deleteOrder.orderId());
printf("side:%hhu\n", deleteOrder.side());
std::cout << "===============================================================" << std::endl;
}
}
void ClearOrder(char *buf, uint16_t offset, uint16_t len)
{
XdpClearOrder clearOrder(buf, len, offset);
if (clearOrder.orderbookId() == 56102818)
{
printf("clear\n");
printf("OrderbookID:%u\n", clearOrder.orderbookId());
std::cout << "===============================================================" << std::endl;
}
}
void Trade(char *buf, uint16_t offset, uint16_t len)
{
XdpTrade trade(buf, len, offset);
// if (trade.orderbookId() == 56102818)
{
printf("tradeTime:%u\n", trade.tradeTime());
printf("orderbookId:%u\n", trade.orderbookId());
printf("orderId:%llu\n", trade.orderId());
printf("tradeId:%llu\n", trade.tradeId());
printf("comboGroupId:%u\n", trade.comboGroupId());
printf("price:%d\n", trade.price());
printf("quantity:%u\n", trade.quantity());
printf("side:%hhu\n", trade.side());
printf("dealType:%hhu\n", trade.dealType());
printf("tradeCondition:%hu\n", trade.tradeCondition());
printf("dealInfo:%hu\n\n", trade.dealInfo());
}
}
void CommodityDefinition(char *buf, uint16_t offset, uint16_t len)
{
XdpCommodityDefinition commodity(buf, len, offset);
// commodity.commodityId();
if (trim(commodity.commodityName()) == "HANG SENG INDEX")
{
std::cout << "commodityCode:" << commodity.commodityCode() << std::endl;
std::cout << "commodityId:" << commodity.commodityId() << std::endl;
std::cout << "underlyingCode:" << commodity.underlyingCode() << std::endl;
std::cout << "commodityName:" << commodity.commodityName() << std::endl;
std::cout << "isinCode:" << commodity.isinCode() << std::endl;
std::cout << "baseCurrency:" << commodity.baseCurrency() << std::endl;
std::cout << "decimalUnderlyingPrice:" << commodity.decimalUnderlyingPrice() << std::endl;
std::cout << "underlyingPriceUnit:" << +commodity.underlyingPriceUnit() << std::endl;
std::cout << "nominalValue:" << commodity.nominalValue() << std::endl;
std::cout << "underlyingType:" << +commodity.underlyingType() << std::endl;
std::cout << "effectiveTomorrow:" << +commodity.effectiveTomorrow() << std::endl;
std::cout << "===============================================================" << std::endl;
}
}
void SeriesDefinitionBase(char *buf, uint16_t offset, uint16_t len)
{
XdpSeriesDefinitionBase sdb(buf, len, offset);
printf("orderbookId:%u\n", sdb.orderbookId());
std::cout << "symbol:" << sdb.symbol() << std::endl;
printf("financialProduct:%hhu\n", sdb.financialProduct());
printf("numberOfDecimalsPrice:%hu\n", sdb.numberOfDecimalsPrice());
printf("numberOfLegs:%hhu\n", sdb.numberOfLegs());
printf("strikePrice:%u\n", sdb.strikePrice());
std::cout << "expirationDate:" << sdb.expirationDate() << std::endl;
printf("price:%hhu\n", sdb.putOrCall());
}
void SeriesDefinitionExtended(char *buf, uint16_t offset, uint16_t len)
{
XdpSeriesDefinitionExtented sde(buf, len, offset);
// if((sde.commodityCode() == 4002 || sde.commodityCode() == 4010) && sde.instrumentGroup() == 4)
if (trim(sde.symbol()) == "HSIV8")
{
std::cout << "orderbookId:" << sde.orderbookId() << std::endl;
std::cout << "symbol:" << sde.symbol() << std::endl;
std::cout << "country:" << +sde.country() << std::endl;
std::cout << "market:" << +sde.market() << std::endl;
std::cout << "instrumentGroup:" << +sde.instrumentGroup() << std::endl;
std::cout << "modifier:" << +sde.modifier() << std::endl;
std::cout << "commodityCode:" << sde.commodityCode() << std::endl;
std::cout << "expirationDate:" << sde.expirationDate() << std::endl;
std::cout << "strikePrice:" << sde.strikePrice() << std::endl;
std::cout << "contractSize:" << +sde.contractSize() << std::endl;
std::cout << "isinCode:" << sde.isinCode() << std::endl;
std::cout << "seriesStatus:" << +sde.seriesStatus() << std::endl;
std::cout << "effectiveTomorrow:" << +sde.effectiveTomorrow() << std::endl;
std::cout << "priceQuotationFactor:" << sde.priceQuotationFactor() << std::endl;
std::cout << "effectiveExpDate:" << sde.effectiveExpDate() << std::endl;
std::cout << "dateTimeLastTrading:" << sde.dateTimeLastTrading() << std::endl;
std::cout << "===============================================================" << std::endl;
}
}
void ClassDefinition(char *buf, uint16_t offset, uint16_t len)
{
XdpClassDefinition cd(buf, len, offset);
if (cd.commodityCode() == 4002 || cd.commodityCode() == 4010)
{
printf("country:%hhu\n", cd.country());
printf("market:%hhu\n", cd.market());
printf("instrumentGroup:%hhu\n", cd.instrumentGroup());
printf("modifier:%hhu\n", cd.modifier());
printf("commodityCode:%hu\n", cd.commodityCode());
printf("expirationDate:%d\n", cd.priceQuotationFactor());
printf("strikePrice:%u\n", cd.contractSize());
printf("contractSize:%hu\n", cd.decimalInStrikePrice());
printf("decimalInContractSize:%hu\n", cd.decimalInContractSize());
printf("decimalInPremium:%hu\n", cd.decimalInPremium());
printf("rnkingType:%hu\n", cd.rnkingType());
printf("tradable:%hhu\n", cd.tradable());
printf("premiumUnit4Price:%hhu\n", cd.premiumUnit4Price());
std::cout << "baseCurrency:" << cd.baseCurrency() << std::endl;
std::cout << "instrumentClassID:" << cd.instrumentClassID() << std::endl;
std::cout << "instrumentClassName:" << cd.instrumentClassName() << std::endl;
std::cout << "isFractions:" << cd.isFractions() << std::endl;
std::cout << "settlementCurrencyID:" << cd.settlementCurrencyID() << std::endl;
printf("market:%hhu\n", cd.effectiveTomorrow());
printf("instrumentGroup:%d\n\n", cd.tickStepSize());
}
}