Skip to content

Commit dae48a7

Browse files
committed
[修改] WebSocket.Server 模块增加 XML 文档注释,优化子协议处理器、压缩扩展工厂和 WebSocket 主机构建器
1 parent dbcafd9 commit dae48a7

10 files changed

Lines changed: 307 additions & 92 deletions

src/GameFrameX.SuperSocket.WebSocket.Server/CommandSubProtocolHandler.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55

66
namespace GameFrameX.SuperSocket.WebSocket.Server;
77

8-
internal sealed class CommandSubProtocolHandler<TPackageInfo> : SubProtocolHandlerBase
8+
/// <summary>
9+
/// Handles WebSocket sub-protocol commands.
10+
/// </summary>
11+
/// <typeparam name="TPackageInfo">The type of the package information.</typeparam>
12+
sealed class CommandSubProtocolHandler<TPackageInfo> : SubProtocolHandlerBase
913
{
10-
private readonly IPackageHandler<WebSocketPackage> _commandMiddleware;
14+
private IPackageHandler<WebSocketPackage> _commandMiddleware;
1115

16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="CommandSubProtocolHandler{TPackageInfo}"/> class.
18+
/// </summary>
19+
/// <param name="name">The name of the sub-protocol.</param>
20+
/// <param name="serviceProvider">The service provider.</param>
21+
/// <param name="commandOptions">The command options.</param>
22+
/// <param name="mapper">The package mapper.</param>
1223
public CommandSubProtocolHandler(string name, IServiceProvider serviceProvider, IOptions<CommandOptions> commandOptions, IPackageMapper<WebSocketPackage, TPackageInfo> mapper)
1324
: base(name)
1425
{
@@ -17,6 +28,13 @@ public CommandSubProtocolHandler(string name, IServiceProvider serviceProvider,
1728
_commandMiddleware = Activator.CreateInstance(commandMiddlewareType, serviceProvider, commandOptions, mapper) as IPackageHandler<WebSocketPackage>;
1829
}
1930

31+
/// <summary>
32+
/// Handles a WebSocket package for the sub-protocol.
33+
/// </summary>
34+
/// <param name="session">The session associated with the package.</param>
35+
/// <param name="package">The WebSocket package.</param>
36+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
37+
/// <returns>A task that represents the asynchronous handling operation.</returns>
2038
public override async ValueTask Handle(IAppSession session, WebSocketPackage package, CancellationToken cancellationToken)
2139
{
2240
await _commandMiddleware.Handle(session, package, cancellationToken);

src/GameFrameX.SuperSocket.WebSocket.Server/DelegateSubProtocolHandler.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@
22

33
namespace GameFrameX.SuperSocket.WebSocket.Server;
44

5-
internal class DelegateSubProtocolHandler : SubProtocolHandlerBase
5+
/// <summary>
6+
/// Handles WebSocket sub-protocols using delegates.
7+
/// </summary>
8+
class DelegateSubProtocolHandler : SubProtocolHandlerBase
69
{
7-
private readonly Func<WebSocketSession, WebSocketPackage, CancellationToken, ValueTask> _packageHandler;
10+
private Func<WebSocketSession, WebSocketPackage, CancellationToken, ValueTask> _packageHandler;
811

12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="DelegateSubProtocolHandler"/> class with a delegate that does not use a cancellation token.
14+
/// </summary>
15+
/// <param name="name">The name of the sub-protocol.</param>
16+
/// <param name="packageHandler">The delegate to handle the package.</param>
917
public DelegateSubProtocolHandler(string name, Func<WebSocketSession, WebSocketPackage, ValueTask> packageHandler)
1018
: base(name)
1119
{
1220
_packageHandler = (session, package, cancellationToken) => packageHandler(session, package);
1321
}
1422

23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="DelegateSubProtocolHandler"/> class with a delegate that uses a cancellation token.
25+
/// </summary>
26+
/// <param name="name">The name of the sub-protocol.</param>
27+
/// <param name="packageHandler">The delegate to handle the package.</param>
1528
public DelegateSubProtocolHandler(string name, Func<WebSocketSession, WebSocketPackage, CancellationToken, ValueTask> packageHandler)
1629
: base(name)
1730
{
1831
_packageHandler = packageHandler;
1932
}
2033

34+
/// <summary>
35+
/// Handles a WebSocket package for the sub-protocol.
36+
/// </summary>
37+
/// <param name="session">The session associated with the package.</param>
38+
/// <param name="package">The WebSocket package.</param>
39+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
40+
/// <returns>A task that represents the asynchronous handling operation.</returns>
2141
public override async ValueTask Handle(IAppSession session, WebSocketPackage package, CancellationToken cancellationToken)
2242
{
2343
await _packageHandler(session as WebSocketSession, package, cancellationToken);

src/GameFrameX.SuperSocket.WebSocket.Server/Extensions/Compression/WebSocketPerMessageCompressionExtensionFactory.cs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,53 @@
44

55
namespace GameFrameX.SuperSocket.WebSocket.Server.Extensions.Compression;
66

7-
/// <summary>
8-
/// WebSocket Per-Message Compression Extension
9-
/// https://tools.ietf.org/html/rfc7692
10-
/// </summary>
11-
public class WebSocketPerMessageCompressionExtensionFactory : IWebSocketExtensionFactory
12-
{
13-
private static readonly NameValueCollection _supportedOptions;
14-
15-
static WebSocketPerMessageCompressionExtensionFactory()
7+
/// <summary>
8+
/// WebSocket Per-Message Compression Extension
9+
/// https://tools.ietf.org/html/rfc7692
10+
/// </summary>
11+
/// <summary>
12+
/// Represents a factory for creating WebSocket Per-Message Compression extensions.
13+
/// </summary>
14+
/// <remarks>
15+
/// Implements the WebSocket Per-Message Compression Extension as defined in RFC 7692.
16+
/// </remarks>
17+
public class WebSocketPerMessageCompressionExtensionFactory : IWebSocketExtensionFactory
1618
{
17-
_supportedOptions = new NameValueCollection();
18-
_supportedOptions.Add("client_no_context_takeover", string.Empty);
19-
}
19+
/// <summary>
20+
/// Gets the name of the WebSocket extension.
21+
/// </summary>
22+
public string Name => WebSocketPerMessageCompressionExtension.PMCE;
2023

21-
public string Name
22-
{
23-
get { return WebSocketPerMessageCompressionExtension.PMCE; }
24-
}
24+
private static readonly NameValueCollection _supportedOptions;
2525

26-
public IWebSocketExtension Create(NameValueCollection options, out NameValueCollection supportedOptions)
27-
{
28-
supportedOptions = _supportedOptions;
26+
static WebSocketPerMessageCompressionExtensionFactory()
27+
{
28+
_supportedOptions = new NameValueCollection();
29+
_supportedOptions.Add("client_no_context_takeover", string.Empty);
30+
}
2931

30-
if (options != null && options.Count > 0)
32+
/// <summary>
33+
/// Creates a WebSocket Per-Message Compression extension based on the specified options.
34+
/// </summary>
35+
/// <param name="options">The options for the extension.</param>
36+
/// <param name="supportedOptions">The supported options for the extension.</param>
37+
/// <returns>The created WebSocket extension, or null if the options are invalid.</returns>
38+
public IWebSocketExtension Create(NameValueCollection options, out NameValueCollection supportedOptions)
3139
{
32-
foreach (var key in options.AllKeys)
40+
supportedOptions = _supportedOptions;
41+
42+
if (options != null && options.Count > 0)
3343
{
34-
if (key.StartsWith("server_", StringComparison.OrdinalIgnoreCase))
44+
foreach (var key in options.AllKeys)
3545
{
36-
if (!string.IsNullOrEmpty(options.Get(key)))
37-
return null;
46+
if (key.StartsWith("server_", StringComparison.OrdinalIgnoreCase))
47+
{
48+
if (!string.IsNullOrEmpty(options.Get(key)))
49+
return null;
50+
}
3851
}
3952
}
40-
}
4153

42-
return new WebSocketPerMessageCompressionExtension();
54+
return new WebSocketPerMessageCompressionExtension();
55+
}
4356
}
44-
}

src/GameFrameX.SuperSocket.WebSocket.Server/Extensions/IWebSocketExtensionFactory.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33

44
namespace GameFrameX.SuperSocket.WebSocket.Server.Extensions;
55

6+
/// <summary>
7+
/// Defines a factory for creating WebSocket extensions.
8+
/// </summary>
69
public interface IWebSocketExtensionFactory
710
{
811
/// <summary>
9-
/// The name of the WebSocket extension.
12+
/// Gets the name of the WebSocket extension.
1013
/// </summary>
1114
string Name { get; }
1215

1316
/// <summary>
14-
/// Creates a new instance of the WebSocket extension.
17+
/// Creates a WebSocket extension based on the specified options.
1518
/// </summary>
16-
/// <param name="options">The options for the WebSocket extension.</param>
17-
/// <param name="supportedOptions">The supported options for the WebSocket extension.</param>
19+
/// <param name="options">The options for the extension.</param>
20+
/// <param name="supportedOptions">The supported options for the extension.</param>
1821
/// <returns>The created WebSocket extension.</returns>
1922
IWebSocketExtension Create(NameValueCollection options, out NameValueCollection supportedOptions);
2023
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
namespace GameFrameX.SuperSocket.WebSocket.Server;
22

3+
/// <summary>
4+
/// Represents options for WebSocket handshake operations.
5+
/// </summary>
36
public class HandshakeOptions
47
{
58
/// <summary>
6-
/// Handshake queue checking interval, in seconds
9+
/// Gets or sets the handshake queue checking interval, in seconds.
710
/// </summary>
8-
/// <value>default: 60</value>
11+
/// <value>Default: 60 seconds.</value>
912
public int CheckingInterval { get; set; } = 60;
1013

1114
/// <summary>
12-
/// Open handshake timeout, in seconds
15+
/// Gets or sets the open handshake timeout, in seconds.
1316
/// </summary>
14-
/// <value>default: 120</value>
17+
/// <value>Default: 120 seconds.</value>
1518
public int OpenHandshakeTimeOut { get; set; } = 120;
1619

1720
/// <summary>
18-
/// Close handshake timeout, in seconds
21+
/// Gets or sets the close handshake timeout, in seconds.
1922
/// </summary>
20-
/// <value>default: 120</value>
23+
/// <value>Default: 120 seconds.</value>
2124
public int CloseHandshakeTimeOut { get; set; } = 120;
2225

23-
2426
/// <summary>
25-
/// Gets or sets the handshake validator.
27+
/// Gets or sets the validator function for WebSocket handshakes.
2628
/// </summary>
2729
public Func<WebSocketSession, WebSocketPackage, ValueTask<bool>> HandshakeValidator { get; set; }
2830
}

src/GameFrameX.SuperSocket.WebSocket.Server/ISubProtocolHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace GameFrameX.SuperSocket.WebSocket.Server;
44

5-
internal interface ISubProtocolHandler : IPackageHandler<WebSocketPackage>
5+
/// <summary>
6+
/// Defines a handler for WebSocket sub-protocols.
7+
/// </summary>
8+
interface ISubProtocolHandler : IPackageHandler<WebSocketPackage>
69
{
10+
/// <summary>
11+
/// Gets the name of the sub-protocol.
12+
/// </summary>
713
string Name { get; }
814
}

src/GameFrameX.SuperSocket.WebSocket.Server/SubProtocolHandlerBase.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22

33
namespace GameFrameX.SuperSocket.WebSocket.Server;
44

5-
internal abstract class SubProtocolHandlerBase : ISubProtocolHandler
5+
/// <summary>
6+
/// Serves as a base class for WebSocket sub-protocol handlers.
7+
/// </summary>
8+
abstract class SubProtocolHandlerBase : ISubProtocolHandler
69
{
10+
/// <summary>
11+
/// Gets the name of the sub-protocol.
12+
/// </summary>
13+
public string Name { get; private set; }
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="SubProtocolHandlerBase"/> class.
17+
/// </summary>
18+
/// <param name="name">The name of the sub-protocol.</param>
719
public SubProtocolHandlerBase(string name)
820
{
921
Name = name;
1022
}
1123

12-
public string Name { get; }
13-
24+
/// <summary>
25+
/// Handles a WebSocket package for the sub-protocol.
26+
/// </summary>
27+
/// <param name="session">The session associated with the package.</param>
28+
/// <param name="package">The WebSocket package.</param>
29+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
30+
/// <returns>A task that represents the asynchronous handling operation.</returns>
1431
public abstract ValueTask Handle(IAppSession session, WebSocketPackage package, CancellationToken cancellationToken);
1532
}

0 commit comments

Comments
 (0)