-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEtherealRead.cpp
More file actions
413 lines (343 loc) · 9.19 KB
/
EtherealRead.cpp
File metadata and controls
413 lines (343 loc) · 9.19 KB
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
#include "kdvtype.h"
#include "osp.h"
#include "EthFileRead.h"
#include "kdvsys.h"
/*******************************
*******************************/
s32 SendProc(CEthFile* pcEthFile, u32 dwSndDstIp, u16 wSndDstPort, u32 dwSndSrcIp, u16 wSndSrcPort)
{
FILE *fpRawData = NULL;
FILE *fpRawDataLen = NULL;
if (NULL == pcEthFile)
{
return -1;
}
#if 0 //不另存文件
if (pcEthFile->m_bWriteFile)
{
fpRawData = fopen("c:\\data.dat", "wb");
fpRawDataLen = fopen("c:\\datalen.txt", "wb");
}
#endif
s32 nSocket;
nSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
printf("socket = %d\n", nSocket);
if (nSocket < 0)
{
return -1;
}
/* 绑定指定端口发送数据 */
if (wSndSrcPort != 0)
{
s32 nResult = 0;
struct sockaddr_in sout;
memset(&sout, 0, sizeof(sout));
sout.sin_family = AF_INET;
sout.sin_port = htons(wSndSrcPort);
sout.sin_addr.s_addr = 0;
nResult = bind(nSocket, (struct sockaddr*)&sout, sizeof(sout));
if (nResult != 0)
{
printf("warning: bind to specified port %d error!", wSndSrcPort);
Sleep(5000);
}
}
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(wSndDstPort);
sin.sin_addr.s_addr = dwSndDstIp;
s32 nLen;
s32 nCount = 0;
u32 dwSpan = 0;
u32 dwNowHi;
u32 dwNowLo;
frame_hdr* ptFrame;
StartRead:
nLen = pcEthFile->FirstPacket();
ptFrame = pcEthFile->GetFrame();
if (nLen > 0 && NULL != ptFrame)
{
dwNowHi = ptFrame->time_hi;
dwNowLo = ptFrame->time_lo;
}
u64 qwLocalStartTime;
u32 dwRunTime = 0;
qwLocalStartTime = GetTickCount();
qwLocalStartTime *= 1000;
while(nLen > 0)
{
nCount++;
printf("packet %d len: %d ", nCount, nLen);
if (dwSpan != 0)
{
printf("time span %d\n", dwSpan);
}
else
{
printf("\n");
}
u8* pData;
pData = pcEthFile->GetData();
if (nLen > 0 && NULL != pData)
{
if (wSndDstPort == 0)
{
u16 wRealPort = pcEthFile->GetUdpDstPort();
sin.sin_port = htons(wRealPort);
}
sendto(nSocket, (s8*)pData, nLen, 0, (struct sockaddr*)&sin, sizeof(sin));
if (pcEthFile->m_bWriteFile && fpRawData && fpRawDataLen)
{
fwrite(pData, nLen, 1, fpRawData);
s8 abyTmp[16];
sprintf(abyTmp, "%d\r\n", nLen);
fwrite(abyTmp, 1, strlen(abyTmp), fpRawDataLen);
}
}
u32 dwLastHi = 0;
u32 dwLastLo = 0;
dwLastHi = dwNowHi;
dwLastLo = dwNowLo;
nLen = pcEthFile->NextPacket();
if (nLen > 0)
{
ptFrame = pcEthFile->GetFrame();
dwNowHi = ptFrame->time_hi;
dwNowLo = ptFrame->time_lo;
if ((dwLastHi == dwNowHi && dwLastLo > dwNowLo) || dwLastHi == dwNowHi+1)
{ // at duro cpu, sometime, time is a little revert
dwLastHi = dwNowHi;
dwLastLo = dwNowLo;
}
if (dwLastHi > dwNowHi)
{
dwLastHi = dwNowHi;
dwLastLo = dwNowLo;
}
dwSpan = (dwNowHi-dwLastHi)*1000000;
if (dwNowLo >= dwLastLo)
{
dwSpan = dwSpan + (dwNowLo - dwLastLo);
}
else
{
dwSpan = dwSpan - (dwLastLo - dwNowLo);
}
dwSpan = 20 * 1000;
dwRunTime += dwSpan;
u64 qwNowTime = GetTickCount();
qwNowTime *= 1000;
u64 qwPlayTick = qwLocalStartTime + dwRunTime;
//当前时间相差大于1S(因为中间程序暂停缘故),则不进行弥补,直接跳过。
if (qwPlayTick + 1000*1000 < qwNowTime)
{
dwRunTime = qwNowTime - qwLocalStartTime;
}
else if (qwPlayTick > qwNowTime)
{
u32 dwSleepTime = dwRunTime + qwLocalStartTime - qwNowTime;
if (dwSleepTime > 200*1000)
{
s32 i = 0;
}
Sleep(dwSleepTime/1000);
}
}
}
if (pcEthFile->m_bRepeat)
goto StartRead;
if (fpRawData)
{
fclose(fpRawData);
}
if (fpRawDataLen)
{
fclose(fpRawDataLen);
}
closesocket(nSocket);
return nCount;
}
s32 main(int argc, char *argv[])
{
#ifdef WIN32
{
WSADATA wsadata;
if(WSAStartup(0x0101, &wsadata)!=0)
return FALSE;
}
#endif
s8* strFileDefault = "config.ini"; /* config file name */
s8* strFileConfig = strFileDefault;
// ?strFileName的作用是什么
s8* strFilename = NULL;
if (2 >= argc)
{
printf("you can use: %s <config> <filename>\n", argv[0]);
Sleep(2 * 1000);
}
else
{
strFileConfig = argv[1];
//strFilename = argv[2];
}
s8 achFilename[256] = "test";
/* receive */
s8 achRcvSrcIp[64];
s8 achRcvDstIp[64];
s32 nSrcPort;
s32 nDstPort;
/* send */
s8 achSendDstIp[64];
s8 achSendSrcIp[64];
s32 nSendDstPort;
s32 nSendSrcPort;
/* ctrl variables */
BOOL32 bRepeat;
BOOL32 bWriteYUVFile;
BOOL32 bOK = FALSE;
do
{
if (!GetRegKeyString(strFileConfig,
"FILTERINFO",
"SRC_IP",
"0.0.0.0",
achRcvSrcIp,
sizeof(achRcvSrcIp)))
{
printf("Read Filter SRC_IP error!\n");
strcpy(achRcvSrcIp, "0.0.0.0");
}
if (!GetRegKeyInt(strFileConfig,
"FILTERINFO",
"SRC_PORT",
0,
&nSrcPort))
{
printf("Read Filter SRC_PORT error!\n");
nSrcPort = 0;
}
if (!GetRegKeyString(strFileConfig,
"FILTERINFO",
"DST_IP",
"0.0.0.0",
achRcvDstIp,
sizeof(achRcvDstIp)))
{
printf("Read Filter DST_IP error!\n");
strcpy(achRcvDstIp, "0.0.0.0");
}
if (!GetRegKeyInt(strFileConfig,
"FILTERINFO",
"DST_PORT",
0,
&nDstPort))
{
printf("Read Filter DST_PORT error!\n");
nDstPort = 0;
}
GetRegKeyString(strFileConfig, "FILTERINFO", "FILENAME", "test", achFilename, 255);
if (!GetRegKeyString(strFileConfig,
"SENDINFO",
"DST_IP",
"127.0.0.1",
achSendDstIp,
sizeof(achSendDstIp)))
{
printf("Read Send DST_IP error!\n");
break;
}
/* obtain send destination port */
if (!GetRegKeyInt(strFileConfig,
"SENDINFO",
"DST_PORT",
0,
&nSendDstPort))
{
printf("Read Send DST_PORT error!\n");
break;
}
/* obtain local send addr */
if (!GetRegKeyString(strFileConfig,
"SENDINFO",
"SRC_IP",
"127.0.0.1",
achSendSrcIp,
sizeof(achSendSrcIp)))
{
printf("use default local send addr %s\n", achSendSrcIp);
}
/* obtain local send port */
if (!GetRegKeyInt(strFileConfig,
"SENDINFO",
"SRC_PORT",
0,
&nSendSrcPort))
{
printf("use default local send port %d\n", nSendSrcPort);
}
/* obtain repeat info */
if (!GetRegKeyInt(strFileConfig,
"SENDINFO",
"REPEAT",
0,
&bRepeat))
{
bRepeat = 0;
}
if (!GetRegKeyInt(strFileConfig,
"SENDINFO",
"WRITEFILE",
0,
&bWriteYUVFile))
{
printf("don't save the yuv file\n");
bWriteYUVFile = 0;
}
bOK = TRUE;
} while(0);
if (3 == argc)
{
nSendSrcPort = atoi(argv[2]);
printf("nSendSrcPort is %u\n", nSendSrcPort);
}
CEthFile cFileRead;
s8 *strFileCap = achFilename;
if (NULL != strFilename)
{
strFileCap = strFilename;
}
if (!cFileRead.Create(strFileCap))
{
return FALSE;
}
if (bOK)
{
cFileRead.SetFilter(inet_addr(achRcvSrcIp),
inet_addr(achRcvDstIp),
(u16)nSrcPort,
(u16)nDstPort);
cFileRead.SetParam(bRepeat, bWriteYUVFile);
do
{
if (SendProc(&cFileRead,
inet_addr(achSendDstIp),
(u16)nSendDstPort,
inet_addr(achSendSrcIp),
(u16)nSendSrcPort) <= 0)
break;
} while (0);
}
else
{
//wait
char aby[256];
scanf("%s\n", aby);
}
printf("Game Over\n");
#ifdef WIN32
WSACleanup();
#endif
return TRUE;
//getchar();
}