示例#1
0
        public void HandleNetworkStateChange(NetworkState state)
        {
            switch (state)
            {
            case NetworkState.Online:
                if (ConnectionState == ConnectionState.Disconnected ||
                    ConnectionState == ConnectionState.Suspended)
                {
                    if (Logger.IsDebug)
                    {
                        Logger.Debug("Network state is Online. Attempting reconnect.");
                    }

                    ExecuteCommand(ConnectCommand.Create().TriggeredBy("ConnectionManager.HandleNetworkStateChange(Online)"));
                }

                break;

            case NetworkState.Offline:
                if (ConnectionState == ConnectionState.Connected ||
                    ConnectionState == ConnectionState.Connecting)
                {
                    if (Logger.IsDebug)
                    {
                        Logger.Debug("Network state is Offline. Moving to disconnected.");
                    }

                    // RTN20a
                    var errorInfo =
                        new ErrorInfo(
                            "Connection disconnected due to Operating system network going offline",
                            80017);
                    ExecuteCommand(SetDisconnectedStateCommand.Create(errorInfo, retryInstantly: true).TriggeredBy("ConnectionManager.HandleNetworkStateChange(Offline)"));
                }

                break;
            }
        }
示例#2
0
        /// <summary>
        /// Instructs the library to start a connection to the server.
        /// </summary>
        public void Connect()
        {
            ThrowIfDisposed();

            ExecuteCommand(ConnectCommand.Create().TriggeredBy("Connection.Connect()"));
        }
示例#3
0
        private async Task beginAcceptTcpClient()
        {
            TcpClient tcpClient = null;

            try
            {
                tcpClient = await listener.AcceptTcpClientAsync();

                var      uri      = new Uri(serverModel.Url);
                QpClient qpClient = null;
                switch (uri.Scheme)
                {
                case "tcp":
                    qpClient = new QpTcpClient(new QpTcpClientOptions()
                    {
                        Host           = uri.Host,
                        Port           = uri.Port,
                        Password       = serverModel.Password,
                        EnableCompress = serverModel.EnableCompress,
                        EnableEncrypt  = serverModel.EnableEncrypt,
                        InstructionSet = new[] { TcpGuard.Core.Protocol.V1.Instruction.Instance }
                    });
                    break;

                case "ws":
                    qpClient = new QpWebSocketClient(new QpWebSocketClientOptions()
                    {
                        Url            = serverModel.Url,
                        Password       = serverModel.Password,
                        EnableCompress = serverModel.EnableCompress,
                        EnableEncrypt  = serverModel.EnableEncrypt,
                        InstructionSet = new[] { TcpGuard.Core.Protocol.V1.Instruction.Instance }
                    });
                    break;
                }
                qpClient.Disconnected += (sender, e) =>
                {
                    try { tcpClient.Close(); } catch { }
                };
                await qpClient.ConnectAsync();

                //check version
                {
                    var rep = await qpClient.SendCommand(new GetVersionCommand());

                    if (rep.Code != 0)
                    {
                        throw new ApplicationException("Get server verion error,reason:" + rep.Message);
                    }
                    var serverVersion = rep.Data;
                    var clientVersion = this.GetType().Assembly.GetName().Version;
                    if (clientVersion != serverVersion)
                    {
                        throw new ApplicationException($"Client[{clientVersion}] and server[{serverVersion}] version not match.");
                    }
                }
                //connect
                {
                    var rep = await qpClient.SendCommand(ConnectCommand.Create(new ConnectCommand.CommandContent()
                    {
                        Host         = portalModel.RemoteHost,
                        Port         = portalModel.RemotePort,
                        SendInterval = portalModel.SendInterval
                    }));

                    if (rep.Code != 0)
                    {
                        qpClient.Close();
                        throw new ApplicationException(rep.Message);
                    }
                }
                var portal = new Portal(qpClient, tcpClient, portalModel.SendInterval);
                portal.Stoped += (sender, e) =>
                {
                    try { qpClient.Close(); } catch { }
                    try { tcpClient.Close(); } catch { }
                };
                portal.Start();
                lock (portalDict)
                    portalDict[portalModel] = portal;
                _ = beginAcceptTcpClient();
            }
            catch
            {
                try { tcpClient?.Close(); } catch { }
                lock (portalDict)
                    if (portalDict.ContainsKey(portalModel))
                    {
                        portalDict.Remove(portalModel);
                    }
            }
        }