Ejemplo n.º 1
0
        internal override void CloseConnection(Exception ex, IConnection remoteHost)
        {
            if (!SocketMap.ContainsKey(remoteHost.RemoteHost))
            {
                return;                                                //already been removed
            }
            var clientSocket = SocketMap[remoteHost.RemoteHost];

            try
            {
                if (clientSocket.Socket.Connected)
                {
                    clientSocket.Socket.Close();
                }
            }
            catch (Exception innerEx)
            {
                OnErrorIfNotNull(innerEx, remoteHost);
            }
            finally
            {
                NodeDisconnected(new HeliosConnectionException(ExceptionType.Closed, ex), remoteHost);

                if (SocketMap.ContainsKey(remoteHost.RemoteHost))
                {
                    SocketMap.Remove(remoteHost.RemoteHost);
                }

                if (!clientSocket.WasDisposed)
                {
                    clientSocket.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        private void SendCallback(IAsyncResult ar)
        {
            var receiveState = (NetworkState)ar.AsyncState;

            try
            {
                var bytesSent = receiveState.Socket.EndSend(ar);
                receiveState.Buffer.SkipBytes(bytesSent);

                if (receiveState.Buffer.ReadableBytes > 0) //need to send again
                {
                    receiveState.Socket.BeginSendTo(receiveState.Buffer.ToArray(), 0, receiveState.Buffer.ReadableBytes,
                                                    SocketFlags.None, receiveState.RemoteHost.ToEndPoint(),
                                                    SendCallback, receiveState);
                }
            }
            catch (SocketException ex) //node disconnected
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    CloseConnection(ex, connection);
                }
            }
            catch (Exception ex)
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    OnErrorIfNotNull(ex, connection);
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 关闭连接
 /// </summary>
 /// <param name="reason"></param>
 /// <param name="remoteConnection"></param>
 internal override void CloseConnection(Exception reason, IConnection remoteConnection)
 {
     Console.WriteLine("CloseConnection-->>" + reason.ToString() + remoteConnection.ToString());
     if (SocketMap.ContainsKey(remoteConnection.RemoteHost.nodeConfig.ToString()))
     {
         var connection = SocketMap[remoteConnection.RemoteHost.nodeConfig.ToString()];
         CloseConnection(connection);
     }
 }
Ejemplo n.º 4
0
        ///// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="receiveState"></param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            var receiveState = (NetworkState)ar.AsyncState;

            try
            {
                var received = ListenerSocket.EndReceiveFrom(ar, ref RemoteEndPoint);
                if (received == 0)
                {
                    return;
                }

                var remoteAddress = (IPEndPoint)RemoteEndPoint;

                receiveState.RemoteHost = remoteAddress.ToNode(ReactorType.Udp);

                INode node = receiveState.RemoteHost;
                if (SocketMap.ContainsKey(receiveState.RemoteHost.nodeConfig.ToString()))
                {
                    var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()];
                    node = connection.RemoteHost;
                }
                else
                {
                    RefactorRequestChannel requestChannel = new RefactorProxyRequestChannel(this, node, "none");
                    SocketMap.Add(node.nodeConfig.ToString(), requestChannel);
                }
                receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received);

                Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out List <IByteBuf> decoded);

                foreach (var message in decoded)
                {
                    var networkData = NetworkData.Create(receiveState.RemoteHost, message);
                    LogAgent.Info(String.Format("Socket收到数据-->>{0}", this.Encoder.ByteEncode(networkData.Buffer)));
                    if (ConnectionAdapter is ReactorConnectionAdapter)
                    {
                        ((ReactorConnectionAdapter)ConnectionAdapter).networkDataDocker.AddNetworkData(networkData);
                        ((EventWaitHandle)((ReactorConnectionAdapter)ConnectionAdapter).protocolEvents[(int)ProtocolEvents.PortReceivedData]).Set();
                    }
                }

                //清除数据继续接收
                receiveState.RawBuffer = new byte[receiveState.RawBuffer.Length];
                ListenerSocket.BeginReceiveFrom(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length, SocketFlags.None,
                                                ref RemoteEndPoint, ReceiveCallback, receiveState);
            }
            catch  //node disconnected
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost.nodeConfig.ToString()))
                {
                    var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()];
                    CloseConnection(connection);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 开始监听
        /// </summary>
        protected override void StartInternal()
        {
            var receiveState = CreateNetworkState(Listener, Node.Empty());

            if (!SocketMap.ContainsKey(this.LocalEndpoint.nodeConfig.ToString()))
            {
                RefactorRequestChannel adapter;
                adapter = new RefactorProxyRequestChannel(this, this.LocalEndpoint, "none");
                SocketMap.Add(this.LocalEndpoint.nodeConfig.ToString(), adapter);
            }
            ListenerSocket.DataReceived += new SerialDataReceivedEventHandler(PortDataReceived);
            ListenerSocket.Open();
            IsActive = true;
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 开始监听
 /// </summary>
 protected override void StartInternal()
 {
     IsActive = true;
     if (!SocketMap.ContainsKey(this.LocalEndpoint.nodeConfig.ToString()))
     {
         RefactorRequestChannel adapter = new RefactorProxyRequestChannel(this, this.LocalEndpoint, "none");
         SocketMap.Add(this.LocalEndpoint.nodeConfig.ToString(), ConnectionAdapter);
     }
     IsActive = true;
     ListenerSocket.Bind(LocalEndPoint);
     ListenerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
     ListenerSocket.BeginReceiveFrom(this.networkState.RawBuffer, 0, this.networkState.RawBuffer.Length, SocketFlags.None,
                                     ref RemoteEndPoint, PortDataReceived, this.networkState);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="receiveState"></param>
        private void ReceiveCallback(NetworkState receiveState)
        {
            try
            {
                var received = receiveState.RawBuffer.Length;
                if (received == 0)
                {
                    return;
                }
                INode node = receiveState.RemoteHost;
                if (SocketMap.ContainsKey(receiveState.RemoteHost.nodeConfig.ToString()))
                {
                    var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()];
                    node = connection.RemoteHost;
                }
                else
                {
                    RefactorProxyResponseChannel adapter = new RefactorProxyResponseChannel(this, null);
                    SocketMap.Add(adapter.RemoteHost.nodeConfig.ToString(), adapter.requestChannel);
                }
                receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received);

                List <IByteBuf> decoded;
                Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded);

                foreach (var message in decoded)
                {
                    var    networkData = NetworkData.Create(receiveState.RemoteHost, message);
                    string log         = String.Format("{0}:串口处理数据-->>{1}", DateTime.Now.ToString("hh:mm:ss"), this.Encoder.ByteEncode(networkData.Buffer));
                    Console.WriteLine(log);
                    if (ConnectionAdapter is ReactorConnectionAdapter)
                    {
                        ((ReactorConnectionAdapter)ConnectionAdapter).networkDataDocker.AddNetworkData(networkData);
                        ((EventWaitHandle)((ReactorConnectionAdapter)ConnectionAdapter).protocolEvents[(int)ProtocolEvents.PortReceivedData]).Set();
                    }
                }
            }
            catch  //node disconnected
            {
                var connection = SocketMap[receiveState.RemoteHost.nodeConfig.ToString()];
                CloseConnection(connection);
            }
        }
