-
Notifications
You must be signed in to change notification settings - Fork 8
/
TcpNETServerBase.cs
127 lines (115 loc) · 4.31 KB
/
TcpNETServerBase.cs
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
using Tcp.NET.Server.Models;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Tcp.NET.Server.Handlers;
using PHS.Networking.Server.Events.Args;
using Tcp.NET.Server.Events.Args;
using PHS.Networking.Server.Enums;
using PHS.Networking.Server.Services;
using System;
using PHS.Networking.Server.Managers;
using System.Linq;
namespace Tcp.NET.Server
{
public abstract class TcpNETServerBase<T, U, V, W, X, Y, Z> :
CoreNetworkingServer<T, U, V, W, X, Y, Z>,
ICoreNetworkingServer<T, U, V, Z>
where T : TcpConnectionServerBaseEventArgs<Z>
where U : TcpMessageServerBaseEventArgs<Z>
where V : TcpErrorServerBaseEventArgs<Z>
where W : IParamsTcpServer
where X : TcpHandlerServerBase<T, U, V, W, Z>
where Y : ConnectionManager<Z>
where Z : ConnectionTcpServer
{
protected Timer _timerPing;
protected bool _isPingRunning;
public TcpNETServerBase(W parameters) : base(parameters)
{
}
public TcpNETServerBase(W parameters,
byte[] certificate,
string certificatePassword) : base(parameters, certificate, certificatePassword)
{
}
protected override void OnServerEvent(object sender, ServerEventArgs args)
{
if (_timerPing != null)
{
_timerPing.Dispose();
_timerPing = null;
}
if (_parameters.PingIntervalSec > 0)
{
switch (args.ServerEventType)
{
case ServerEventType.Start:
_timerPing = new Timer(OnTimerPingTick, args.CancellationToken, 100, 100);
break;
case ServerEventType.Stop:
break;
default:
break;
}
}
base.FireEvent(sender, args);
}
protected virtual void OnTimerPingTick(object state)
{
if (!_isPingRunning)
{
_isPingRunning = true;
Task.Run(async () =>
{
foreach (var connection in _connectionManager.GetAllConnections().Where(x => !x.Disposed && x.NextPing <= DateTime.UtcNow))
{
try
{
if (connection.HasBeenPinged)
{
await _handler.DisconnectConnectionAsync(connection, (CancellationToken)state, "No ping response - disconnected.").ConfigureAwait(false);
}
else
{
connection.HasBeenPinged = true;
await SendToConnectionAsync(_parameters.PingBytes, connection, (CancellationToken)state).ConfigureAwait(false);
connection.NextPing = DateTime.UtcNow.AddSeconds(_parameters.PingIntervalSec);
}
}
catch (Exception ex)
{
FireEvent(this, CreateErrorEventArgs(new TcpErrorServerBaseEventArgs<Z>
{
Connection = connection,
Exception = ex,
Message = ex.Message,
CancellationToken = (CancellationToken)state
}));
}
}
_isPingRunning = false;
});
}
}
protected abstract T CreateConnectionEventArgs(TcpConnectionServerBaseEventArgs<Z> args);
protected abstract U CreateMessageEventArgs(TcpMessageServerBaseEventArgs<Z> args);
protected abstract V CreateErrorEventArgs(TcpErrorServerBaseEventArgs<Z> args);
public override void Dispose()
{
if (_timerPing != null)
{
_timerPing.Dispose();
_timerPing = null;
}
base.Dispose();
}
public TcpListener Server
{
get
{
return _handler?.Server;
}
}
}
}