private async Task DataReceiver(ElyClient tcpC)
        {
            try
            {
                byte[] inValue = new byte[] { 1, 0, 0, 0, 0x10, 0x27, 0, 0, 0xe8, 0x03, 0, 0 };//{1,10000ms,1000ms}
                tcpC.TcpHandle.Client.IOControl(IOControlCode.KeepAliveValues, inValue, null);
                // 循环监听&接收数据
                while (!Token.IsCancellationRequested)
                {
                    byte[] data = await MessageReadAsync(tcpC);

                    if (data != null && iDataReceivedFDback != null)
                    {
                        Task <bool> unwaited = Task.Run(() => iDataReceivedFDback(tcpC.IpPortStr, data));
                    }
                }
            }
            finally
            {
                RemoveClient(tcpC);
                if (iDisconnectedFDback != null)
                {
                    Task <bool> unwaited = Task.Run(() => iDisconnectedFDback(tcpC.IpPortStr));
                }
                tcpC.Dispose();
            }
        }
        public void Disconnect(string IpPort)
        {
            ElyClient ElyC = null;

            if (ClientList.TryGetValue(IpPort, out ElyC))
            {
                RemoveClient(ElyC);
                ElyC.Dispose();
            }
        }
Exemple #3
0
        private async Task <bool> StartSslConnection(ElyClient sslC)
        {
            try
            {
                SslProtocols SSLVer = GetUserSetProtocalType();
                await sslC.Sstream.AuthenticateAsServerAsync(SslCertificate, iMaumutually, SSLVer, false);

                if (!sslC.Sstream.IsEncrypted)
                {
                    throw new Exception("此连接未进行加密处理");
                }
                if (!sslC.Sstream.IsAuthenticated)
                {
                    throw new Exception("身份验证失败");
                }
                if (iMaumutually && !sslC.Sstream.IsMutuallyAuthenticated)
                {
                    throw new Exception("客户端证书验证失败");
                }
                // SSL证书验证成功,加入客户端列表,监听数据
                HandleConnectedClient(sslC);
                return(true);
            }
            catch (Exception ex)
            {
                Task unwait = null;
                if (ex.InnerException != null)
                {
                    unwait = Task.Run(() => iPromptMsgPrinter($"Error: [{sslC.IpPortStr}] {ex.InnerException.Message}"));
                }
                else
                {
                    unwait = Task.Run(() => iPromptMsgPrinter($"Error: [{sslC.IpPortStr}] {ex.Message}"));
                }

                sslC.Dispose();
                return(false);
            }
        }