Ejemplo n.º 8
0
 internal override void CloseConnection(Exception reason, IConnection remoteConnection)
 {
     //NO-OP (no connections in UDP)
     try
     {
         NodeDisconnected(new HeliosConnectionException(ExceptionType.Closed, reason), remoteConnection);
     }
     catch (Exception innerEx)
     {
         OnErrorIfNotNull(innerEx, remoteConnection);
     }
     finally
     {
         if (SocketMap.ContainsKey(remoteConnection.RemoteHost))
         {
             SocketMap.Remove(remoteConnection.RemoteHost);
         }
     }
 }
Ejemplo n.º 9
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            var receiveState = (NetworkState)ar.AsyncState;

            try
            {
                var received = receiveState.Socket.EndReceive(ar);

                if (!receiveState.Socket.Connected || received == 0)
                {
                    HeliosTrace.Instance.TcpInboundReceiveFailure();
                    var connection = SocketMap[receiveState.RemoteHost];
                    CloseConnection(connection);
                    return;
                }


                receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received);
                HeliosTrace.Instance.TcpInboundReceive(received);
                var adapter = SocketMap[receiveState.RemoteHost];

                List <IByteBuf> decoded;
                adapter.Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded);

                foreach (var message in decoded)
                {
                    var networkData = NetworkData.Create(receiveState.RemoteHost, message);
                    ReceivedData(networkData, adapter);
                    HeliosTrace.Instance.TcpInboundReceiveSuccess();
                }

                //reuse the buffer
                if (receiveState.Buffer.ReadableBytes == 0)
                {
                    receiveState.Buffer.SetIndex(0, 0);
                }
                else
                {
                    receiveState.Buffer.CompactIfNecessary();
                    var postCompact = receiveState.Buffer;
                }


                //continue receiving in a loop
                receiveState.Socket.BeginReceive(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length,
                                                 SocketFlags.None, ReceiveCallback, receiveState);
            }
            catch (SocketException ex) //node disconnected
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    CloseConnection(ex, connection);
                }
                HeliosTrace.Instance.TcpInboundReceiveFailure();
            }
            catch (ObjectDisposedException ex)
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    CloseConnection(ex, connection);
                }
                HeliosTrace.Instance.TcpInboundReceiveFailure();
            }
            catch (Exception ex)
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    OnErrorIfNotNull(ex, connection);
                }
                HeliosTrace.Instance.TcpInboundReceiveFailure();
            }
        }
