Exemple #1
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);
                }
            }
        }
        /// <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;
        }
 /// <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);
 }