Repository navigation
Expand file tree
/
Copy pathINetworkClient.cs
More file actions
172 lines (154 loc) · 8.53 KB
/
Copy pathINetworkClient.cs
File metadata and controls
172 lines (154 loc) · 8.53 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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
namespace SS.Core.ComponentInterfaces
{
/// <summary>
/// Interface of a service that handles events for a client connection.
/// </summary>
public interface IClientConnectionHandler
{
/// <summary>
/// Processes after the connection has been established (encryption handshake completed).
/// </summary>
void Connected();
/// <summary>
/// Processes incoming data.
/// </summary>
/// <param name="data">The buffer containing the packet data that was received.</param>
/// <param name="flags">Flags indicating how the data was received.</param>
void HandlePacket(Span<byte> data, NetReceiveFlags flags);
/// <summary>
/// Processes when the connection has been disconnected.
/// </summary>
void Disconnected();
}
/// <summary>
/// Interface for a connection that is acting as a client communicating to a server using the Subspace 'core' protocol.
/// </summary>
public interface IClientConnection
{
IClientConnectionHandler Handler { get; }
#region Extra Data methods
bool TryAddExtraData<T>(T value) where T : class;
bool TryGetExtraData<T>([MaybeNullWhen(false)] out T value) where T : class;
bool TryRemoveExtraData<T>([MaybeNullWhen(false)] out T value) where T : class;
#endregion
}
/// <summary>
/// Interface for a service that can encrypt and decrypt data for a client connection.
/// </summary>
public interface IClientEncrypt : IComponentInterface
{
/// <summary>
/// Initializes encryption for a connection.
/// </summary>
/// <param name="connection">The client connection to initialize.</param>
void Initialize(IClientConnection connection);
/// <summary>
/// Encrypts data for a connection, in place.
/// </summary>
/// <param name="connection">The client connection to encrypt data for.</param>
/// <param name="data">A buffer containing the data to encrypt.</param>
/// <param name="length">The # of bytes in <paramref name="data"/> to encrypt.</param>
/// <returns></returns>
int Encrypt(IClientConnection connection, Span<byte> data, int length);
/// <summary>
/// Decrypts data for a connection, in place.
/// </summary>
/// <param name="connection">The client connection to decrypt data for.</param>
/// <param name="data">A buffer containing the data to decrypt.</param>
/// <param name="length">The # of bytes in <paramref name="data"/> to decrypt.</param>
/// <returns></returns>
int Decrypt(IClientConnection connection, Span<byte> data, int length);
/// <summary>
/// Performs cleanup of encryption resources for a connection.
/// </summary>
/// <remarks>
/// This is called when a connection being disconnected.
/// </remarks>
/// <param name="connection">The client connection to perform cleanup on.</param>
void Void(IClientConnection connection);
}
/// <summary>
/// Delegate for a callback when the send of reliable data completes.
/// </summary>
/// <param name="connection">The connection the data was being sent to.</param>
/// <param name="success">Whether the data was sucessfully sent. <see langword="true"/> if an ACK was received. <see langword="false"/> if the send was cancelled out.</param>
public delegate void ClientReliableCallback(IClientConnection connection, bool success);
/// <summary>
/// Interface of a service that provides the ability to act as a client communicating to a server using the Subspace 'core' protocol.
/// </summary>
/// <remarks>
/// <para>
/// The <see cref="Modules.BillingUdp"> module uses this to act as a client to a billing server.
/// </para>
/// <para>
/// The <see cref="Modules.Network"/> module implements this interface since it contains the logic of communicating using the Subspace 'core' protocol.
/// </para>
/// </remarks>
public interface INetworkClient : IComponentInterface
{
/// <summary>
/// Opens a new client connection to a server.
/// </summary>
/// <param name="endPoint">The IP address and port of the server to connect to.</param>
/// <param name="handler">
/// The handler for the connection that will handle what to do when it's connected, data is received, or it's disconnected.
/// <para>All calls to the handler are guaranteed to be executed on the mainloop thread.</para>
/// </param>
/// <param name="encryptorName">The name of the encryptor (<see cref="IClientEncrypt"/>) to use for the connection.</param>
/// <param name="bandwidthLimiterProviderName">The name of the bandwidth limiter provider (<see cref="IBandwidthLimiterProvider"/>) to use for the connection.</param>
/// <returns>The connection if one was created. <see langword="null"/> if there was a problem.</returns>
/// <exception cref="ArgumentNullException"><paramref name="endPoint"/> is null.</exception>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="encryptorName"/> is null or white-space.</exception>
/// <exception cref="ArgumentException"><paramref name="bandwidthLimiterProviderName"/> is null or white-space.</exception>
IClientConnection? MakeClientConnection(IPEndPoint endPoint, IClientConnectionHandler handler, string encryptorName, string bandwidthLimiterProviderName);
/// <summary>
/// Sends data to the server.
/// </summary>
/// <param name="connection">The connection to send data to.</param>
/// <param name="data">The data to send.</param>
/// <param name="flags">Flags indicating how the data should be sent.</param>
/// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
void SendPacket(IClientConnection connection, ReadOnlySpan<byte> data, NetSendFlags flags);
/// <summary>
/// Sends data to the server.
/// </summary>
/// <typeparam name="T">The type of struct containing data to send.</typeparam>
/// <param name="connection">The connection to send data to.</param>
/// <param name="data">The data to send.</param>
/// <param name="flags">Flags indicating how the data should be sent.</param>
/// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
void SendPacket<T>(IClientConnection connection, ref T data, NetSendFlags flags) where T : struct;
/// <summary>
/// Reliably sends <paramref name="data"/> to a <paramref name="connection"/> and invokes a <paramref name="callback"/>
/// after receiving an ACK (successfully sent) or cancelling out (e.g. disconnect, lagout, etc.).
/// </summary>
/// <remarks>
/// The <paramref name="callback"/> is executed on the mainloop thread.
/// </remarks>
/// <param name="connection">The connection to send data to.</param>
/// <param name="data">The data to send.</param>
/// <param name="callback">The callback to invoke after the data has been sent.</param>
/// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
void SendWithCallback(IClientConnection connection, ReadOnlySpan<byte> data, ClientReliableCallback callback);
/// <summary>
/// Initiates the disconnection of a connection.
/// </summary>
/// <remarks>
/// When the disconnection completes, <see cref="IClientConnectionHandler.Disconnected"/> will be invoked.
/// </remarks>
/// <param name="connection">The connection to disconnect.</param>
/// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
void DropConnection(IClientConnection connection);
/// <summary>
/// Gets statistics about the connection.
/// </summary>
/// <param name="connection">The connection to get stats about.</param>
/// <param name="stats">The object to populate with statistics.</param>
/// <exception cref="ArgumentNullException"><paramref name="connection"/> is null.</exception>
void GetConnectionStats(IClientConnection connection, ref NetConnectionStats stats);
}
}