示例#1
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);
             }
         }
     }
 }
示例#2
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}");
        }
示例#3
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();
        }
示例#4
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();
     }
 }