Beispiel #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);
        }
Beispiel #2
0
        /// <summary>读取指定长度的数据,一般是一帧</summary>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public override Int32 Receive(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (!Open())
            {
                return(-1);
            }

            if (count < 0)
            {
                count = buffer.Length - offset;
            }

            try
            {
                // 通过任务拦截异步接收
                var tsc = _recv;
                if (tsc == null)
                {
                    tsc = _recv = new TaskCompletionSource <ReceivedEventArgs>();
                }
                if (!tsc.Task.Wait(Timeout))
                {
                    return(-1);
                }

                var e = tsc.Task.Result;
                if (e == null)
                {
                    return(-1);
                }

                // 读取数据
                if (offset + count > e.Length)
                {
                    count = e.Length - offset;
                }
                var size = e.Stream.Read(buffer, offset, count);
                e.Stream.Seek(-size, SeekOrigin.Current);
                LastRemote = e.UserState as IPEndPoint;

                if (StatReceive != null)
                {
                    StatReceive.Increment(size);
                }

                return(size);
            }
            finally
            {
                _recv = null;
            }
        }
Beispiel #3
0
        /// <summary>处理消息</summary>
        /// <param name="session"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        IMessage IApiHost.Process(IApiSession session, IMessage msg)
        {
            if (msg.Reply)
            {
                return(null);
            }

            StatReceive?.Increment();

            return(OnProcess(session, msg));
        }
Beispiel #4
0
        /// <summary>触发数据到达事件</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void RaiseReceive(Object sender, ReceivedEventArgs e)
        {
            LastTime = DateTime.Now;
            if (StatReceive != null)
            {
                StatReceive.Increment(e.Length);
            }

            if (Received != null)
            {
                Received(sender, e);
            }
        }
Beispiel #5
0
        /// <summary>读取指定长度的数据,一般是一帧</summary>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public override Int32 Receive(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            if (!Open())
            {
                return(-1);
            }

            if (count < 0)
            {
                count = buffer.Length - offset;
            }

            var size = 0;
            var sp   = Client;

            try
            {
                EndPoint remoteEP = null;
                size       = Client.ReceiveFrom(buffer, offset, count, SocketFlags.None, ref remoteEP);
                LastRemote = remoteEP as IPEndPoint;
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Receive", ex);

                    // 异常可能是连接出了问题,UDP不需要关闭
                    //Close();

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(-1);
            }

            if (StatReceive != null)
            {
                StatReceive.Increment(size);
            }

            return(size);
        }
Beispiel #6
0
        /// <summary>读取指定长度的数据,一般是一帧</summary>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public override Int32 Receive(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (!Open())
            {
                return(-1);
            }

            if (count < 0)
            {
                count = buffer.Length - offset;
            }

            var rs = 0;

            try
            {
                //if (count > 0) rs = Stream.Read(buffer, offset, count);
                if (count > 0)
                {
                    rs = Client.Receive(buffer, offset, count, SocketFlags.None);
                }
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Receive", ex);

                    // 发送异常可能是连接出了问题,需要关闭
                    Close("同步接收出错");
                    Reconnect();

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(-1);
            }

            LastTime = DateTime.Now;
            if (StatReceive != null)
            {
                StatReceive.Increment(rs);
            }

            return(rs);
        }
Beispiel #7
0
        /// <summary>触发数据到达事件</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void RaiseReceive(Object sender, ReceivedEventArgs e)
        {
            LastTime = DateTime.Now;
            if (StatReceive != null)
            {
                StatReceive.Increment(e.Length);
            }

            Received?.Invoke(sender, 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(sender, me);
            }
        }
Beispiel #8
0
        /// <summary>读取指定长度的数据,一般是一帧</summary>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public override Int32 Receive(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!Open())
            {
                return(-1);
            }

            if (count < 0)
            {
                count = buffer.Length - offset;
            }

            var size = 0;
            var sp   = Client;

            try
            {
                IPEndPoint remoteEP = null;
                var        data     = Client.Receive(ref remoteEP);
                LastRemote = remoteEP;
                if (data != null && data.Length > 0)
                {
                    size = data.Length;
                    // 计算还有多少可用空间
                    if (size > count)
                    {
                        size = count;
                    }
                    buffer.Write(offset, data, 0, size);
                }
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Receive", ex);

                    // 异常可能是连接出了问题,UDP不需要关闭
                    //Close();

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(-1);
            }

            if (StatReceive != null)
            {
                StatReceive.Increment(size);
            }

            return(size);
        }
Beispiel #9
0
        /// <summary>处理收到的数据</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        protected 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;

            StatReceive?.Increment(pk.Count);
            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);
        }