Пример #1
0
 // Remote Cycle
 private void OnRemoteReceive(IAsyncResult ar)
 {
     try
     {
         if (RemoteSocket.Connected)
         {
             int length = RemoteSocket.EndReceive(ar);
             if (length > 0 && ClientSocket.Connected)
             {
                 if (Encoding.ASCII.GetString(RemoteBuffer, 0, 4).StartsWith("HTTP"))
                 {
                     string respose = Encoding.ASCII.GetString(RemoteBuffer, 0, length);
                     int    sp      = respose.IndexOf("\r\n");
                     Helper.Debug(respose.Substring(0, sp > 0 ? sp + 2 : 1024), ConsoleColor.DarkYellow);
                 }
                 ClientSocket.BeginSend(RemoteBuffer, 0, length, SocketFlags.None, this.OnClientSent, ClientSocket);
                 return;
             }
         }
     }
     catch (Exception ex)
     {
         Dispose();
         Helper.PublishException(ex);
     }
     Dispose();
 }
Пример #2
0
 private void ListenThread()
 {
     try
     {
         RemoteSocket.ReceiveTimeout = 1000;
         byte[] buffer = new byte[1024 * 1024];
         while (this.IsExpired == false)
         {
             SocketHelpers.Read(this, RemoteSocket, buffer, 4);
             if (this.IsExpired)
             {
                 break;
             }
             var messageLength = BitConverter.ToInt32(buffer, 0);
             SocketHelpers.Read(this, RemoteSocket, buffer, messageLength);
             if (this.IsExpired)
             {
                 break;
             }
             var messageText = Encoding.UTF8.GetString(buffer, 0, messageLength);
             MessageReceived.Fire(messageText);
         }
     }
     catch (Exception ex)
     {
         UnexpectedDisconnect.Fire(ex);
         this.Dispose();
     }
     finally
     {
         RemoteSocket.Close();
     }
 }
Пример #3
0
        protected virtual void End()
        {
            RemoteStream.Close();

            RemoteSocket.Shutdown(SocketShutdown.Both);
            RemoteSocket.Close();

            ClientStream.Close();

            ClientSocket.Shutdown(SocketShutdown.Both);
            ClientSocket.Close();
        }
Пример #4
0
 private void ConnectCallback(IAsyncResult ar)
 {
     try
     {
         RemoteSocket.EndConnect(ar);
         SendRespoce(0x00);
     }
     catch
     {
         SendRespoce(0x01);
     }
 }
Пример #5
0
 public void StartRelay()
 {
     try
     {
         ClientSocket.BeginReceive(ClientBuffer, 0, ClientBuffer.Length, SocketFlags.None, this.OnClientReceive, ClientSocket);
         RemoteSocket.BeginReceive(RemoteBuffer, 0, RemoteBuffer.Length, SocketFlags.None, this.OnRemoteReceive, RemoteSocket);
     }
     catch (Exception ex)
     {
         Dispose();
         Helper.PublishException(ex);
     }
 }
Пример #6
0
 // Client Cycle
 private void OnRemoteSent(IAsyncResult ar)
 {
     try
     {
         if (RemoteSocket.Connected)
         {
             int length = RemoteSocket.EndSend(ar);
             if (length > 0 && ClientSocket.Connected)
             {
                 ClientSocket.BeginReceive(ClientBuffer, 0, ClientBuffer.Length, SocketFlags.None, this.OnClientReceive, ClientSocket);
                 return;
             }
         }
     }
     catch (Exception ex)
     {
         Dispose();
         Helper.PublishException(ex);
     }
     Dispose();
 }
Пример #7
0
 // Client Cycle
 private void OnClientReceive(IAsyncResult ar)
 {
     try
     {
         if (ClientSocket.Connected)
         {
             int length = ClientSocket.EndReceive(ar);
             if (length > 0 && RemoteSocket.Connected)
             {
                 RemoteSocket.BeginSend(ClientBuffer, 0, length, SocketFlags.None, new AsyncCallback(this.OnRemoteSent), RemoteSocket);
                 return;
             }
         }
     }
     catch (Exception ex)
     {
         Dispose();
         Helper.PublishException(ex);
     }
     Dispose();
 }
Пример #8
0
 private void OnConnected(IAsyncResult ar)
 {
     try
     {
         RemoteSocket.EndConnect(ar);
         if (this.requestType == "CONNECT")
         {
             string respose = string.Format(ErrorPages.HTTPS_CONNECTED, this.httpVersion);
             ClientSocket.BeginSend(Encoding.ASCII.GetBytes(respose), 0, respose.Length, SocketFlags.None, this.OnRequestSent, ClientSocket);
         }
         else
         {
             string request = RebuildQuery();
             Helper.Debug(request.Substring(0, request.IndexOf("\r\n") + 2), ConsoleColor.Green);
             RemoteSocket.BeginSend(Encoding.ASCII.GetBytes(request), 0, request.Length, SocketFlags.None, this.OnRequestSent, RemoteSocket);
         }
     }
     catch (Exception ex)
     {
         Dispose();
         Helper.PublishException(ex);
     }
 }
Пример #9
0
 /// <summary>
 /// Return a string representation of this object.
 /// </summary>
 public override String ToString()
 {
     return("UDP packet received at " + ServerTimestamp +
            " from " + RemoteSocket.ToString() +
            " to " + LocalSocket.ToString());
 }