Ejemplo n.º 10
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            var receiveState = (NetworkState)ar.AsyncState;

            try
            {
                var received = receiveState.Socket.EndReceiveFrom(ar, ref RemoteEndPoint);
                if (received == 0)
                {
                    if (SocketMap.ContainsKey(receiveState.RemoteHost))
                    {
                        var connection = SocketMap[receiveState.RemoteHost];
                        CloseConnection(connection);
                    }
                    return;
                }

                var remoteAddress = (IPEndPoint)RemoteEndPoint;

                if (receiveState.RemoteHost.IsEmpty())
                {
                    receiveState.RemoteHost = remoteAddress.ToNode(TransportType.Udp);
                }

                ReactorResponseChannel adapter;
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    adapter = SocketMap[receiveState.RemoteHost];
                }
                else
                {
                    adapter = new ReactorProxyResponseChannel(this, receiveState.Socket, remoteAddress,
                                                              EventLoop.Clone(ProxiesShareFiber));
                    ;
                    SocketMap.Add(adapter.RemoteHost, adapter);
                    NodeConnected(adapter.RemoteHost, adapter);
                }

                receiveState.Buffer.WriteBytes(receiveState.RawBuffer, 0, received);

                List <IByteBuf> decoded;
                Decoder.Decode(ConnectionAdapter, receiveState.Buffer, out decoded);

                foreach (var message in decoded)
                {
                    var networkData = NetworkData.Create(receiveState.RemoteHost, message);
                    ReceivedData(networkData, adapter);
                }

                //reuse the buffer
                if (receiveState.Buffer.ReadableBytes == 0)
                {
                    receiveState.Buffer.SetIndex(0, 0);
                }
                else
                {
                    receiveState.Buffer.CompactIfNecessary();
                }

                receiveState.Socket.BeginReceiveFrom(receiveState.RawBuffer, 0, receiveState.RawBuffer.Length,
                                                     SocketFlags.None, ref RemoteEndPoint, ReceiveCallback, receiveState); //receive more messages
            }
            catch (SocketException ex)                                                                                     //node disconnected
            {
                var connection = SocketMap[receiveState.RemoteHost];
                CloseConnection(ex, connection);
            }
            catch (ObjectDisposedException ex)
            {
                if (SocketMap.ContainsKey(receiveState.RemoteHost))
                {
                    var connection = SocketMap[receiveState.RemoteHost];
                    CloseConnection(ex, connection);
                }
            }
            catch (Exception ex)
            {
                var connection = SocketMap[receiveState.RemoteHost];
                OnErrorIfNotNull(ex, connection);
            }
        }