示例#1
0
        /// <summary>发送数据</summary>
        /// <remarks>
        /// 目标地址由<seealso cref="Remote"/>决定
        /// </remarks>
        /// <param name="pk">数据包</param>
        /// <returns>是否成功</returns>
        public Boolean Send(Packet pk)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            if (!Open())
            {
                return(false);
            }

            if (SendFilter == null)
            {
                return(OnSend(pk));
            }

            var ctx = new SessionFilterContext
            {
                Session = this,
                Packet  = pk
            };

            SendFilter.Execute(ctx);
            pk = ctx.Packet;

            if (pk == null)
            {
                return(false);
            }

            return(OnSend(pk));
        }
示例#2
0
        /// <summary>以队列发送数据包,自动拆分大包,合并小包</summary>
        /// <param name="pk"></param>
        /// <param name="remote"></param>
        /// <returns></returns>
        internal Boolean SendByQueue(Packet pk, IPEndPoint remote)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
            if (!Open())
            {
                return(false);
            }

            if (_SendQueue == null)
            {
                _SendQueue = new SendQueue(this);
            }

            var filter = SendFilter;

            if (filter == null)
            {
                return(_SendQueue.Add(pk, remote));
            }

            var ctx = new SessionFilterContext
            {
                Session = this,
                Packet  = pk,
                Remote  = remote
            };

            filter.Execute(ctx);

            pk     = ctx.Packet;
            remote = ctx.Remote;

            if (pk == null)
            {
                return(false);
            }

            return(_SendQueue.Add(pk, remote));
        }