コード例 #1
0
ファイル: Connection.cs プロジェクト: Gary2008/Helpers
        /// <summary>
        /// 构造函数
        ///     构造时指定准备使用的通讯协议。
        /// </summary>
        /// <param name="aProtocol">准备使用的通讯协议实例</param>
        public Connection(Protocol aProtocol, string aName)
        {
            Name        = aName;
            _Dispatcher = Dispatcher.CurrentDispatcher;

            // 保存准备使用的通讯协议
            Protocol = aProtocol;
            if (Protocol != null)
            {
                Protocol.ByteFrameReceived   += new System.Action <byte[]>(Protocol_ByteFrameReceived);
                Protocol.StringFrameReceived += new System.Action <string>(Protocol_StringFrameReceived);
                Protocol.ObjectFrameReceived += new System.Action <object>(Protocol_ObjectFrameReceived);
                Protocol.Clarify             += OnProtocol_Clarify;
                ReceiveTimeOut = Protocol.ReceiveTimeOut;
            }

            AllConnections.Add(this);

            _ReceiveBuffer = new byte[ReceiveBufferLength];
            _ReadCallback  = new AsyncCallback(ReadCallback);

            _ReceiveTimeOutTimer          = new DispatcherTimer();
            _ReceiveTimeOutTimer.Interval = ReceiveTimeOut;
            _ReceiveTimeOutTimer.Tick    += OnRecveTimeOut_Tick;

            _HeartbeatTimer          = new DispatcherTimer();
            _HeartbeatTimer.Interval = HeartBeatInterval;
            _HeartbeatTimer.Tick    += OnHeartbeat_Tick;
        }
コード例 #2
0
ファイル: Connections.cs プロジェクト: Omybot/GoBot
        /// <summary>
        /// Ajoute une connexion UDP aux connexions suivies
        /// </summary>
        /// <param name="board">Carte associée à la connexion</param>
        /// <param name="ip">Adresse IP de la connexion</param>
        /// <param name="inPort">Port d'entrée pour le PC</param>
        /// <param name="outPort">Port de sortie pour le PC</param>
        /// <returns>La connexion créée</returns>
        private static UDPConnection AddUDPConnection(Board board, IPAddress ip, int inPort, int outPort)
        {
            UDPConnection output = new UDPConnection();

            output.Connect(ip, inPort, outPort);
            UDPBoardConnection.Add(board, output);
            EnableConnection.Add(board, true);
            AllConnections.Add(output);
            output.ConnectionChecker.SendConnectionTest += ConnexionCheck_SendConnectionTestUDP;

            return(output);
        }
コード例 #3
0
        private void AcceptConnectCallback(IAsyncResult ar)
        {
            Listener = (Socket)ar.AsyncState;

            //acknowledge the connection
            Socket incomingSocket = null;

            try
            {
                incomingSocket = Listener.EndAccept(ar);
            }
            catch
            {
                ReportError?.Invoke("EndAccept failed on incoming connection", "");
            }

            //put the listener back to listening
            Listener.BeginAccept(new AsyncCallback(AcceptConnectCallback), Listener);

            if (incomingSocket == null)
            {
                return;
            }
            IPEndPoint ep = (IPEndPoint)incomingSocket.RemoteEndPoint;

            TConnection C = new TConnection();

            C.Setup(incomingSocket, ep.Address, ep.Port, ConnectionBufSize);

            if (AllConnections.ContainsKey(C.Address) == false)
            {
                AllConnections.Add(C.Address, C);
                AllConnectionsList.Add(C);
            }

            //Signal that a new connection has been created
            NewConnection?.Invoke(C);

            //configure the socket to receive incoming data and arm the data reception event
            try
            {
                C.ConnectionSocket.BeginReceive(C.IncomingData, 0, C.IncomingData.Length, 0, new AsyncCallback(ReadCallback), C.Address);
            }
            catch
            {
                ReportError?.Invoke("BeginReceive failed on new connection", C.Address);
            }
        }
コード例 #4
0
        ///<inheritdoc/>
        public ConnectionGene GetConnection(NodeGene In, NodeGene Out)
        {
            ConnectionGene connection = new ConnectionGene(In, Out);

            if (AllConnections.ContainsKey(connection))
            {
                connection.InnovationNumber = AllConnections[connection].InnovationNumber;
            }
            else
            {
                connection.InnovationNumber = AllConnections.Count + 1;
                AllConnections.Add(connection, connection);
            }

            return(connection);
        }
コード例 #5
0
ファイル: Connection.cs プロジェクト: vadomlore/Helpers
        /// <summary>
        /// 构造函数
        ///     构造时指定准备使用的通讯协议。
        /// </summary>
        /// <param name="aProtocol">准备使用的通讯协议实例</param>
        public Connection(Protocol aProtocol)
        {
            // 指定默认属性值
            IdleInterval        = 10;
            ReceiveTimeOut      = TimeSpan.Zero;
            ExitTimeoutInterval = 2000;
            HeartBeatInterval   = TimeSpan.FromSeconds(2);

            // 保存准备使用的通讯协议
            _Protocol = aProtocol;
            if (_Protocol != null)
            {
                _Protocol.ByteFrameReceived   += new System.Action <byte[]>(Protocol_ByteFrameReceived);
                _Protocol.StringFrameReceived += new System.Action <string>(Protocol_StringFrameReceived);
                _Protocol.ObjectFrameReceived += new System.Action <object>(Protocol_ObjectFrameReceived);
                _Protocol.Clarify             += OnProtocol_Clarify;
                ReceiveTimeOut = _Protocol.ReceiveTimeOut;
            }

            AllConnections.Add(this);
        }
コード例 #6
0
        protected virtual void SecureClientConnected(TcpClient client)
        {
            if (SecureSettings.Certificate == null)
            {
                client.Close();
                return;
            }
            //prepare session
            var connection = new HttpConnection()
            {
                NetworkClient = client,
                Ip            = client.Client.RemoteEndPoint is IPEndPoint iPEndPoint
                    ? iPEndPoint.Address.ToString()
                    : client.Client.RemoteEndPoint?.ToString(),
            };

            AllConnections.Add(connection);
            //listen to connection
            _ = Task.Run(async() =>
            {
                //authentificate as server and establish ssl connection
                var stream = new SslStream(client.GetStream(), false);
                connection.NetworkStream = stream;
                stream.AuthenticateAsServer(
                    serverCertificate:          SecureSettings.Certificate,
                    clientCertificateRequired:  false,
                    enabledSslProtocols:        SslProtocols.None,
                    checkCertificateRevocation: true
                    );
                if (!stream.IsAuthenticated)
                {
                    stream.Dispose();
                    client.Close();
                    AllConnections.Remove(connection);
                    return;
                }

                await SafeClientStartListen(connection).ConfigureAwait(false);
            });
        }
コード例 #7
0
 public void AddConnection(IConnection connection) => AllConnections.Add(connection);