Exemple #1
0
        public async Task Connect()
        {
            var qpClientTypeInfo = QpClientTypeManager.Instance.Get(ConnectionInfo.QpClientTypeName);

            if (qpClientTypeInfo == null)
            {
                throw new ApplicationException($"未找到类型为[{ConnectionInfo.QpClientTypeName}]的QP客户端类型!");
            }
            QpClient = (QpClient)Activator.CreateInstance(qpClientTypeInfo.QpClientType, ConnectionInfo.QpClientOptions);
            QpClient.Disconnected += QpClient_Disconnected;
            try
            {
                await QpClient.ConnectAsync();
            }
            catch
            {
                QpClient?.Close();
                QpClient = null;
                throw;
            }
            //获取指令集信息
            try
            {
                var rep = await QpClient.SendCommand(new Quick.Protocol.Commands.GetQpInstructions.Request());

                ConnectionInfo.Instructions = rep.Data;
                Connected = true;
            }
            catch
            {
                QpClient?.Close();
                QpClient = null;
                throw;
            }
        }
Exemple #2
0
        public void Dispose()
        {
            Connected = false;
            var client = QpClient;

            QpClient = null;
            client?.Close();
        }
Exemple #3
0
        private void btnStopRecv_Click(object sender, EventArgs e)
        {
            txtFormTitle.Enabled = true;
            nudMaxLines.Enabled  = true;
            btnStartRecv.Enabled = true;
            btnStopRecv.Enabled  = false;

            if (client != null)
            {
                client.Disconnected             -= Client_Disconnected;
                client.HeartbeatPackageReceived -= Client_HeartbeatPackageReceived;
                client = null;
            }
            pushLog("已停止接收.");
        }
Exemple #4
0
        private void btnStartRecv_Click(object sender, EventArgs e)
        {
            client = connectionContext.QpClient;
            if (client == null)
            {
                pushLog($"当前未连接,无法接收!");
                return;
            }

            txtFormTitle.Enabled = false;
            nudMaxLines.Enabled  = false;
            btnStartRecv.Enabled = false;
            btnStopRecv.Enabled  = true;

            maxLines = Convert.ToInt32(nudMaxLines.Value);
            pushLog("开始接收..");
            client.Disconnected             += Client_Disconnected;
            client.HeartbeatPackageReceived += Client_HeartbeatPackageReceived;
        }
Exemple #5
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);
                    }
            }
        }
Exemple #6
0
        private async void btnTest_Click(object sender, EventArgs e)
        {
            if (lvServers.SelectedItems.Count <= 0)
            {
                return;
            }
            var lvi   = lvServers.SelectedItems[0];
            var model = (Model.ServerModel)lvi.Tag;

            showLoading($"Testing server[{model.Url}]...");
            var      uri    = new Uri(model.Url);
            QpClient client = null;

            try
            {
                switch (uri.Scheme)
                {
                case "tcp":
                    client = new QpTcpClient(new QpTcpClientOptions()
                    {
                        Host           = uri.Host,
                        Port           = uri.Port,
                        Password       = model.Password,
                        EnableCompress = model.EnableCompress,
                        EnableEncrypt  = model.EnableEncrypt,
                        InstructionSet = new[] { TcpGuard.Core.Protocol.V1.Instruction.Instance }
                    });
                    break;

                case "ws":
                    client = new QpWebSocketClient(new QpWebSocketClientOptions()
                    {
                        Url            = model.Url,
                        Password       = model.Password,
                        EnableCompress = model.EnableCompress,
                        EnableEncrypt  = model.EnableEncrypt,
                        InstructionSet = new[] { TcpGuard.Core.Protocol.V1.Instruction.Instance }
                    });
                    break;
                }
                if (client == null)
                {
                    throw new ApplicationException($"Unknown Url:{model.Url}");
                }
                await client.ConnectAsync();

                var rep = await client.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.");
                }
                client.Close();
                MessageBox.Show("Test success.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Test failed.Reason:" + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            closeLoading();
        }