Skip to content

Commit 05ae2cf

Browse files
committed
[修改] Server.Abstractions 增加 XML 文档注释,新增 ConsoleConnection 监听器/工厂,扩展会话容器、主机构建器和中间件接口
1 parent 23606c6 commit 05ae2cf

39 files changed

Lines changed: 992 additions & 36 deletions

src/GameFrameX.SuperSocket.Server.Abstractions/AsyncEventHandler.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,32 @@
33

44
namespace GameFrameX.SuperSocket.Server.Abstractions
55
{
6+
/// <summary>
7+
/// Represents an asynchronous event handler.
8+
/// </summary>
9+
/// <param name="sender">The source of the event.</param>
10+
/// <param name="e">The event data.</param>
11+
/// <returns>A task that represents the asynchronous operation.</returns>
612
public delegate ValueTask AsyncEventHandler(object sender, EventArgs e);
713

14+
/// <summary>
15+
/// Represents an asynchronous event handler with a specific event argument type.
16+
/// </summary>
17+
/// <typeparam name="TEventArgs">The type of the event data.</typeparam>
18+
/// <param name="sender">The source of the event.</param>
19+
/// <param name="e">The event data.</param>
20+
/// <returns>A task that represents the asynchronous operation.</returns>
821
public delegate ValueTask AsyncEventHandler<TEventArgs>(object sender, TEventArgs e)
922
where TEventArgs : EventArgs;
1023

24+
/// <summary>
25+
/// Represents an asynchronous event handler with specific sender and event argument types.
26+
/// </summary>
27+
/// <typeparam name="TSender">The type of the event sender.</typeparam>
28+
/// <typeparam name="TEventArgs">The type of the event data.</typeparam>
29+
/// <param name="sender">The source of the event.</param>
30+
/// <param name="e">The event data.</param>
31+
/// <returns>A task that represents the asynchronous operation.</returns>
1132
public delegate ValueTask AsyncEventHandler<TSender, TEventArgs>(TSender sender, TEventArgs e)
1233
where TSender : class
1334
where TEventArgs : EventArgs;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using GameFrameX.SuperSocket.Connection;
2+
using SuperSocket.Connection;
3+
4+
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
5+
{
6+
/// <summary>
7+
/// Factory for creating console connections.
8+
/// </summary>
9+
public class ConsoleConnectionFactory : IConnectionFactory
10+
{
11+
private readonly ConnectionOptions _connectionOptions;
12+
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="ConsoleConnectionFactory"/> class.
15+
/// </summary>
16+
/// <param name="connectionOptions">The connection options.</param>
17+
public ConsoleConnectionFactory(ConnectionOptions connectionOptions)
18+
{
19+
_connectionOptions = connectionOptions;
20+
}
21+
22+
/// <summary>
23+
/// Creates a console connection.
24+
/// </summary>
25+
/// <param name="connection">The connection object (ignored for console connections).</param>
26+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
27+
/// <returns>A task that represents the asynchronous creation operation.</returns>
28+
public Task<IConnection> CreateConnection(object connection, CancellationToken cancellationToken)
29+
{
30+
var consoleConnection = new ConsoleConnection(_connectionOptions);
31+
return Task.FromResult<IConnection>(consoleConnection);
32+
}
33+
}
34+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using GameFrameX.SuperSocket.Connection;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
5+
{
6+
/// <summary>
7+
/// Listener for console connections that immediately creates a single connection.
8+
/// </summary>
9+
public class ConsoleConnectionListener : IConnectionListener
10+
{
11+
private readonly ILogger _logger;
12+
private bool _isRunning;
13+
private IConnection _consoleConnection;
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ConsoleConnectionListener"/> class.
17+
/// </summary>
18+
/// <param name="options">The options for the listener.</param>
19+
/// <param name="connectionFactory">The factory for creating connections.</param>
20+
/// <param name="logger">The logger for logging events.</param>
21+
public ConsoleConnectionListener(ListenOptions options, IConnectionFactory connectionFactory, ILogger logger)
22+
{
23+
Options = options;
24+
ConnectionFactory = connectionFactory;
25+
_logger = logger;
26+
}
27+
28+
/// <inheritdoc/>
29+
public ListenOptions Options { get; }
30+
31+
/// <inheritdoc/>
32+
public IConnectionFactory ConnectionFactory { get; }
33+
34+
/// <inheritdoc/>
35+
public bool IsRunning => _isRunning;
36+
37+
/// <inheritdoc/>
38+
public event NewConnectionAcceptHandler NewConnectionAccept;
39+
40+
/// <inheritdoc/>
41+
public bool Start()
42+
{
43+
if (_isRunning)
44+
return true;
45+
46+
try
47+
{
48+
_isRunning = true;
49+
50+
// Create the console connection immediately
51+
_ = Task.Run(async () =>
52+
{
53+
try
54+
{
55+
_consoleConnection = await ConnectionFactory.CreateConnection(null, default).ConfigureAwait(false);
56+
57+
if (_consoleConnection != null && NewConnectionAccept != null)
58+
{
59+
await NewConnectionAccept(Options, _consoleConnection).ConfigureAwait(false);
60+
}
61+
}
62+
catch (Exception ex)
63+
{
64+
_logger?.LogError(ex, "Failed to create console connection");
65+
}
66+
});
67+
68+
_logger?.LogInformation("Console connection listener started");
69+
return true;
70+
}
71+
catch (Exception ex)
72+
{
73+
_logger?.LogError(ex, "Failed to start console connection listener");
74+
_isRunning = false;
75+
return false;
76+
}
77+
}
78+
79+
/// <inheritdoc/>
80+
public async Task StopAsync()
81+
{
82+
if (!_isRunning)
83+
return;
84+
85+
_isRunning = false;
86+
87+
if (_consoleConnection != null)
88+
{
89+
try
90+
{
91+
await _consoleConnection.CloseAsync(CloseReason.ServerShutdown).ConfigureAwait(false);
92+
}
93+
catch (Exception ex)
94+
{
95+
_logger?.LogWarning(ex, "Error closing console connection during shutdown");
96+
}
97+
finally
98+
{
99+
_consoleConnection = null;
100+
}
101+
}
102+
103+
_logger?.LogInformation("Console connection listener stopped");
104+
}
105+
106+
/// <inheritdoc/>
107+
public void Dispose()
108+
{
109+
if (_isRunning)
110+
{
111+
StopAsync().GetAwaiter().GetResult();
112+
}
113+
}
114+
}
115+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using GameFrameX.SuperSocket.Connection;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
5+
{
6+
/// <summary>
7+
/// Factory for creating console connection listeners.
8+
/// </summary>
9+
public class ConsoleConnectionListenerFactory : IConnectionListenerFactory
10+
{
11+
/// <inheritdoc/>
12+
public IConnectionListener CreateConnectionListener(ListenOptions options, ConnectionOptions connectionOptions, ILoggerFactory loggerFactory)
13+
{
14+
connectionOptions.Logger = loggerFactory.CreateLogger(nameof(IConnection));
15+
var connectionListenerLogger = loggerFactory.CreateLogger(nameof(ConsoleConnectionListener));
16+
17+
var connectionFactory = new ConsoleConnectionFactory(connectionOptions);
18+
return new ConsoleConnectionListener(options, connectionFactory, connectionListenerLogger);
19+
}
20+
}
21+
}

src/GameFrameX.SuperSocket.Server.Abstractions/Connections/IConnectionFactoryBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
44
{
5+
/// <summary>
6+
/// Defines a builder for creating connection factories.
7+
/// </summary>
58
public interface IConnectionFactoryBuilder
69
{
10+
/// <summary>
11+
/// Builds a connection factory based on the specified listen and connection options.
12+
/// </summary>
13+
/// <param name="listenOptions">The options for the listener.</param>
14+
/// <param name="connectionOptions">The options for the connection.</param>
15+
/// <returns>A connection factory.</returns>
716
IConnectionFactory Build(ListenOptions listenOptions, ConnectionOptions connectionOptions);
817
}
918
}

src/GameFrameX.SuperSocket.Server.Abstractions/Connections/IConnectionListener.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,49 @@
22

33
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
44
{
5+
/// <summary>
6+
/// Represents a delegate that handles new connection accept events.
7+
/// </summary>
8+
/// <param name="listenOptions">The options for the listener that accepted the connection.</param>
9+
/// <param name="connection">The newly accepted connection.</param>
10+
/// <returns>A task that represents the asynchronous handling operation.</returns>
511
public delegate ValueTask NewConnectionAcceptHandler(ListenOptions listenOptions, IConnection connection);
612

13+
/// <summary>
14+
/// Represents a listener for incoming connections.
15+
/// </summary>
716
public interface IConnectionListener : IDisposable
817
{
18+
/// <summary>
19+
/// Gets the options for the listener.
20+
/// </summary>
921
ListenOptions Options { get; }
1022

23+
/// <summary>
24+
/// Starts the connection listener.
25+
/// </summary>
26+
/// <returns><c>true</c> if the listener started successfully; otherwise, <c>false</c>.</returns>
1127
bool Start();
1228

29+
/// <summary>
30+
/// Occurs when a new connection is accepted.
31+
/// </summary>
1332
event NewConnectionAcceptHandler NewConnectionAccept;
1433

34+
/// <summary>
35+
/// Stops the connection listener asynchronously.
36+
/// </summary>
37+
/// <returns>A task that represents the asynchronous stop operation.</returns>
1538
Task StopAsync();
1639

40+
/// <summary>
41+
/// Gets a value indicating whether the listener is running.
42+
/// </summary>
1743
bool IsRunning { get; }
1844

45+
/// <summary>
46+
/// Gets the factory for creating connections.
47+
/// </summary>
1948
IConnectionFactory ConnectionFactory { get; }
2049
}
2150
}

src/GameFrameX.SuperSocket.Server.Abstractions/Connections/IConnectionListenerFactory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33

44
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
55
{
6+
/// <summary>
7+
/// Defines a factory for creating connection listeners.
8+
/// </summary>
69
public interface IConnectionListenerFactory
710
{
11+
/// <summary>
12+
/// Creates a connection listener based on the specified options.
13+
/// </summary>
14+
/// <param name="options">The options for the listener.</param>
15+
/// <param name="connectionOptions">The options for the connection.</param>
16+
/// <param name="loggerFactory">The factory for creating loggers.</param>
17+
/// <returns>A connection listener.</returns>
818
IConnectionListener CreateConnectionListener(ListenOptions options, ConnectionOptions connectionOptions, ILoggerFactory loggerFactory);
919
}
1020
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using System;
2-
using Microsoft.Extensions.Logging;
3-
using GameFrameX.SuperSocket.ProtoBase;
4-
using System.Threading.Tasks;
5-
61
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
72
{
3+
/// <summary>
4+
/// Defines a register for managing connections.
5+
/// </summary>
86
public interface IConnectionRegister
97
{
8+
/// <summary>
9+
/// Registers a connection asynchronously.
10+
/// </summary>
11+
/// <param name="connection">The connection to register.</param>
12+
/// <returns>A task that represents the asynchronous registration operation.</returns>
1013
Task RegisterConnection(object connection);
1114
}
1215
}

src/GameFrameX.SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55

66
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
77
{
8+
/// <summary>
9+
/// Defines an initializer for connection streams.
10+
/// </summary>
811
public interface IConnectionStreamInitializer
912
{
13+
/// <summary>
14+
/// Sets up the initializer with the specified listen options.
15+
/// </summary>
16+
/// <param name="listenOptions">The options for the listener.</param>
1017
void Setup(ListenOptions listenOptions);
1118

19+
/// <summary>
20+
/// Initializes a connection stream asynchronously.
21+
/// </summary>
22+
/// <param name="socket">The socket associated with the connection.</param>
23+
/// <param name="stream">The existing stream to initialize.</param>
24+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
25+
/// <returns>A task that represents the asynchronous initialization operation.</returns>
1226
Task<Stream> InitializeAsync(Socket socket, Stream stream, CancellationToken cancellationToken);
1327
}
1428
}

src/GameFrameX.SuperSocket.Server.Abstractions/Connections/IConnectionStreamInitializersFactory.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
namespace GameFrameX.SuperSocket.Server.Abstractions.Connections
99
{
10+
/// <summary>
11+
/// Defines a factory for creating connection stream initializers.
12+
/// </summary>
1013
public interface IConnectionStreamInitializersFactory
1114
{
15+
/// <summary>
16+
/// Creates a collection of connection stream initializers based on the specified listen options.
17+
/// </summary>
18+
/// <param name="listenOptions">The options for the listener.</param>
19+
/// <returns>A collection of connection stream initializers.</returns>
1220
IEnumerable<IConnectionStreamInitializer> Create(ListenOptions listenOptions);
1321
}
1422
}

0 commit comments

Comments
 (0)