18
18
19
19
namespace AsyncSocketSample
20
20
{
21
- // This class is designed for use as the object to be assigned to the SocketAsyncEventArgs.UserToken property.
22
- //
23
- class AsyncUserToken
24
- {
25
- Socket m_socket ;
26
-
27
- public AsyncUserToken ( ) : this ( null ) { }
28
-
29
- public AsyncUserToken ( Socket socket )
30
- {
31
- m_socket = socket ;
32
- }
33
-
34
- public Socket Socket
35
- {
36
- get { return m_socket ; }
37
- set { m_socket = value ; }
38
- }
39
- }
40
-
41
21
//<Snippet1>
42
22
// This class creates a single large buffer which can be divided up
43
23
// and assigned to SocketAsyncEventArgs objects for use with each
@@ -207,7 +187,6 @@ public void Init()
207
187
//Pre-allocate a set of reusable SocketAsyncEventArgs
208
188
readWriteEventArg = new SocketAsyncEventArgs ( ) ;
209
189
readWriteEventArg . Completed += new EventHandler < SocketAsyncEventArgs > ( IO_Completed ) ;
210
- readWriteEventArg . UserToken = new AsyncUserToken ( ) ;
211
190
212
191
// assign a byte buffer from the buffer pool to the SocketAsyncEventArg object
213
192
m_bufferManager . SetBuffer ( readWriteEventArg ) ;
@@ -282,7 +261,7 @@ private void ProcessAccept(SocketAsyncEventArgs e)
282
261
// Get the socket for the accepted client connection and put it into the
283
262
//ReadEventArg object user token
284
263
SocketAsyncEventArgs readEventArgs = m_readWritePool . Pop ( ) ;
285
- ( ( AsyncUserToken ) readEventArgs . UserToken ) . Socket = e . AcceptSocket ;
264
+ readEventArgs . UserToken = e . AcceptSocket ;
286
265
287
266
// As soon as the client is connected, post a receive to the connection
288
267
bool willRaiseEvent = e . AcceptSocket . ReceiveAsync ( readEventArgs ) ;
@@ -318,7 +297,6 @@ void IO_Completed(object sender, SocketAsyncEventArgs e)
318
297
private void ProcessReceive ( SocketAsyncEventArgs e )
319
298
{
320
299
// check if the remote host closed the connection
321
- AsyncUserToken token = ( AsyncUserToken ) e . UserToken ;
322
300
if ( e . BytesTransferred > 0 && e . SocketError == SocketError . Success )
323
301
{
324
302
//increment the count of the total bytes receive by the server
@@ -327,7 +305,8 @@ private void ProcessReceive(SocketAsyncEventArgs e)
327
305
328
306
//echo the data received back to the client
329
307
e . SetBuffer ( e . Offset , e . BytesTransferred ) ;
330
- bool willRaiseEvent = token . Socket . SendAsync ( e ) ;
308
+ Socket socket = ( Socket ) e . UserToken ;
309
+ bool willRaiseEvent = socket . SendAsync ( e ) ;
331
310
if ( ! willRaiseEvent )
332
311
{
333
312
ProcessSend ( e ) ;
@@ -349,9 +328,9 @@ private void ProcessSend(SocketAsyncEventArgs e)
349
328
if ( e . SocketError == SocketError . Success )
350
329
{
351
330
// done echoing data back to the client
352
- AsyncUserToken token = ( AsyncUserToken ) e . UserToken ;
331
+ Socket socket = ( Socket ) e . UserToken ;
353
332
// read the next block of data send from the client
354
- bool willRaiseEvent = token . Socket . ReceiveAsync ( e ) ;
333
+ bool willRaiseEvent = socket . ReceiveAsync ( e ) ;
355
334
if ( ! willRaiseEvent )
356
335
{
357
336
ProcessReceive ( e ) ;
@@ -365,16 +344,16 @@ private void ProcessSend(SocketAsyncEventArgs e)
365
344
366
345
private void CloseClientSocket ( SocketAsyncEventArgs e )
367
346
{
368
- AsyncUserToken token = e . UserToken as AsyncUserToken ;
347
+ Socket socket = ( Socket ) e . UserToken ;
369
348
370
349
// close the socket associated with the client
371
350
try
372
351
{
373
- token . Socket . Shutdown ( SocketShutdown . Send ) ;
352
+ socket . Shutdown ( SocketShutdown . Send ) ;
374
353
}
375
354
// throws if client process has already closed
376
355
catch ( Exception ) { }
377
- token . Socket . Close ( ) ;
356
+ socket . Close ( ) ;
378
357
379
358
// decrement the counter keeping track of the total number of clients connected to the server
380
359
Interlocked . Decrement ( ref m_numConnectedSockets ) ;
0 commit comments