Ejemplo n.º 1
0
        /// <summary>处理收到的数据</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        protected override Boolean OnReceive(Packet pk, IPEndPoint remote)
        {
            if (pk == null || pk.Count == 0 && !MatchEmpty)
            {
                return(true);
            }

#if !__MOBILE__
            // 更新全局远程IP地址
            NewLife.Web.WebHelper.UserHost = Remote.EndPoint?.Address + "";
#endif

            StatReceive?.Increment(pk.Count);
            if (base.OnReceive(pk, remote))
            {
                return(true);
            }

            // 分析处理
            var e = new ReceivedEventArgs(pk);
            e.UserState = Remote.EndPoint;

            if (Log.Enable && LogReceive)
            {
                WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex(32, null));
            }

            RaiseReceive(this, e);

            return(true);
        }
Ejemplo n.º 2
0
        private void OnReceive(Packet pk, IPEndPoint remote)
        {
            var e = new ReceivedEventArgs(pk);

            e.UserState = remote;

            LastTime = DateTime.Now;
            //if (StatReceive != null) StatReceive.Increment(e.Length);

            if (Log.Enable && LogReceive)
            {
                WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex(32, null));
            }

            Received?.Invoke(this, e);

            if (Packet != null && e.Packet != null && MessageReceived != null)
            {
                var msg = Packet.LoadMessage(e.Packet);
                var me  = new MessageEventArgs
                {
                    Packet    = e.Packet,
                    UserState = e.UserState,
                    Message   = msg
                };
                MessageReceived(this, me);
            }
        }
Ejemplo n.º 3
0
        /// <summary>处理收到的数据</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        internal override Boolean OnReceive(Packet pk, IPEndPoint remote)
        {
            if (pk.Count == 0 && DisconnectWhenEmptyData)
            {
                Close("收到空数据");
                Dispose();

                return(true);
            }

#if !__MOBILE__
            // 更新全局远程IP地址
            NewLife.Web.WebHelper.UserHost = Remote.EndPoint?.Address + "";
#endif

            if (base.OnReceive(pk, remote))
            {
                return(true);
            }

            // 分析处理
            var e = new ReceivedEventArgs(pk);
            e.UserState = Remote.EndPoint;

            if (Log.Enable && LogReceive)
            {
                WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex(32, null));
            }

            RaiseReceive(this, e);

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>处理收到的数据</summary>
        /// <param name="stream"></param>
        /// <param name="remote"></param>
        internal override void OnReceive(Stream stream, IPEndPoint remote)
        {
            // 过滤自己广播的环回数据。放在这里,兼容UdpSession
            if (!Loopback && remote.Port == Port)
            {
                if (!Local.Address.IsAny())
                {
                    if (remote.Address.Equals(Local.Address))
                    {
                        return;
                    }
                }
                else
                {
                    foreach (var item in NetHelper.GetIPsWithCache())
                    {
                        if (remote.Address.Equals(item))
                        {
                            return;
                        }
                    }
                }
            }

#if !__MOBILE__
            // 更新全局远程IP地址
            NewLife.Web.WebHelper.UserHost = remote.ToString();
#endif
            // 分析处理
            var e = new ReceivedEventArgs();
            e.Stream    = stream;
            e.UserState = remote;

            // 同步匹配
            _recv?.SetResult(e);
            _recv = null;

            // 为该连接单独创建一个会话,方便直接通信
            var session = CreateSession(remote);
            // 数据直接转交给会话,不再经过事件,那样在会话较多时极为浪费资源
            var us = session as UdpSession;
            if (us != null)
            {
                us.OnReceive(e);
            }
            else
            {
                // 没有匹配到任何会话时,才在这里显示日志。理论上不存在这个可能性
                if (Log.Enable && LogReceive)
                {
                    WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex());
                }
            }

            if (session != null)
            {
                RaiseReceive(session, e);
            }
        }
Ejemplo n.º 5
0
        private void OnReceive(Stream stream, IPEndPoint remote)
        {
            var e = new ReceivedEventArgs();

            e.Stream    = stream;
            e.UserState = remote;

            // 同步匹配
            _recv?.SetResult(e);
            _recv = null;

            LastTime = DateTime.Now;
            //if (StatReceive != null) StatReceive.Increment(e.Length);

            if (Log.Enable && LogReceive)
            {
                WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex());
            }

            Received?.Invoke(this, e);
        }
Ejemplo n.º 6
0
        /// <summary>处理收到的数据</summary>
        /// <param name="stream"></param>
        protected virtual void OnReceive(Stream stream)
        {
#if !__MOBILE__
            // 更新全局远程IP地址
            NewLife.Web.WebHelper.UserHost = Remote.EndPoint.ToString();
#endif
            // 分析处理
            var e = new ReceivedEventArgs();
            e.Stream    = stream;
            e.UserState = Remote.EndPoint;

            // 同步匹配
            _recv?.SetResult(e);
            _recv = null;

            if (Log.Enable && LogReceive)
            {
                WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex());
            }

            RaiseReceive(this, e);
        }
Ejemplo n.º 7
0
        ///// <summary>接收数据</summary>
        ///// <returns></returns>
        //public override Byte[] Receive()
        //{
        //    if (Disposed) throw new ObjectDisposedException(GetType().Name);

        //    if (!Open()) return null;

        //    var task = SendAsync(null, null);
        //    if (Timeout > 0 && !task.Wait(Timeout)) return null;

        //    return task.Result;
        //}

        /// <summary>处理收到的数据</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        internal override Boolean OnReceive(Packet pk, IPEndPoint remote)
        {
            // 过滤自己广播的环回数据。放在这里,兼容UdpSession
            if (!Loopback && remote.Port == Port)
            {
                if (!Local.Address.IsAny())
                {
                    if (remote.Address.Equals(Local.Address))
                    {
                        return(false);
                    }
                }
                else
                {
                    foreach (var item in NetHelper.GetIPsWithCache())
                    {
                        if (remote.Address.Equals(item))
                        {
                            return(false);
                        }
                    }
                }
            }

#if !__MOBILE__
            // 更新全局远程IP地址
            NewLife.Web.WebHelper.UserHost = remote?.Address + "";
#endif
            LastRemote = remote;

            if (base.OnReceive(pk, remote))
            {
                return(true);
            }

            // 分析处理
            var e = new ReceivedEventArgs(pk);
            e.UserState = remote;

            // 为该连接单独创建一个会话,方便直接通信
            var session = CreateSession(remote);
            // 数据直接转交给会话,不再经过事件,那样在会话较多时极为浪费资源
            var us = session as UdpSession;
            if (us != null)
            {
                us.OnReceive(e);
            }
            else
            {
                // 没有匹配到任何会话时,才在这里显示日志。理论上不存在这个可能性
                if (Log.Enable && LogReceive)
                {
                    WriteLog("Recv [{0}]: {1}", e.Length, e.ToHex(32, null));
                }
            }

            if (session != null)
            {
                RaiseReceive(session, e);
            }

            return(true);
        }