Ejemplo n.º 1
0
        private void HandleSendPayload(ProtocolSocket proxy, byte[] buffer, int offset, int count)
        {
            int sent = 0;

            _settingUp.Wait();
            while (IsActive && count > 0)
            {
                try
                {
                    sent = Client.Send(buffer, offset, count, SocketFlags.None);
                }
                catch (Exception ex)
                {
                    Close(ex);
                }
                if (sent == 0)
                {
                    Close();
                }
                else
                {
                    offset += sent;
                    count  -= sent;
                }
            }
        }
Ejemplo n.º 2
0
 public void Start(bool fromTimer = false)
 {
     if (IsConnected)
     {
         return;
     }
     lock (_sync)
     {
         MainSocket     = null;
         _lastConnected = _lastPing = _lastPong = DateTimeOffset.Now;
         try
         {
             var socket = TryConnectTcp(RelayAddress, RelayPort);
             if (socket == null)
             {
                 return;
             }
             MainSocket = new ProtocolSocket(socket);
             MainSocket.S2C_OnStartNewConnection = HandleStartNewConnection;
             MainSocket.S2C_OnKeepAlive          = HandleServerKeepAlive;
             MainSocket.Initialize();
         }
         catch
         {
         }
         finally
         {
             if (!fromTimer)
             {
                 _reconnectTimer.Change(100, Timeout.Infinite);
             }
         }
     }
 }
Ejemplo n.º 3
0
 private void HandleServerKeepAlive(ProtocolSocket obj)
 {
     lock (_sync)
     {
         _lastPong = DateTimeOffset.Now;
         Console.WriteLine($"Roundtrip ping: {(_lastPong - _lastPing).TotalMilliseconds:0.00}ms");
     }
 }
Ejemplo n.º 4
0
 private void HandleStartRelay(ProtocolSocket socket, int id)
 {
     if (!Channels.TryGetValue(id, out var channel))
     {
         return;
     }
     channel.SetProxy(socket);
 }
Ejemplo n.º 5
0
        private void HandleNewInternalSocket(Socket socket)
        {
            var protoSocket = new ProtocolSocket(socket);

            protoSocket.Disconnected    += ProtoSocket_Disconnected;
            protoSocket.C2S_OnStartRelay = HandleStartRelay;
            protoSocket.Initialize();
            Console.WriteLine($"New internal socket from {socket.RemoteEndPoint}");
        }
Ejemplo n.º 6
0
        public void SetProxy(ProtocolSocket proxy)
        {
            if (IsActive)
            {
                return;
            }

            _settingUp.Reset();
            Proxy = proxy;
            Proxy.OnSendPayload = HandleSendPayload;
            Proxy.Disconnected += Proxy_Disconnected;

            proxy.Initialize();
            TryInitialize();
            _settingUp.Set();
        }
Ejemplo n.º 7
0
        private void HandleStartNewConnection(ProtocolSocket socket, int id, ushort targetPort, ushort internalPort)
        {
            Console.WriteLine($"HandleStartNewConnection #{id} target={targetPort} internal={internalPort}");
            var localSocket  = TryConnect(BoundInterface.ToString(), targetPort);
            var remoteSocket = TryConnect(RelayAddress, internalPort);
            var success      = localSocket != null && remoteSocket != null;

            if (IsConnected)
            {
                MainSocket.C2S_StartNewConnectionReply(id, internalPort, success);
            }
            var protoSocket = new ProtocolSocket(remoteSocket);

            protoSocket.C2S_StartRelay(id);
            var proxy = new ChannelProxy(localSocket);

            proxy.ChannelId = id;
            proxy.SetProxy(protoSocket);
        }
Ejemplo n.º 8
0
 private void HandleConnectionReply(ProtocolSocket socket, int id, ushort internalPort, bool success)
 {
     Console.WriteLine($"HandleConnectionReply id={id}, internal={internalPort}, success={success}");
     if (!Relays.TryGetValue(internalPort, out var relay))
     {
         return;
     }
     if (!relay.Channels.TryGetValue(id, out var channel))
     {
         return;
     }
     if (channel.IsActive)
     {
         return;
     }
     if (!success)
     {
         channel.Close();
     }
 }
Ejemplo n.º 9
0
 private void HandleNewSocket(Socket socket)
 {
     if (MainSocket == null || !MainSocket.IsConnected || (DateTimeOffset.Now - _lastPing).TotalSeconds > 15)
     {
         Console.WriteLine($"Replacing the old socket with one from {socket.RemoteEndPoint}");
         if (MainSocket != null)
         {
             MainSocket.Close();
         }
         _lastPing  = DateTimeOffset.Now;
         MainSocket = new ProtocolSocket(socket);
         MainSocket.C2S_OnStartNewConnectionReply = HandleConnectionReply;
         MainSocket.C2S_OnKeepAlive = HandleKeepAlive;
         MainSocket.Initialize();
     }
     else
     {
         socket.Close();
     }
 }
Ejemplo n.º 10
0
 public ChannelProxy(ProtocolSocket proxy)
 {
     SetProxy(proxy);
 }
Ejemplo n.º 11
0
 private void Proxy_Disconnected(ProtocolSocket socket, Exception exception)
 {
     Close(exception);
 }
Ejemplo n.º 12
0
 private void HandleKeepAlive(ProtocolSocket obj)
 {
     Console.WriteLine("Sending keep alive");
     _lastPing = DateTimeOffset.Now;
     obj.S2C_KeepAlive();
 }
Ejemplo n.º 13
0
 private void ProtoSocket_Disconnected(ProtocolSocket socket, Exception exception)
 {
     TemporarySockets.TryRemove(socket, out _);
 }