Skip to content

Commit 0816b47

Browse files
committed
[修改] Connection 模块增加 XML 文档注释,扩展连接接口(增加 SendAsync 重载、Dispose 等),新增 ConsoleConnection 和 CloseReason.Rejected
1 parent 0ffa02e commit 0816b47

22 files changed

Lines changed: 640 additions & 71 deletions

src/GameFrameX.SuperSocket.Connection/CloseReason.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
11
namespace GameFrameX.SuperSocket.Connection
22
{
33
/// <summary>
4-
/// 关闭原因
4+
/// Specifies the reasons for closing a connection.
55
/// </summary>
66
public enum CloseReason
77
{
88
/// <summary>
9-
/// The socket is closed for unknown reason
9+
/// The socket is closed for an unknown reason.
1010
/// </summary>
1111
Unknown = 0,
1212

1313
/// <summary>
14-
/// Close for server shutdown
14+
/// The connection is closed due to server shutdown.
1515
/// </summary>
1616
ServerShutdown = 1,
1717

1818
/// <summary>
19-
/// The close behavior is initiated from the remote endpoint
19+
/// The connection is closed by the remote endpoint.
2020
/// </summary>
2121
RemoteClosing = 2,
2222

2323
/// <summary>
24-
/// The close behavior is initiated from the local endpoint
24+
/// The connection is closed by the local endpoint.
2525
/// </summary>
2626
LocalClosing = 3,
2727

2828
/// <summary>
29-
/// Application error
29+
/// The connection is closed due to an application error.
3030
/// </summary>
3131
ApplicationError = 4,
3232

3333
/// <summary>
34-
/// The socket is closed for a socket error
34+
/// The connection is closed due to a socket error.
3535
/// </summary>
3636
SocketError = 5,
3737

3838
/// <summary>
39-
/// The socket is closed by server for timeout
39+
/// The connection is closed by the server due to a timeout.
4040
/// </summary>
4141
TimeOut = 6,
4242

4343
/// <summary>
44-
/// Protocol error
44+
/// The connection is closed due to a protocol error.
4545
/// </summary>
4646
ProtocolError = 7,
4747

4848
/// <summary>
49-
/// SuperSocket internal error
49+
/// The connection is closed due to an internal error in SuperSocket.
5050
/// </summary>
5151
InternalError = 8,
52+
53+
/// <summary>
54+
/// The connection is closed because it was rejected.
55+
/// </summary>
56+
Rejected = 9
5257
}
58+
59+
/// <summary>
60+
/// Provides data for connection close events.
61+
/// </summary>
5362
public class CloseEventArgs : EventArgs
5463
{
64+
/// <summary>
65+
/// Gets the reason for the connection closure.
66+
/// </summary>
5567
public CloseReason Reason { get; private set; }
68+
69+
/// <summary>
70+
/// Initializes a new instance of the <see cref="CloseEventArgs"/> class with the specified close reason.
71+
/// </summary>
72+
/// <param name="reason">The reason for the connection closure.</param>
5673
public CloseEventArgs(CloseReason reason)
5774
{
5875
Reason = reason;

src/GameFrameX.SuperSocket.Connection/ConnectionBase.cs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,96 @@
1-
using System.IO.Pipelines;
1+
using System.Buffers;
2+
using System.IO.Pipelines;
23
using System.Net;
34
using GameFrameX.SuperSocket.ProtoBase;
45
using GameFrameX.SuperSocket.ProtoBase.ProxyProtocol;
56

67
namespace GameFrameX.SuperSocket.Connection
78
{
9+
/// <summary>
10+
/// Provides an abstract base class for connections, defining common connection functionality.
11+
/// </summary>
812
public abstract class ConnectionBase : IConnection
913
{
14+
/// <summary>
15+
/// Runs the connection asynchronously with the specified pipeline filter.
16+
/// </summary>
17+
/// <typeparam name="TPackageInfo">The type of the package information.</typeparam>
18+
/// <param name="pipelineFilter">The pipeline filter to use for processing data.</param>
19+
/// <returns>An asynchronous enumerable of package information.</returns>
1020
public abstract IAsyncEnumerable<TPackageInfo> RunAsync<TPackageInfo>(IPipelineFilter<TPackageInfo> pipelineFilter);
1121

22+
/// <summary>
23+
/// Sends data over the connection asynchronously using the specified buffer.
24+
/// </summary>
25+
/// <param name="buffer">The buffer containing the data to send.</param>
26+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
27+
/// <returns>A task that represents the asynchronous send operation.</returns>
1228
public abstract ValueTask SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default);
1329

30+
/// <summary>
31+
/// Sends data over the connection asynchronously using the specified buffer.
32+
/// </summary>
33+
/// <param name="buffer">The buffer containing the data to send.</param>
34+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
35+
/// <returns>A task that represents the asynchronous send operation.</returns>
36+
public abstract ValueTask SendAsync(ReadOnlySequence<byte> buffer, CancellationToken cancellationToken = default);
37+
38+
/// <summary>
39+
/// Sends a package over the connection asynchronously using the specified encoder and package.
40+
/// </summary>
41+
/// <typeparam name="TPackage">The type of the package to send.</typeparam>
42+
/// <param name="packageEncoder">The encoder to use for the package.</param>
43+
/// <param name="package">The package to send.</param>
44+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
45+
/// <returns>A task that represents the asynchronous send operation.</returns>
1446
public abstract ValueTask SendAsync<TPackage>(IPackageEncoder<TPackage> packageEncoder, TPackage package, CancellationToken cancellationToken = default);
1547

48+
/// <summary>
49+
/// Sends data over the connection asynchronously using a custom write action.
50+
/// </summary>
51+
/// <param name="write">The action to write data to the pipe writer.</param>
52+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
53+
/// <returns>A task that represents the asynchronous send operation.</returns>
1654
public abstract ValueTask SendAsync(Action<PipeWriter> write, CancellationToken cancellationToken = default);
1755

56+
/// <summary>
57+
/// Gets a value indicating whether the connection is closed.
58+
/// </summary>
1859
public bool IsClosed { get; private set; }
1960

61+
/// <summary>
62+
/// Gets the remote endpoint of the connection.
63+
/// </summary>
2064
public EndPoint RemoteEndPoint { get; protected set; }
2165

66+
/// <summary>
67+
/// Gets the local endpoint of the connection.
68+
/// </summary>
2269
public EndPoint LocalEndPoint { get; protected set; }
2370

71+
/// <summary>
72+
/// Gets the reason for the connection closure, if available.
73+
/// </summary>
2474
public CloseReason? CloseReason { get; protected set; }
2575

76+
/// <summary>
77+
/// Gets the last active time of the connection.
78+
/// </summary>
2679
public DateTimeOffset LastActiveTime { get; protected set; } = DateTimeOffset.Now;
2780

81+
/// <summary>
82+
/// Gets the cancellation token associated with the connection.
83+
/// </summary>
2884
public CancellationToken ConnectionToken { get; protected set; }
85+
86+
/// <summary>
87+
/// Gets the proxy information associated with the connection.
88+
/// </summary>
2989
public ProxyInfo ProxyInfo { get; protected set; }
3090

91+
/// <summary>
92+
/// Handles actions to perform when the connection is closed.
93+
/// </summary>
3194
protected virtual void OnClosed()
3295
{
3396
IsClosed = true;
@@ -45,10 +108,34 @@ protected virtual void OnClosed()
45108
closed.Invoke(this, new CloseEventArgs(closeReason));
46109
}
47110

111+
/// <summary>
112+
/// Occurs when the connection is closed.
113+
/// </summary>
48114
public event EventHandler<CloseEventArgs> Closed;
49115

116+
/// <summary>
117+
/// Closes the connection asynchronously with the specified reason.
118+
/// </summary>
119+
/// <param name="closeReason">The reason for closing the connection.</param>
120+
/// <returns>A task that represents the asynchronous close operation.</returns>
50121
public abstract ValueTask CloseAsync(CloseReason closeReason);
51122

123+
/// <summary>
124+
/// Detaches the connection asynchronously.
125+
/// </summary>
126+
/// <returns>A task that represents the asynchronous detach operation.</returns>
52127
public abstract ValueTask DetachAsync();
128+
129+
public void Dispose()
130+
{
131+
DisposeAsync()
132+
.AsTask()
133+
.Wait();
134+
}
135+
136+
public ValueTask DisposeAsync()
137+
{
138+
return CloseAsync(Connection.CloseReason.LocalClosing);
139+
}
53140
}
54141
}

src/GameFrameX.SuperSocket.Connection/ConnectionExtensions.cs

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

44
namespace GameFrameX.SuperSocket.Connection
55
{
6+
/// <summary>
7+
/// Provides extension methods for working with connections.
8+
/// </summary>
69
public static class ConnectionExtensions
710
{
11+
/// <summary>
12+
/// Gets the remote certificate associated with the connection, if available.
13+
/// </summary>
14+
/// <param name="connection">The connection to retrieve the remote certificate from.</param>
15+
/// <returns>The remote certificate, or <c>null</c> if no certificate is available.</returns>
816
public static X509Certificate GetRemoteCertificate(this IConnection connection)
917
{
1018
if (connection is IStreamConnection streamConnection

src/GameFrameX.SuperSocket.Connection/ConnectionOptions.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,59 @@
33

44
namespace GameFrameX.SuperSocket.Connection
55
{
6+
/// <summary>
7+
/// Represents configuration options for managing connections.
8+
/// </summary>
69
public class ConnectionOptions
710
{
8-
// 1M by default
11+
/// <summary>
12+
/// Gets or sets the maximum package length in bytes. Default is 1 MB.
13+
/// </summary>
914
public int MaxPackageLength { get; set; } = 1024 * 1024;
1015

11-
// 4k by default
16+
/// <summary>
17+
/// Gets or sets the size of the receive buffer in bytes. Default is 4 KB.
18+
/// </summary>
1219
public int ReceiveBufferSize { get; set; } = 1024 * 4;
1320

14-
// 4k by default
21+
/// <summary>
22+
/// Gets or sets the size of the send buffer in bytes. Default is 4 KB.
23+
/// </summary>
1524
public int SendBufferSize { get; set; } = 1024 * 4;
1625

17-
// trigger the read only when the stream is being consumed
26+
/// <summary>
27+
/// Gets or sets a value indicating whether data should be read only when the stream is being consumed.
28+
/// </summary>
1829
public bool ReadAsDemand { get; set; }
1930

2031
/// <summary>
21-
/// in milliseconds
32+
/// Gets or sets the receive timeout in milliseconds.
2233
/// </summary>
23-
/// <value></value>
2434
public int ReceiveTimeout { get; set; }
2535

2636
/// <summary>
27-
/// in milliseconds
37+
/// Gets or sets the send timeout in milliseconds.
2838
/// </summary>
29-
/// <value></value>
3039
public int SendTimeout { get; set; }
3140

41+
/// <summary>
42+
/// Gets or sets the logger for the connection.
43+
/// </summary>
3244
public ILogger Logger { get; set; }
3345

46+
/// <summary>
47+
/// Gets or sets the input pipe for the connection.
48+
/// </summary>
3449
public Pipe Input { get; set; }
3550

51+
/// <summary>
52+
/// Gets or sets the output pipe for the connection.
53+
/// </summary>
3654
public Pipe Output { get; set; }
3755

56+
/// <summary>
57+
/// Gets or sets additional key-value pairs for connection configuration.
58+
/// </summary>
3859
public Dictionary<string, string> Values { get; set; }
3960
}
4061
}

0 commit comments

Comments
 (0)