Advertisement
parabola949

Untitled

May 27th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. int bytesRead = 0;
  2.  
  3.                                         msg = new byte[4096];
  4.                                         Logging.Write(
  5.                                             "Starting task to wait for Connection Established from server (registration)");
  6.                                         var ar = stream.BeginRead(msg, 0, msg.Length, null, null);
  7.                                         var wh = ar.AsyncWaitHandle;
  8.                                         try
  9.                                         {
  10.                                             connected = true;
  11.                                             if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(10), false))
  12.                                             {
  13.                                                 client.Close();
  14.                                                 connected = false;
  15.                                                 throw new TimeoutException();
  16.                                             }
  17.                                             bytesRead = stream.EndRead(ar);
  18.                                            
  19.                                         }
  20.                                         catch
  21.                                         {
  22.                                             _events.Publish(FormatMessage("SHUTTING DOWN"));
  23.                                         }
  24.                                         finally
  25.                                         {
  26.                                             wh.Close();
  27.                                         }
  28.                                         if (msg[0] == 0)
  29.                                         {
  30.                                             _events.Publish(FormatMessage("SHUTTING DOWN"));
  31.                                             connected = false;
  32.                                         }
  33.                                         if (connected)
  34.                                         {
  35.                                            
  36.                                             Logging.Write("Message Received: " +
  37.                                                           _encoder.GetString(msg, 0, bytesRead));
  38.                                             _events.Publish(FormatMessage(_encoder.GetString(msg, 0, bytesRead)));
  39.                                             await HandleClient(client);
  40.                                         }
  41.                                         else
  42.                                         {
  43.                                             stream.Close();
  44.                                             client.Close();
  45.                                         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement