Skip to content

Commit 4f86d49

Browse files
committed
[修改] Server 模块增加 XML 文档注释,重命名 SessionID→SessionId,扩展 SuperSocketService、AppSession 和主机构建器
1 parent 05ae2cf commit 4f86d49

11 files changed

Lines changed: 744 additions & 109 deletions

src/GameFrameX.SuperSocket.Server/AppSession.cs

Lines changed: 102 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Buffers;
12
using System.Net;
23
using GameFrameX.SuperSocket.Connection;
34
using GameFrameX.SuperSocket.Primitives;
@@ -38,9 +39,13 @@ public AppSession()
3839
void IAppSession.Initialize(IServerInfo server, IConnection connection)
3940
{
4041
if (connection is IConnectionWithSessionIdentifier connectionWithSessionIdentifier)
41-
SessionID = connectionWithSessionIdentifier.SessionIdentifier;
42+
{
43+
SessionId = connectionWithSessionIdentifier.SessionIdentifier;
44+
}
4245
else
43-
SessionID = Guid.NewGuid().ToString();
46+
{
47+
SessionId = Guid.NewGuid().ToString();
48+
}
4449

4550
Server = server;
4651
StartTime = DateTimeOffset.Now;
@@ -51,30 +56,8 @@ void IAppSession.Initialize(IServerInfo server, IConnection connection)
5156
/// <summary>
5257
/// Gets the session ID.
5358
/// </summary>
54-
public string SessionID { get; private set; }
55-
59+
public string SessionId { get; private set; }
5660

57-
/// <summary>
58-
/// Sends binary data asynchronously.
59-
/// </summary>
60-
/// <param name="data">The binary data to send.</param>
61-
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
62-
/// <returns>A task that represents the asynchronous send operation.</returns>ly.
63-
public virtual ValueTask SendAsync(byte[] data, CancellationToken cancellationToken = default)
64-
{
65-
return _connection.SendAsync(data, cancellationToken);
66-
}
67-
68-
/// <summary>
69-
/// Sends binary data asynchronously.
70-
/// </summary>
71-
/// <param name="data">The binary data to send.</param>
72-
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
73-
/// <returns>A task that represents the asynchronous send operation.</returns>
74-
public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
75-
{
76-
return _connection.SendAsync(data, cancellationToken);
77-
}
7861

7962
/// <summary>
8063
/// Gets the start time of the session.
@@ -192,15 +175,20 @@ public object this[object name]
192175
}
193176

194177
/// <summary>
195-
/// Closes the session.
178+
/// Called when the session is closed.
196179
/// </summary>
197-
/// <param name="e"></param>
198-
/// <returns></returns>
180+
/// <param name="e">The close event arguments containing the reason for closing.</param>
181+
/// <returns>A task representing the async operation.</returns>
199182
protected virtual ValueTask OnSessionClosedAsync(CloseEventArgs e)
200183
{
201184
return new ValueTask();
202185
}
203186

187+
/// <summary>
188+
/// Fires the session closed event.
189+
/// </summary>
190+
/// <param name="e">The close event arguments containing the reason for closing.</param>
191+
/// <returns>A task representing the async operation.</returns>
204192
internal async ValueTask FireSessionClosedAsync(CloseEventArgs e)
205193
{
206194
State = SessionState.Closed;
@@ -216,11 +204,19 @@ internal async ValueTask FireSessionClosedAsync(CloseEventArgs e)
216204
}
217205

218206

207+
/// <summary>
208+
/// Called when the session is connected.
209+
/// </summary>
210+
/// <returns>A task representing the async operation.</returns>
219211
protected virtual ValueTask OnSessionConnectedAsync()
220212
{
221213
return new ValueTask();
222214
}
223215

216+
/// <summary>
217+
/// Fires the session connected event.
218+
/// </summary>
219+
/// <returns>A task representing the async operation.</returns>
224220
internal async ValueTask FireSessionConnectedAsync()
225221
{
226222
State = SessionState.Connected;
@@ -230,17 +226,62 @@ internal async ValueTask FireSessionConnectedAsync()
230226
var connectedEventHandler = Connected;
231227

232228
if (connectedEventHandler == null)
229+
{
233230
return;
231+
}
234232

235233
await connectedEventHandler.Invoke(this, EventArgs.Empty);
236234
}
237235

236+
/// <summary>
237+
/// Sends binary data asynchronously.
238+
/// </summary>
239+
/// <param name="data">The binary data to send.</param>
240+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
241+
/// <returns>A task that represents the asynchronous send operation.</returns>ly.
242+
public virtual ValueTask SendAsync(byte[] data, CancellationToken cancellationToken = default)
243+
{
244+
return _connection.SendAsync(data, cancellationToken);
245+
}
246+
247+
/// <summary>
248+
/// Sends binary data asynchronously.
249+
/// </summary>
250+
/// <param name="data">The binary data to send.</param>
251+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
252+
/// <returns>A task that represents the asynchronous send operation.</returns>
253+
public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
254+
{
255+
return _connection.SendAsync(data, cancellationToken);
256+
}
257+
258+
/// <summary>
259+
/// Sends a sequence of binary data asynchronously using the connection.
260+
/// </summary>
261+
/// <param name="data">The sequence of binary data to send.</param>
262+
/// <param name="cancellationToken">The token for canceling the operation.</param>
263+
/// <returns>A task representing the asynchronous send operation.</returns>
264+
public ValueTask SendAsync(ReadOnlySequence<byte> data, CancellationToken cancellationToken = default)
265+
{
266+
return _connection.SendAsync(data, cancellationToken);
267+
}
238268

