1- using System . IO . Pipelines ;
1+ using System . Buffers ;
2+ using System . IO . Pipelines ;
23using System . Net ;
34using GameFrameX . SuperSocket . ProtoBase ;
45using GameFrameX . SuperSocket . ProtoBase . ProxyProtocol ;
56
67namespace 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}
0 commit comments