Skip to content

Commit d8ebe53

Browse files
authored
AsyncSocketServer sample: delete the AsyncUserToken class (#8534)
There is no value in having a type that only wraps a Socket, we acn simplify the example by storing the Socket as e.UserToken directly.
1 parent e2cd6de commit d8ebe53

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

snippets/csharp/System.Net.Sockets/SocketAsyncEventArgs/Overview/AsyncSocketServer.cs

+8-29
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818

1919
namespace AsyncSocketSample
2020
{
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-
4121
//<Snippet1>
4222
// This class creates a single large buffer which can be divided up
4323
// and assigned to SocketAsyncEventArgs objects for use with each
@@ -207,7 +187,6 @@ public void Init()
207187
//Pre-allocate a set of reusable SocketAsyncEventArgs
208188
readWriteEventArg = new SocketAsyncEventArgs();
209189
readWriteEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
210-
readWriteEventArg.UserToken = new AsyncUserToken();
211190

212191
// assign a byte buffer from the buffer pool to the SocketAsyncEventArg object
213192
m_bufferManager.SetBuffer(readWriteEventArg);
@@ -282,7 +261,7 @@ private void ProcessAccept(SocketAsyncEventArgs e)
282261
// Get the socket for the accepted client connection and put it into the
283262
//ReadEventArg object user token
284263
SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();
285-
((AsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket;
264+
readEventArgs.UserToken = e.AcceptSocket;
286265

287266
// As soon as the client is connected, post a receive to the connection
288267
bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);
@@ -318,7 +297,6 @@ void IO_Completed(object sender, SocketAsyncEventArgs e)
318297
private void ProcessReceive(SocketAsyncEventArgs e)
319298
{
320299
// check if the remote host closed the connection
321-
AsyncUserToken token = (AsyncUserToken)e.UserToken;
322300
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
323301
{
324302
//increment the count of the total bytes receive by the server
@@ -327,7 +305,8 @@ private void ProcessReceive(SocketAsyncEventArgs e)
327305

328306
//echo the data received back to the client
329307
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);
331310
if (!willRaiseEvent)
332311
{
333312
ProcessSend(e);
@@ -349,9 +328,9 @@ private void ProcessSend(SocketAsyncEventArgs e)
349328
if (e.SocketError == SocketError.Success)
350329
{
351330
// done echoing data back to the client
352-
AsyncUserToken token = (AsyncUserToken)e.UserToken;
331+
Socket socket = (Socket)e.UserToken;
353332
// read the next block of data send from the client
354-
bool willRaiseEvent = token.Socket.ReceiveAsync(e);
333+
bool willRaiseEvent = socket.ReceiveAsync(e);
355334
if (!willRaiseEvent)
356335
{
357336
ProcessReceive(e);
@@ -365,16 +344,16 @@ private void ProcessSend(SocketAsyncEventArgs e)
365344

366345
private void CloseClientSocket(SocketAsyncEventArgs e)
367346
{
368-
AsyncUserToken token = e.UserToken as AsyncUserToken;
347+
Socket socket = (Socket)e.UserToken;
369348

370349
// close the socket associated with the client
371350
try
372351
{
373-
token.Socket.Shutdown(SocketShutdown.Send);
352+
socket.Shutdown(SocketShutdown.Send);
374353
}
375354
// throws if client process has already closed
376355
catch (Exception) { }
377-
token.Socket.Close();
356+
socket.Close();
378357

379358
// decrement the counter keeping track of the total number of clients connected to the server
380359
Interlocked.Decrement(ref m_numConnectedSockets);

0 commit comments

Comments
 (0)