239-
ValueTask IAppSession.SendAsync<TPackage>(IPackageEncoder<TPackage> packageEncoder, TPackage package, CancellationToken cancellationToken)
269+
/// <summary>
270+
/// Sends a package to the client asynchronously.
271+
/// </summary>
272+
/// <typeparam name="TPackage">The type of the package.</typeparam>
273+
/// <param name="packageEncoder">The encoder used to encode the package.</param>
274+
/// <param name="package">The package to send.</param>
275+
/// <param name="cancellationToken">The cancellation token.</param>
276+
/// <returns>A task representing the async send operation.</returns>
277+
public ValueTask SendAsync<TPackage>(IPackageEncoder<TPackage> packageEncoder, TPackage package, CancellationToken cancellationToken)
240278
{
241279
return _connection.SendAsync(packageEncoder, package, cancellationToken);
242280
}
243281

282+
/// <summary>
283+
/// Resets the session state. Called by the server when recycling the session.
284+
/// </summary>
244285
void IAppSession.Reset()
245286
{
246287
ClearEvent(ref Connected);
@@ -255,10 +296,18 @@ void IAppSession.Reset()
255296
Reset();
256297
}
257298

299+
/// <summary>
300+
/// Called when the session is reset. Derived classes can override this method to perform additional cleanup.
301+
/// </summary>
258302
protected virtual void Reset()
259303
{
260304
}
261305

306+
/// <summary>
307+
/// Clears all handlers from an event.
308+
/// </summary>
309+
/// <typeparam name="TEventHandler">The type of the event handler.</typeparam>
310+
/// <param name="sessionEvent">The event to clear.</param>
262311
private void ClearEvent<TEventHandler>(ref TEventHandler sessionEvent)
263312
where TEventHandler : Delegate
264313
{
@@ -308,23 +357,45 @@ public virtual async ValueTask CloseAsync(CloseReason reason)
308357
#region ILogger
309358

310359
/// <summary>
311-
/// Gets the logger associated with the session.
360+
/// Gets the logger associated with the session from the server.
312361
/// </summary>
362+
/// <returns>The logger instance.</returns>
313363
ILogger GetLogger()
314364
{
315365
return (Server as ILoggerAccessor).Logger;
316366
}
317367

368+
/// <summary>
369+
/// Writes a log entry with the specified log level, event ID, state, exception, and formatter.
370+
/// Prefixes log entries with the session ID.
371+
/// </summary>
372+
/// <typeparam name="TState">The type of the object to be written.</typeparam>
373+
/// <param name="logLevel">The level of the log entry.</param>
374+
/// <param name="eventId">The event ID for the log entry.</param>
375+
/// <param name="state">The state to be logged.</param>
376+
/// <param name="exception">The exception related to this entry.</param>
377+
/// <param name="formatter">The function to format the state and exception into a log message.</param>
318378
void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
319379
{
320-
GetLogger().Log<TState>(logLevel, eventId, state, exception, (s, e) => { return $"Session[{this.SessionID}]: {formatter(s, e)}"; });
380+
GetLogger().Log<TState>(logLevel, eventId, state, exception, (s, e) => { return $"Session[{this.SessionId}]: {formatter(s, e)}"; });
321381
}
322382

383+
/// <summary>
384+
/// Checks if the given log level is enabled.
385+
/// </summary>
386+
/// <param name="logLevel">The log level to check.</param>
387+
/// <returns>True if the log level is enabled; otherwise, false.</returns>
323388
bool ILogger.IsEnabled(LogLevel logLevel)
324389
{
325390
return GetLogger().IsEnabled(logLevel);
326391
}
327392

393+
/// <summary>
394+
/// Begins a logical operation scope.
395+
/// </summary>
396+
/// <typeparam name="TState">The type of the state to begin scope for.</typeparam>
397+
/// <param name="state">The identifier for the scope.</param>
398+
/// <returns>An IDisposable that ends the logical operation scope on dispose.</returns>
328399
IDisposable ILogger.BeginScope<TState>(TState state)
329400
{
330401
return GetLogger().BeginScope<TState>(state);

src/GameFrameX.SuperSocket.Server/ClearIdleSessionMiddleware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ private void OnTimerCallback(object state)
6363
try
6464
{
6565
s.Connection.CloseAsync(CloseReason.TimeOut);
66-
_logger.LogWarning($"Close the idle session {s.SessionID}, it's LastActiveTime is {s.LastActiveTime}.");
66+
_logger.LogWarning($"Close the idle session {s.SessionId}, it's LastActiveTime is {s.LastActiveTime}.");
6767
}
6868
catch (Exception exc)
6969
{
70-
_logger.LogError(exc, $"Error happened when close the session {s.SessionID} for inactive for a while.");
70+
_logger.LogError(exc, $"Error happened when close the session {s.SessionId} for inactive for a while.");
7171
}
7272
}
7373
}

src/GameFrameX.SuperSocket.Server/GenericSessionFactory.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ namespace GameFrameX.SuperSocket.Server
77
/// A generic session factory for creating instances of a specific session type.
88
/// </summary>
99
/// <typeparam name="TSession">The type of session to create.</typeparam>
10-
public class GenericSessionFactory<TSession> : ISessionFactory where TSession : IAppSession
10+
public class GenericSessionFactory<TSession> : ISessionFactory
11+
where TSession : IAppSession
1112
{
1213
/// <summary>
1314
/// Gets the type of session created by this factory.
1415
/// </summary>
15-
public Type SessionType
16-
{
17-
get { return typeof(TSession); }
18-
}
16+
public Type SessionType => typeof(TSession);
1917

2018
/// <summary>
2119
/// Gets the service provider used to create session instances.

0 commit comments

Comments
 (0)