VoteServer.cs
20.1 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
using GeneralLib;
using NetLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SunVoteARSPPT
{
public class VoteServer
{
private static NetServer NServer = null;
public static Dictionary<string, NetSession> DicSessionName = null;
public static Dictionary<string, int> DicSessionNameNo = null;
private static Dictionary<string, NetSession> DicSessionID = null;
private static string PwdServer = "";
public static Dictionary<string, List<List<string>>> DicRosterMeet = new Dictionary<string, List<List<string>>>();
public static FrmServerSet FmServerSet = null;
public static string KeyName = "Name";
public static string KeyPwd = "Pwd";
public static string KeyLogon = "KeyLogon";
public static string KeyStartVote = "StartVote";
public static string KeySendType = "SendType";
/// <summary>
/// 程序启动时调用一次
/// </summary>
public static void ServerInit()
{
NServer = new NetServer();
DicSessionName = new Dictionary<string, NetSession>();
DicSessionNameNo = new Dictionary<string, int>();
DicSessionID = new Dictionary<string, NetSession>();
NServer.SessionConnected += NServer_SessionConnected;
NServer.SessionClosed += NServer_SessionClosed;
NServer.MessageReceived += NServer_MessageReceived;
NServer.DataReceived += NServer_DataReceived;
NServer.SessionErr += NServer_SessionErr;
StartVote(false);
TmrCheckOn = new System.Timers.Timer();
TmrCheckOn.Elapsed += TmrCheckOn_Elapsed;
TmrCheckOn.Interval = 1000;
TmrCheckOn.AutoReset = true;
}
public static bool IsStart
{
get
{
return NServer.IsStart;
}
}
public static bool Start(int port, string pwd)
{
bool res = false;
try
{
if (!NServer.Setup(port))
return false;
PwdServer = pwd;
res = NServer.Start();
AddLog("Server Start");//日志
TmrCheckOn.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
}
return res;
}
public static void Stop()
{
TmrCheckOn.Enabled = false;
AddLog("Server Stop");//日志
VoteServer.DicSessionID.Clear();
NServer.Stop();
Globals.SunVoteARSAddIn.PPTShow.StopTmrSendSlideScreen();
}
public static void ShowServerSet()
{
if ((FmServerSet == null) || (FmServerSet.IsDisposed))//杨斌 2020-05-16
FmServerSet = new FrmServerSet();
FmServerSet.TopMost = true;//杨斌 2020-05-15
FmServerSet.Show();//杨斌 2020-05-15
}
public static string GetDicDataPar(Dictionary<string, object> dicData, string key)
{
string res = "";
if (dicData.ContainsKey(key))
res = dicData[key] + "";
return res;
}
public static string GetSessionPar(NetSession session, string key)
{
string res = "";
if (session.DicPar.ContainsKey(key))
res = session.DicPar[key];
return res;
}
private static void NServer_SessionConnected(NetSession session)
{
bool bePass = false;
if (string.IsNullOrEmpty(PwdServer))
{
bePass = true;
}
else
{
string pwd = GetSessionPar(session, KeyPwd);
bePass = (pwd == PwdServer);
}
Dictionary<string, object> dicSend = new Dictionary<string, object>();
string msg = "";
AddLog("Session Connect", session);//日志
if (bePass)
{
string name = GetSessionPar(session, KeyName);
if (name.Length > 0)
{
if (DicSessionName.ContainsKey(name))
{
string oldID = DicSessionName[name].ID;
if (oldID != session.ID)//不会碰到相同ID
{
if (DicSessionID.ContainsKey(oldID))
{
AddLog("Session Already Logond Elsewhere", DicSessionName[name]);
Dictionary<string, object> dicSendOld = new Dictionary<string, object>();
dicSendOld.Add(KeyLogon, "Already logond elsewhere!");
//dicSendOld.Add(KeyStartVote, GetVoteState());
GetVoteState(dicSendOld);
msg = MyJson.Serialize(dicSendOld);
NServer.Send(msg, oldID);
NServer.CloseSession(oldID);
}
if (DicSessionID.ContainsKey(oldID))
DicSessionID.Remove(oldID);
}
DicSessionName[name] = session;
dicSend.Add(KeyLogon, "OK");
//dicSend.Add(KeyStartVote, GetVoteState());
GetVoteState(dicSend);
msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
AddLog("Session Connect OK", session);
//if (Globals.SunVoteARSAddIn.PPTShow.IsShowSlide)//杨斌 2019-05-10
//{
// SendVoteData(session);
//}
}
else
{
DicSessionName.Add(name, session);
DicSessionNameNo.Add(name, DicSessionName.Count);
dicSend.Add(KeyLogon, "OK");
//dicSend.Add(KeyStartVote, GetVoteState());
GetVoteState(dicSend);
msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
AddLog("Session Connect OK", session);
//if (Globals.SunVoteARSAddIn.PPTShow.IsShowSlide)//杨斌 2019-05-10
//{
// SendVoteData(session);
//}
}
DicSessionID.Add(session.ID, session);//不会碰到相同ID
SessionChange(session);
DicLastMsg[session.ID] = DateTime.Now;//杨斌 2020-05-18
}
else//客户端控制,不会碰到空名字
{
AddLog("Session Name is Empty", session);
dicSend.Add(KeyLogon, "The name can not be empty!");
msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
NServer.CloseSession(session.ID);
}
}
else
{
AddLog("Session Wrong Password", session);
dicSend.Add(KeyLogon, "Wrong Password!");
msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
NServer.CloseSession(session.ID);
}
}
static List<string> LstLog = new List<string>();
internal static void AddLog(string info, NetSession session = null)
{
try
{
info = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + info;
if (session != null)
{
info += " " + GetSessionPar(session, KeyName) + " " + session.ID +
" " + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port;
}
LstLog.Add(info);
if (TmrLog == null)
{
TmrLog = new System.Timers.Timer();
TmrLog.Elapsed += TmrLog_Elapsed;
TmrLog.Interval = 10;
TmrLog.AutoReset = false;
}
if (!TmrLog.Enabled)
TmrLog.Enabled = true;
}
catch (Exception ex)
{
string sErr = ex + "";
//System.Windows.Forms.MessageBox.Show(ex + "");
}
}
static System.Timers.Timer TmrLog = null;
static bool IsRunTimer = false;
private static void TmrLog_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (IsRunTimer)
{
TmrLog.Enabled = true;
return;
}
IsRunTimer = true;
////Delay(10000);
try
{
TmrLog.Enabled = false;
string file = AppDomain.CurrentDomain.BaseDirectory + "session-" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
while (LstLog.Count > 0)
{
string info = LstLog[0] + "\r\n";
LstLog.RemoveAt(0);
System.IO.File.AppendAllText(file, info);
}
}
catch (Exception ex)
{
string sErr = ex + "";
//System.Windows.Forms.MessageBox.Show(ex + "");
}
IsRunTimer = false;
}
public static void Delay(long ms)
{
Stopwatch t = Stopwatch.StartNew();
while (t.ElapsedMilliseconds < ms)
System.Windows.Forms.Application.DoEvents();
}
public static bool IsOnline(NetSession session)
{
bool isOnline = DicSessionID.ContainsKey(session.ID);
string name = GetSessionPar(session, KeyName);
if (VoteServer.DicSessionName.ContainsKey(name))
isOnline = VoteServer.DicSessionID.ContainsKey(VoteServer.DicSessionName[name].ID);
return isOnline;
}
private static void SessionChange(NetSession session)
{
bool isOnline = IsOnline(session);
if ((FmServerSet != null) && (!FmServerSet.IsDisposed))//杨斌 2020-05-17
FmServerSet.SessionChange(session, isOnline);
}
private static string RosterMeetFile
{
get
{
return AppDomain.CurrentDomain.BaseDirectory + "RosterMeet.dat";
}
}
public static void SaveRosterMeet()
{
//if (DicRosterMeet.Count < 1)
// return;
if (DicRosterMeet == null)
{
LoadRosterMeet();
return;
}
string data = MyJson.Serialize(DicRosterMeet);
System.IO.File.WriteAllText(RosterMeetFile, data);
}
public static void LoadRosterMeet()
{
if (System.IO.File.Exists(RosterMeetFile))
{
string data = System.IO.File.ReadAllText(RosterMeetFile);
DicRosterMeet = MyJson.DisSerialize<Dictionary<string, List<List<string>>>>(data);
}
if (DicRosterMeet == null)
DicRosterMeet = new Dictionary<string, List<List<string>>>();
}
public static void SendMsg(Dictionary<string, object> data)
{
if (DicSessionID.Count < 1)
return;
string msg = MyJson.Serialize(data);
NServer.Send(msg);
}
public static void ClearVote()
{
Dictionary<string, object> dicSend = new Dictionary<string, object>();
dicSend.Add("ClearVote", "");
SendMsg(dicSend);
}
/// <summary>
/// 杨斌 2019-05-10
/// </summary>
public static void SendVoteData()
{
foreach (var v in DicSessionName)
{
SendVoteData(v.Value);
}
}
/// <summary>
/// 杨斌 2019-05-10
/// </summary>
public static void SendVoteData(NetSession session)
{
string meetName = GetSessionPar(session, KeyName);
int meetNo = GetMeetNo(meetName);
string meetNoStr = meetNo + "F";
Dictionary<string, object> dicSend = new Dictionary<string, object>();
string data = "";
List<Dictionary<string, object>> lstData = new List<Dictionary<string, object>>();
for (int i = 0; i < GlobalInfo.response.ResponseDataList.Count; i++)
{
var v = GlobalInfo.response.ResponseDataList[i];
if (v.KeyID.IndexOf(meetNoStr) == 0)
{
Dictionary<string, object> dicData = new Dictionary<string, object>();
dicData.Add("KeyID", v.KeyID);
dicData.Add("KeyValue", v.KeyValue);
dicData.Add("Speed", v.Speed);
dicData.Add("Time", v.Time);
lstData.Add(dicData);
}
}
data = MyJson.Serialize(lstData);
dicSend.Add("SendVoteData", "");
dicSend.Add("VoteData", data);
string msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
}
private static Dictionary<string, object> DicVotePar = new Dictionary<string, object>();
/// <summary>
/// 模拟投票,测试用
/// </summary>
public static void StartVote(bool isStart)
{
DicVotePar = new Dictionary<string, object>();
int itemCount = 10;
DicVotePar.Add("VoteType", "Choices");
DicVotePar.Add("ItemCount", itemCount);//专用来统计选项个数,比如图表刷新
if (isStart)
{
DicVotePar.Add(KeyStartVote, "Choices");
DicVotePar.Add("ChoicesCount", itemCount);
DicVotePar.Add("ChoicesLimit", 1);
DicVotePar.Add("OptionsMode", 0);//0ABC,1数字
DicVotePar.Add("ChoicesLessEnabled", 0);
DicVotePar.Add("VoteModifyMode", 1);
DicVotePar.Add("VoteSecrecyMode", 0);
}
else
{
DicVotePar.Add(KeyStartVote, "Stop");
}
SendMsg(DicVotePar);
}
private static string GetVoteState(Dictionary<string, object> dic)//2017-12-08有问题,ServerSet增加加载保存清除
{
string voteType = "Stop";
if (DicVotePar.ContainsKey(KeyStartVote))
voteType = DicVotePar[KeyStartVote] + "";
foreach (var v in DicVotePar)
{
dic.Add(v.Key, v.Value);
}
return voteType;
}
public static void SendData(byte[] data, NetSession session = null)
{
if (session == null)
NServer.Send(data);
else
NServer.Send(data, session.ID);
}
private static void NServer_SessionClosed(NetSession session, NetLib.CloseReason reason)
{
if (DicSessionID.ContainsKey(session.ID))
DicSessionID.Remove(session.ID);
AddLog("Session Close", session);//日志
SessionChange(session);
}
static System.Timers.Timer TmrCheckOn = null;//杨斌 2020-05-17
static Dictionary<string, DateTime> DicLastMsg = new Dictionary<string, DateTime>();//杨斌 2020-05-17
private static void NServer_MessageReceived(NetSession session, string value)
{
//在ARS中解析:名单数据保存到库,按键数据到缓存
//if ((FmServerSet != null) && (!FmServerSet.IsDisposed))//杨斌 2020-05-17
// FmServerSet.ShowMsg(session, value);
string meetName = GetSessionPar(session, KeyName);
int meetNo = GetMeetNo(meetName);
string meetNoStr = meetNo + "F";
Dictionary<string, object> dic = MyJson.DisSerialize<Dictionary<string, object>>(value);
string sendType = GetDicDataPar(dic, KeySendType);
if (sendType == "BeatHeart")//需回应。杨斌 2020-05-17
{
Dictionary<string, object> dicSend = new Dictionary<string, object>();
dicSend.Add(KeySendType, "BeatHeartAck");
string msg = MyJson.Serialize(dicSend);
NServer.Send(msg, session.ID);
}
if ((FmServerSet != null) && (!FmServerSet.IsDisposed))//杨斌 2020-05-17
FmServerSet.SessionChange(session, true);
if (sendType == "SendRoster")
{
List<List<string>> lsTable = new List<List<string>>();
foreach (var v in dic)
{
if (v.Key != KeySendType)
{
lsTable.Add(MyJson.DisSerialize<List<string>>(v.Value + ""));
}
}
if (RosterList.RosterLoad == null)
{
RosterList.RosterLoad = new RosterList();
RosterList.RosterLoad.LoadRoster();
}
if (lsTable.Count > 0)
{
RosterList.RosterLoad.RosterAddMeet(lsTable, meetNo + "", meetName);
if ((FmServerSet != null) && (!FmServerSet.IsDisposed))//杨斌 2020-05-17
FmServerSet.ShowSessionRosterCount(meetName);
}
//杨斌 2019-05-14
//if (Globals.SunVoteARSAddIn.PPTShow.IsShowSlide)//杨斌 2019-05-10
SendVoteData(session);
Globals.SunVoteARSAddIn.PPTShow.SendSlideScreen(null, session);
}
else if (sendType == "SendVote")
{
ResponsePar pa = new ResponsePar();
pa.KeyID = meetNoStr + GetDicDataPar(dic, "KeyID");
pa.KeyValue = GetDicDataPar(dic, "KeyValue");
pa.Speed = ConvertOper.Convert(GetDicDataPar(dic, "Speed")).ToDouble;
DateTime time;
if (DateTime.TryParse(GetDicDataPar(dic, "Time"), out time))
pa.Time = time;
GlobalInfo.response.Busines_ResponseEventHander(pa);
Globals.SunVoteARSAddIn.PPTShow.SendSlideScreenTimer();//杨斌 2017-12-10
//lblRefreshVoteStateButton.Text = (ConvertOper.Convert(lblRefreshVoteStateButton.Text).ToInt + 1) + "";
}
DicLastMsg[session.ID] = DateTime.Now;
}
/// <summary>
/// 杨斌 2020-05-18
/// </summary>
private static void TmrCheckOn_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
foreach(var v in DicSessionID)
{
if (DicLastMsg.ContainsKey(v.Key))
{
if ((DateTime.Now - DicLastMsg[v.Key]).TotalSeconds >= 2)
{
if ((FmServerSet != null) && (!FmServerSet.IsDisposed))
FmServerSet.SessionChange(v.Value, false);
}
}
}
}
private static void NServer_DataReceived(NetSession session, byte[] value)
{
}
/// <summary>
/// 杨斌 2020-05-17
/// </summary>
private static void NServer_SessionErr(NetSession session, Exception ex)
{
if ((FmServerSet != null) && (!FmServerSet.IsDisposed))
FmServerSet.SessionChange(session, false);
}
public static int GetMeetNo(string meetName)
{
int res = 0;
if (DicSessionNameNo.ContainsKey(meetName))
res = DicSessionNameNo[meetName];
return res;
}
}
}