Advertisement
Guest User

Untitled

a guest
Jun 21st, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1.  
  2.         /// <summary>
  3.         /// Callback for received data
  4.         /// </summary>
  5.         private void ReceiveCallback(IAsyncResult ar)
  6.         {
  7.             int len = -1;
  8.             try
  9.             {
  10.                 len = this.GetSocket().EndReceive(ar);
  11.                 if (len <= 0)
  12.                     throw new Exception("Received " + len);
  13.             }
  14.             catch (Exception e)
  15.             {
  16.                 if (ErrorEvent != null)
  17.                     ErrorEvent(this, e);
  18.                 return;
  19.             }
  20.             // TODO: Implement event for received data
  21.             // Retrieve the header object
  22.             Header headerObject = (Header)ar.AsyncState;
  23.             // Get the data length
  24.             int dataLength = BitConverter.ToInt32(headerObject.headerBytes, 0);
  25.             // Create a packet
  26.             Packet packet = new Packet();
  27.             int bytesRead = 0;
  28.             // Read until the total length has been received
  29.             do
  30.             {
  31.                 // Temporary array
  32.                 byte[] tmpArray = new byte[1024];
  33.                 // Receive
  34.                 int i = GetSocket().Receive(tmpArray, tmpArray.Length, SocketFlags.None);
  35.                 bytesRead += i;
  36.                 // Add to packet
  37.                 packet.WriteBytes(tmpArray, i);
  38.             }
  39.             while (bytesRead < dataLength);
  40.             // OK proceed
  41.         if (DataReceivedEvent != null)
  42.                 DataReceivedEvent(this, packet);
  43.         }
  44.  
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement