-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.Connection.cs
More file actions
122 lines (97 loc) · 3.13 KB
/
Copy pathServer.Connection.cs
File metadata and controls
122 lines (97 loc) · 3.13 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
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace SoS;
public abstract partial class Server
{
public abstract class Connection : IDisposable
{
internal Server? Server = null;
internal EndPoint? EndPoint = null;
/// <summary>
/// Send the specified <paramref name="data"/> to the client
/// </summary>
/// <param name="data"></param>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="NullReferenceException"></exception>
public void Send(ReadOnlySpan<byte> data)
{
if (Disposed)
throw new ObjectDisposedException(GetType().FullName);
lock (Server ?? throw new NullReferenceException(nameof(Server)))
lock (EndPoint ?? throw new NullReferenceException(nameof(EndPoint)))
Server.SendTo(EndPoint, Method.Data, data);
}
/// <summary>
/// Sends a disconnect notice and closes the connection
/// </summary>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="NullReferenceException"></exception>
public void Disconnect()
{
if (Disposed)
throw new ObjectDisposedException(GetType().FullName);
lock (Server ?? throw new NullReferenceException(nameof(Server)))
lock (EndPoint ?? throw new NullReferenceException(nameof(EndPoint)))
Server.SendTo(EndPoint, Method.Disconnect);
Close();
}
/// <summary>
/// Closes the connection
/// </summary>
/// <exception cref="ObjectDisposedException"></exception>
/// <exception cref="NullReferenceException"></exception>
public void Close()
{
if (Disposed)
throw new ObjectDisposedException(GetType().FullName);
KeepaliveTokenSource?.Cancel();
IPEndPoint ipEndPoint;
lock (EndPoint ?? throw new NullReferenceException(nameof(EndPoint)))
ipEndPoint = (IPEndPoint)EndPoint;
lock (Server ?? throw new NullReferenceException(nameof(Server)))
Server.RemoveConnection(new IPPort(ipEndPoint.Address, ipEndPoint.Port));
OnClose();
}
private bool Disposed = false;
public void Dispose()
{
if (Disposed)
return;
KeepaliveTokenSource?.Dispose();
Disposed = true;
GC.SuppressFinalize(this);
}
/// <summary>
/// Called when data is received from the client
/// </summary>
/// <param name="data"></param>
protected internal abstract void OnReceive(ReadOnlySpan<byte> data);
/// <summary>
/// Called when the client disconnect notice is received
/// </summary>
protected internal abstract void OnDisconnect();
/// <summary>
/// Called when the client times out
/// </summary>
protected abstract void OnTimeout();
/// <summary>
/// Called when the connection is closed
/// </summary>
protected abstract void OnClose();
private CancellationTokenSource? KeepaliveTokenSource;
internal async void KeepaliveTimeout()
{
KeepaliveTokenSource?.Cancel();
CancellationTokenSource cancellationTokenSource = new();
KeepaliveTokenSource = cancellationTokenSource;
try { await Task.Delay(10000, cancellationTokenSource.Token); }
catch (OperationCanceledException) { }
if (cancellationTokenSource.IsCancellationRequested)
return;
OnTimeout();
Close();
}
}
}