mbmiddleton

Updated AsyncSocketSession.cs

Apr 27th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1.         public void ProcessReceive(SocketAsyncEventArgs e)
  2.         {
  3.             // check if the remote host closed the connection
  4.             if (e.BytesTransferred <= 0)
  5.             {
  6.                 Close(CloseReason.ClientClosing);
  7.                 return;
  8.             }
  9.  
  10.             if (e.SocketError != SocketError.Success)
  11.             {
  12.                 Close(CloseReason.SocketError);
  13.                 return;
  14.             }
  15.  
  16.             int bytesTransferred = e.BytesTransferred;
  17.             int offset = e.Offset;
  18.  
  19.             while (bytesTransferred > 0)
  20.             {
  21.                 TCommandInfo commandInfo = default(TCommandInfo);
  22.                 int left = bytesTransferred;
  23.                 int offsetAcc = offset;
  24.  
  25.                 while(left > 0)
  26.                 {
  27.                     commandInfo = FindCommand(e.Buffer, offsetAcc, left, true, out left);
  28.                     offsetAcc = offset + (bytesTransferred - left);
  29.                 }
  30.                
  31.                 if (IsClosed)
  32.                     return;
  33.  
  34.                 if (commandInfo == null)
  35.                     break;
  36.  
  37.                 try
  38.                 {
  39.                     ExecuteCommand(commandInfo);
  40.                 }
  41.                 catch (Exception exc)
  42.                 {
  43.                     AppServer.Logger.LogError(this, exc);
  44.                     HandleExceptionalError(exc);
  45.                 }
  46.  
  47.                 if (left <= 0)
  48.                     break;
  49.  
  50.                 bytesTransferred = left;
  51.                 offset = e.Offset + e.BytesTransferred - left;
  52.             }
  53.  
  54.             //read the next block of data sent from the client
  55.             StartReceive(e);
  56.         }
Advertisement
Add Comment
Please, Sign In to add comment