-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
495 lines (431 loc) · 17 KB
/
Program.cs
File metadata and controls
495 lines (431 loc) · 17 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.Threading;
using Gps.Plugin.Tcp.GT808;
using Gps.Plugin.Common.Helpers;
using System.Configuration;
namespace GT808ClientTest
{
class Program
{
protected static log4net.ILog log = null;
private string ip;
private int port;
private string deviceId;
Random r = new Random();
private int interval;
private int sendCountByOneDevice;
private ProcessCounter processCounter;
private static bool sendLogPrint;
private static bool waitReceive;
private static bool receiveLogPrint;
static Program()
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
private double initLat = 38.01342;
private double initLon = 114.560478;
public Program(string ip, int port, string deviceId, int interval, int sendCountByOneDevice, ProcessCounter processCounter)
{
// TODO: Complete member initialization
this.latlonBuilder = new LatlonBuilder(initLat, initLon, 31.802893, 39.300299, 104.941406, 117.861328);
this.ip = ip;
this.port = port;
this.deviceId = deviceId;
this.interval = interval;
this.sendCountByOneDevice = sendCountByOneDevice;
this.processCounter = processCounter;
}
private static byte[] stringtobytes(string str)
{
string[] strs = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
byte[] buffer = new byte[strs.Length];
for (int i = 0; i < strs.Length; ++i)
{
buffer[i] = Convert.ToByte(strs[i], 16);
}
return buffer;
}
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//var headpack = new Gps.Plugin.Tcp.GT808.HeadPack();
//byte[] packBytes = stringtobytes("7E-01-02-00-0E-00-20-47-05-52-32-00-00-32-30-31-32-30-31-31-39-30-38-31-30-34-37-0D-7E");
//if (RawFormatter.Instance.Bytes2Struct(headpack, packBytes , 1, HeadPack.PackSize))
//{
// Console.WriteLine("OK");
// Console.ReadKey();
//}
//MessageContext pack = new MessageContext();
//byte[] packBytes = stringtobytes("68-68-25-00-00-00-00-00-00-00-12-34-56-00-18-10-DC-01-13-13-06-17-04-14-11-DC-0C-4A-7F-5C-1A-00-00-00-00-00-00-00-00-00-0A-0D");
//if (RawFormatter.Instance.Bytes2Struct(pack, packBytes, BodyData.PackSize, MessageContext.PackSize))
//{
// Console.WriteLine("OK");
// Console.ReadKey();
//}
sendLogPrint = Convert.ToBoolean(ConfigurationManager.AppSettings["sendLogPrint"]);
waitReceive = Convert.ToBoolean(ConfigurationManager.AppSettings["waitReceive"]);
receiveLogPrint = Convert.ToBoolean(ConfigurationManager.AppSettings["receiveLogPrint"]);
string ip = "";
int port = 0;
while (port == 0 || string.IsNullOrEmpty(ip))
{
string line = "";
string configValue = ConfigurationManager.AppSettings["remoteServerPort"];
if (string.IsNullOrEmpty(configValue))
{
Console.Write("请输入服务器IP和端口(113.31.92.200:8201):");
line = Console.ReadLine();
}
else
{
line = configValue;
}
if (string.IsNullOrEmpty(line))
line = "113.31.92.200:8201";
int pos = line.IndexOf(':');
if (pos == -1)
{
Console.WriteLine("正确格式:xx.xxx.xxx.xxx:xxxx");
continue;
}
ip = line.Substring(0, pos);
string strPort = line.Substring(pos + 1);
int.TryParse(strPort, out port);
}
int min = 0, max = 0;
string deviceIdRangeValue = ConfigurationManager.AppSettings["deviceIdRange"];
if (string.IsNullOrEmpty(deviceIdRangeValue))
{
Console.WriteLine("生成设备标识格式为:0000000000000000,0000000000000001....");
Console.Write("依客户端网络情况输入要模拟的客户端数量(1-10000):");
string strCount = Console.ReadLine();
max = int.Parse(strCount);
}
else
{
string[] items = deviceIdRangeValue.Split('-');
min = int.Parse(items[0]);
max = int.Parse(items[1]);
}
int interval = 30;
string strInterval = ConfigurationManager.AppSettings["interval"];
if (string.IsNullOrEmpty(strInterval))
{
Console.Write("每个消息间隔(秒):");
strInterval = Console.ReadLine();
interval = int.Parse(strInterval);
}
else
{
interval = int.Parse(strInterval);
}
int sendCountByOneDevice = 30;
string strSendCountByOneDevice = ConfigurationManager.AppSettings["sendCountByOneDevice"];
if (string.IsNullOrEmpty(strSendCountByOneDevice))
{
Console.Write("每个设备发送共多少个包后断开连接:");
strSendCountByOneDevice = Console.ReadLine();
sendCountByOneDevice = int.Parse(strSendCountByOneDevice);
}
else
{
sendCountByOneDevice = int.Parse(strSendCountByOneDevice);
}
ProcessCounter processCounter = new ProcessCounter((max - min + 1) * sendCountByOneDevice, pc =>
{
Console.Title = pc.ToString();
});
for (int i = min; i <= max; ++i)
{
string deviceId = string.Concat("010000", i.ToString("00000"));
Program p = new Program(ip, port, deviceId, interval, sendCountByOneDevice, processCounter);
//p.Run();
Thread iThred = new Thread(p.Run);
iThred.Start();
//Thread.SpinWait(2);
}
Console.ReadLine();
}
private void Run()
{
Console.WriteLine("Thread {0} Start {1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
Socket tcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
tcp.Connect(IPAddress.Parse(ip), port);
try
{
while (this.sendCountByOneDevice-- > 0)
{
if (SendPosition(tcp))
{
processCounter.IncrSuccess();
}
else
{
processCounter.IncrFailed();
}
Thread.Sleep(interval * 1000);
//Thread.Sleep(100);
}
/*
if (SendPosition(tcp))
{
processCounter.IncrSuccess();
}
else
{
processCounter.IncrFailed();
}
if (--this.sendCountByOneDevice <= 0)
{
//timer.Change(-1, -1);
//try
//{
// tcp.Disconnect(true);
// tcp.Dispose();
//}
//catch (Exception exp)
//{
// log.Warn("断开连接出错:" + exp.Message);
//}
}
else
{
//SendPosition(tcp);
//timer.Change(Math.Max(1, interval * 1000 - watch.ElapsedMilliseconds), -1);
}
*/
}
catch (SocketException exp)
{
log.Debug("连接已经断开, 重连接中");
//timer.Change(Timeout.Infinite, Timeout.Infinite);
this.Run();
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
//timer.Change(Math.Max(1, interval * 1000 - watch.ElapsedMilliseconds), -1);
}
}
catch (ThreadAbortException)
{
return;
}
catch (Exception exp)
{
log.Error(exp);
}
Console.WriteLine("Thread {0} End {1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
}
ushort seqNum = 1;
ushort GetNextSeqNum()
{
lock (this)
{
return ++seqNum;
}
}
private byte[] bufferRecv = new byte[1024];
private LatlonBuilder latlonBuilder;
private bool SendPosition(Socket tcp)
{
HeadPack head = new HeadPack() { SeqNO = GetNextSeqNum(), MessageId = (ushort)MessageIds.PositionReport, BodyProp = (ushort)0 };
head.SetDeviceId(this.deviceId);
double lat;
double lon;
int speed = 10 + r.Next(90);
latlonBuilder.GetNextLatlon(speed, out lat, out lon);
PositionReportPack pack = new PositionReportPack()
{
AlermFlags = 0,
Speed = (ushort)(speed * 10),
State = 0,
Latitude = Convert.ToUInt32(lat * 1000000),
Longitude = Convert.ToUInt32(lon * 1000000),
Altitude = 200,
Direction = 0,
Time = DateTime.Now.ToString("yyMMddHHmmss")
};
byte[] bytesSend = RawFormatter.Instance.Struct2Bytes(pack);
BodyPropertyHelper.SetMessageLength(ref head.BodyProp, (ushort)bytesSend.Length);
byte[] headBytes = RawFormatter.Instance.Struct2Bytes(head);
byte[] fullBytes = headBytes.Concat(bytesSend).ToArray();
byte checkByte = PackHelper.CalcCheckByte(fullBytes, 0, fullBytes.Length);
bytesSend = (new byte[] { 0x7e }
.Concat(PackHelper.EncodeBytes(fullBytes.Concat(new byte[] { checkByte })))
.Concat(new byte[] { 0x7e })).ToArray();
//Console.WriteLine("{0} {1}",head.SeqNO, bytesSend.ToHexString());
//发送消息
SendBytes(tcp, bytesSend);
//控制台打印日志cpu占用太高
if (sendLogPrint)
{
Console.WriteLine("{0} {1}, LatLon:{2:0.000000},{3:0.000000}", head.GetDeviceId(), DateTime.Now.ToString(), lat, lon);
}
//等待接收服务端返回值
var success = true;
if (waitReceive)
{
success = RecvBytes(tcp);
//var success = true;
}
return success;
}
void SendBytes(Socket tcp, byte[] bytes)
{
lock (System.Reflection.MethodBase.GetCurrentMethod())
{
if (bytes.Length != tcp.Send(bytes))
{
throw new SocketException((int)SocketError.ConnectionReset);
}
}
}
private bool RecvBytes(Socket tcp)
{
lock (bufferRecv)
{
byte[] buffer = bufferRecv;
int received = tcp.Receive(buffer);
//received = tcp.ReceiveAsync.Receive(buffer);
byte[] bytesReceived = PackHelper.DecodeBytes(buffer, 1, received - 2);
int rightSize = HeadPack.PackSize + ServerAnswerPack.PackSize + 1;
if (bytesReceived.Length != rightSize)
{
log.WarnFormat("返回消息长度不正确:" + bytesReceived.Length + "!=" + rightSize);
}
ServerAnswerPack pack = new ServerAnswerPack();
RawFormatter.Instance.Bytes2Struct(pack, bytesReceived, HeadPack.PackSize, ServerAnswerPack.PackSize);
if (pack.Result != 0)
{
log.WarnFormat("发送失败:" + pack.Result.ToString());
}
if(waitReceive && receiveLogPrint)
{
Console.WriteLine("SeqNO:{0} MessageId:{1} Result:{2}", pack.SeqNO,pack.MessageId,pack.Result);
}
return pack.Result == 0;
}
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
log.Error(e.ExceptionObject);
}
}
class LatlonBuilder
{
private double lat;
private double lon;
private double minLat;
private double maxLat;
private double minLon;
private double maxLon;
private int direction = 0;
private Random r = new Random();
public LatlonBuilder(double lat, double lon, double minLat, double maxlat, double minLon, double maxLon)
{
this.lat = lat;
this.lon = lon;
this.minLat = minLat;
this.maxLat = maxlat;
this.minLon = minLon;
this.maxLon = maxLon;
this.direction = r.Next(360);
}
public bool GetNextLatlon(int speed, out double lat, out double lon)
{
direction = (direction + (r.Next(30) - 15)) % 360;
double angle = Math.PI * this.direction / 180.0;
double latAdd = speed / 1000.0 * Math.Sin(angle);
double lonAdd = speed / 1000.0 * Math.Cos(angle);
this.lat = lat = this.lat + latAdd;
this.lon = lon = this.lon + lonAdd;
if (lat < minLat || lat > maxLat || lon < minLon || lon > maxLon)
{
direction = (direction + 180) % 360;
return GetNextLatlon(speed, out lat, out lon);
}
return true;
}
}
class ProcessCounter
{
private long current = 0, failedCounter = 0;
private Thread thread;
private AutoResetEvent evtUpdateUI = new AutoResetEvent(false);
private long updatePerCount;
private long lastUpdateVal;
public ProcessCounter(long max, Action<ProcessCounter> updateUI)
{
this.Max = max;
this.updatePerCount = max / 100;
this.thread = new Thread(new ThreadStart(() =>
{
while (true)
{
try
{
evtUpdateUI.WaitOne(500);
this.lastUpdateVal = Interlocked.Read(ref current);
updateUI(this);
}
catch (ThreadAbortException)
{
return;
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
}
}));
this.thread.IsBackground = true;
this.thread.Start();
}
public long Max { get; set; }
internal void IncrSuccess()
{
lock (this)
Incr();
}
internal void IncrFailed()
{
lock (this)
{
Incr();
Interlocked.Increment(ref failedCounter);
}
}
private void Incr()
{
var val = Interlocked.Increment(ref current);
if (val - lastUpdateVal > this.updatePerCount)
{
this.evtUpdateUI.Set();
}
if (val == Max)
{
Console.WriteLine("Done");
}
}
public override string ToString()
{
long curr = Interlocked.Read(ref current);
float g = Convert.ToSingle(curr / (Max / 100.0));
string ret = string.Concat(curr, "/", Max, "=>", g.ToString("##.00"), "%");
long failed = Interlocked.Read(ref failedCounter);
if (failed > 0)
ret = string.Concat(ret, "(Success:" + (curr - failed), ", Failed:", failed, ")");
return ret;
}
}
}