1313
1414namespace GameFrameX . SuperSocket . Udp
1515{
16+ /// <summary>
17+ /// Represents a listener for UDP connections.
18+ /// </summary>
1619 class UdpConnectionListener : IConnectionListener
1720 {
1821 private ILogger _logger ;
@@ -21,14 +24,29 @@ class UdpConnectionListener : IConnectionListener
2124
2225 private IPEndPoint _acceptRemoteEndPoint ;
2326
27+ /// <summary>
28+ /// Gets the factory for creating connections.
29+ /// </summary>
2430 public IConnectionFactory ConnectionFactory { get ; }
25-
26- public ListenOptions Options { get ; }
27-
31+
32+ /// <summary>
33+ /// Gets the options for the listener.
34+ /// </summary>
35+ public ListenOptions Options { get ; }
36+
37+ /// <summary>
38+ /// Gets the options for the connection.
39+ /// </summary>
2840 public ConnectionOptions ConnectionOptions { get ; }
2941
42+ /// <summary>
43+ /// Gets a value indicating whether the listener is running.
44+ /// </summary>
3045 public bool IsRunning { get ; private set ; }
3146
47+ /// <summary>
48+ /// Occurs when a new connection is accepted.
49+ /// </summary>
3250 public event NewConnectionAcceptHandler NewConnectionAccept ;
3351
3452 private static readonly ArrayPool < byte > _bufferPool = ArrayPool < byte > . Shared ;
@@ -41,6 +59,15 @@ class UdpConnectionListener : IConnectionListener
4159
4260 private IAsyncSessionContainer _sessionContainer ;
4361
62+ /// <summary>
63+ /// Initializes a new instance of the <see cref="UdpConnectionListener"/> class.
64+ /// </summary>
65+ /// <param name="options">The options for the listener.</param>
66+ /// <param name="connectionOptions">The options for the connection.</param>
67+ /// <param name="connectionFactory">The factory for creating connections.</param>
68+ /// <param name="logger">The logger instance.</param>
69+ /// <param name="udpSessionIdentifierProvider">The provider for UDP session identifiers.</param>
70+ /// <param name="sessionContainer">The container for managing sessions.</param>
4471 public UdpConnectionListener ( ListenOptions options , ConnectionOptions connectionOptions , IConnectionFactory connectionFactory , ILogger logger , IUdpSessionIdentifierProvider udpSessionIdentifierProvider , IAsyncSessionContainer sessionContainer )
4572 {
4673 Options = options ;
@@ -51,15 +78,19 @@ public UdpConnectionListener(ListenOptions options, ConnectionOptions connection
5178 _sessionContainer = sessionContainer ;
5279 }
5380
81+ /// <summary>
82+ /// Starts the UDP connection listener.
83+ /// </summary>
84+ /// <returns><c>true</c> if the listener started successfully; otherwise, <c>false</c>.</returns>
5485 public bool Start ( )
5586 {
5687 var options = Options ;
5788
5889 try
59- {
60- var listenEndpoint = options . ToEndPoint ( ) ;
90+ {
91+ var listenEndpoint = options . ToEndPoint ( ) ;
6192 var listenSocket = _listenSocket = new Socket ( listenEndpoint . AddressFamily , SocketType . Dgram , ProtocolType . Udp ) ;
62-
93+
6394 if ( options . NoDelay )
6495 listenSocket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . NoDelay , true ) ;
6596
@@ -82,7 +113,7 @@ public bool Start()
82113 catch ( PlatformNotSupportedException )
83114 {
84115 _logger . LogWarning ( "Failed to set socket option SIO_UDP_CONNRESET because the platform doesn't support it." ) ;
85- }
116+ }
86117
87118 IsRunning = true ;
88119
@@ -115,12 +146,12 @@ private async Task KeepAccept(Socket listenSocket)
115146
116147 var packageData = new ArraySegment < byte > ( buffer , 0 , result . ReceivedBytes ) ;
117148 var remoteEndPoint = result . RemoteEndPoint as IPEndPoint ;
118-
149+
119150 var sessionID = _udpSessionIdentifierProvider . GetSessionIdentifier ( remoteEndPoint , packageData ) ;
120151
121152 var session = await _sessionContainer . GetSessionByIDAsync ( sessionID ) ;
122153
123- IVirtualConnection connection = null ;
154+ IVirtualConnection connection = null ;
124155
125156 if ( session != null )
126157 {
@@ -136,13 +167,13 @@ private async Task KeepAccept(Socket listenSocket)
136167 OnNewConnectionAccept ( connection ) ;
137168 }
138169
139- await connection . WritePipeDataAsync ( packageData . AsMemory ( ) , _cancellationTokenSource . Token ) ;
170+ await connection . WriteInputPipeDataAsync ( packageData . AsMemory ( ) , _cancellationTokenSource . Token ) ;
140171 }
141172 catch ( Exception e )
142173 {
143174 if ( e is ObjectDisposedException || e is NullReferenceException )
144175 break ;
145-
176+
146177 if ( e is SocketException se )
147178 {
148179 var errorCode = se . ErrorCode ;
@@ -153,7 +184,7 @@ private async Task KeepAccept(Socket listenSocket)
153184 break ;
154185 }
155186 }
156-
187+
157188 _logger . LogError ( e , $ "Listener[{ this . ToString ( ) } ] failed to receive udp data") ;
158189 }
159190 finally
@@ -196,16 +227,25 @@ private async ValueTask<IConnection> CreateConnection(Socket socket, IPEndPoint
196227 {
197228 _logger . LogError ( e , $ "Failed to create connection for { socket . RemoteEndPoint } .") ;
198229 return null ;
199- }
230+ }
200231 }
201232
233+ /// <summary>
234+ /// Creates a connection using the specified socket.
235+ /// </summary>
236+ /// <param name="connection">The socket representing the connection.</param>
237+ /// <returns>A task that represents the asynchronous operation. The task result contains the created connection.</returns>
202238 public async Task < IConnection > CreateConnection ( object connection )
203239 {
204240 var socket = ( Socket ) connection ;
205241 var remoteEndPoint = socket . RemoteEndPoint as IPEndPoint ;
206242 return await CreateConnection ( socket , remoteEndPoint , _udpSessionIdentifierProvider . GetSessionIdentifier ( remoteEndPoint , null ) ) ;
207243 }
208244
245+ /// <summary>
246+ /// Stops the UDP connection listener asynchronously.
247+ /// </summary>
248+ /// <returns>A task that represents the asynchronous stop operation.</returns>
209249 public Task StopAsync ( )
210250 {
211251 var listenSocket = _listenSocket ;
@@ -217,17 +257,22 @@ public Task StopAsync()
217257
218258 _cancellationTokenSource . Cancel ( ) ;
219259 listenSocket . Close ( ) ;
220-
260+
221261 return _stopTaskCompletionSource . Task ;
222262 }
223263
224264 public override string ToString ( )
225265 {
226266 return Options ? . ToString ( ) ;
227267 }
268+
269+ /// <summary>
270+ /// Disposes the resources used by the UDP connection listener.
271+ /// </summary>
228272 public void Dispose ( )
229273 {
230274 var listenSocket = _listenSocket ;
275+
231276 if ( listenSocket != null && Interlocked . CompareExchange ( ref _listenSocket , null , listenSocket ) == listenSocket )
232277 {
233278 listenSocket . Dispose ( ) ;
0 commit comments