Example #1
0
        public Boolean Send(Packet pk)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            StatSend?.Increment(pk.Count);
            if (Log.Enable && LogSend)
            {
                WriteLog("Send [{0}]: {1}", pk.Count, pk.ToHex());
            }

            LastTime = DateTime.Now;

            try
            {
                Server.Client.SendTo(pk.Data, pk.Offset, pk.Count, SocketFlags.None, Remote.EndPoint);

                return(true);
            }
            catch (Exception ex)
            {
                OnError("Send", ex);
                Dispose();
                throw;
            }
        }
Example #2
0
        /// <summary>发送数据</summary>
        /// <remarks>
        /// 目标地址由<seealso cref="SessionBase.Remote"/>决定
        /// </remarks>
        /// <param name="pk">数据包</param>
        /// <returns>是否成功</returns>
        protected override Boolean OnSend(Packet pk)
        {
            var count = pk.Total;

            StatSend?.Increment(count);
            if (Log != null && Log.Enable && LogSend)
            {
                WriteLog("Send [{0}]: {1}", count, pk.ToHex());
            }

            try
            {
                // 修改发送缓冲区
                if (Client.SendBufferSize < count)
                {
                    Client.SendBufferSize = count;
                }

                if (count == 0)
                {
                    Client.Send(new Byte[0]);
                }
                else if (pk.Next == null)
                {
                    Client.Send(pk.Data, pk.Offset, count, SocketFlags.None);
                }
                else
                {
                    Client.Send(pk.ToArray(), 0, count, SocketFlags.None);
                }
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Send", ex);

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

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(false);
            }

            LastTime = DateTime.Now;

            return(true);
        }
Example #3
0
        /// <summary>发送数据</summary>
        /// <remarks>
        /// 目标地址由<seealso cref="SessionBase.Remote"/>决定
        /// </remarks>
        /// <param name="pk">数据包</param>
        /// <returns>是否成功</returns>
        protected override Boolean OnSend(Packet pk)
        {
            if (StatSend != null)
            {
                StatSend.Increment(pk.Count);
            }

            try
            {
                var sp = Client;
                lock (sp)
                {
                    if (Client.Connected)
                    {
                        if (Log.Enable && LogSend)
                        {
                            WriteLog("Send [{0}]: {1}", pk.Count, pk.ToHex());
                        }

                        sp.Send(pk.Data, pk.Offset, pk.Count, SocketFlags.None);
                    }
                    else
                    {
                        Client.CheckBroadcast(Remote.Address);
                        if (Log.Enable && LogSend)
                        {
                            WriteLog("Send {2} [{0}]: {1}", pk.Count, pk.ToHex(), Remote.EndPoint);
                        }

                        sp.SendTo(pk.Data, pk.Offset, pk.Count, SocketFlags.None, Remote.EndPoint);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Send", ex);

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

                    if (ThrowException)
                    {
                        throw;
                    }
                }
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// 处理发送缓冲区
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public override byte[] DataOutDeal(byte[] buf)
        {
            lock (QueueLock)
            {
                QueueOut.Enqueue(buf);
                var bufout = QueueOut.Dequeue();

                if (this.sp.IsOpen)
                {
                    this.sp.Write(bufout, 0, bufout.Length);
                    TxCnt += bufout.Length;
                    StatSend.Increment(bufout.Length);
                }
            }
            return(buf);
        }
Example #5
0
        internal Boolean SendAsync(Byte[] buffer, Int32 times, Int32 msInterval, IPEndPoint remote)
        {
            if (!Open())
            {
                return(false);
            }

            var count = buffer.Length;

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }
            if (Log.Enable && LogSend)
            {
                WriteLog("SendAsync [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
            }

            try
            {
                var ts = new SendStat();
                ts.Buffer   = buffer;
                ts.Times    = times - 1;
                ts.Interval = msInterval;
                ts.Remote   = remote;

                Client.BeginSend(buffer, count, remote, OnSend, ts);
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("SendAsync", ex);

                    if (ThrowException)
                    {
                        throw;
                    }
                }
                return(false);
            }

            LastTime = DateTime.Now;

            return(true);
        }
Example #6
0
        public Boolean Send(byte[] buffer, int offset = 0, int count = -1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

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

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }
            if (Log.Enable && LogSend)
            {
                WriteLog("Send [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
            }

            LastTime = DateTime.Now;

            try
            {
                Server.Client.SendTo(buffer, 0, count, SocketFlags.None, Remote.EndPoint);

                return(true);
            }
            catch (Exception ex)
            {
                OnError("Send", ex);
                Dispose();
                throw;
            }
        }
Example #7
0
        /// <summary>发送数据</summary>
        /// <remarks>
        /// 目标地址由<seealso cref="SessionBase.Remote"/>决定,如需精细控制,可直接操作<seealso cref="Client"/>
        /// </remarks>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns>是否成功</returns>
        public override Boolean Send(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (!Open())
            {
                return(false);
            }

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

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }

            try
            {
                var sp = Client;
                lock (sp)
                {
                    if (Client.Client.Connected)
                    {
                        if (Log.Enable && LogSend)
                        {
                            WriteLog("Send [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
                        }

                        if (offset == 0)
                        {
                            sp.Send(buffer, count);
                        }
                        else
                        {
                            sp.Send(buffer.ReadBytes(offset, count), count);
                        }
                    }
                    else
                    {
                        if (Log.Enable && LogSend)
                        {
                            WriteLog("Send {2} [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)), Remote.EndPoint);
                        }

                        if (offset == 0)
                        {
                            sp.Send(buffer, count, Remote.EndPoint);
                        }
                        else
                        {
                            sp.Send(buffer.ReadBytes(offset, count), count, Remote.EndPoint);
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Send", ex);

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

                    if (ThrowException)
                    {
                        throw;
                    }
                }
                return(false);
            }
        }
Example #8
0
        /// <summary>发送数据</summary>
        /// <remarks>
        /// 目标地址由<seealso cref="SessionBase.Remote"/>决定
        /// </remarks>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <returns>是否成功</returns>
        public override Boolean Send(Byte[] buffer, Int32 offset = 0, Int32 count = -1)
        {
            if (!Open())
            {
                return(false);
            }

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

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }
            if (Log != null && Log.Enable && LogSend)
            {
                WriteLog("Send [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
            }

            try
            {
                // 修改发送缓冲区
                if (Client.SendBufferSize < count)
                {
                    Client.SendBufferSize = count;
                }

                if (count == 0)
                {
                    Client.Send(new Byte[0]);
                }
                else
                {
                    Client.Send(buffer, offset, count, SocketFlags.None);
                }
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("Send", ex);

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

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(false);
            }

            LastTime = DateTime.Now;

            return(true);
        }
Example #9
0
        internal Boolean SendAsync_(Byte[] buffer, IPEndPoint remote)
        {
            if (!Open())
            {
                return(false);
            }

            var count = buffer.Length;

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }
            if (Log != null && Log.Enable && LogSend)
            {
                WriteLog("SendAsync [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
            }

            LastTime = DateTime.Now;

            // 估算完成时间,执行过长时提示
            using (var tc = new TimeCost("{0}.SendAsync".F(GetType().Name), 500))
            {
                tc.Log = Log;

                if (Object.Equals(remote.Address, IPAddress.Broadcast))
                {
                    Client.EnableBroadcast = true;
                }

                // 同时只允许一个异步发送,其它发送放入队列

                // 考虑到超长数据包,拆分为多个包
                var max = 1472;
                if (buffer.Length <= max)
                {
                    var qi = new QueueItem();
                    qi.Buffer = buffer;
                    qi.Remote = remote;

                    _SendQueue.Enqueue(qi);
                }
                else
                {
                    var ms = new MemoryStream(buffer);
                    while (true)
                    {
                        var remain = (Int32)(ms.Length - ms.Position);
                        if (remain <= 0)
                        {
                            break;
                        }

                        var len = Math.Min(remain, max);

                        var qi = new QueueItem();
                        qi.Buffer = ms.ReadBytes(len);
                        qi.Remote = remote;

                        _SendQueue.Enqueue(qi);
                    }
                }

                CheckSendQueue(false);
            }

            return(true);
        }
Example #10
0
        /// <summary>异步多次发送数据</summary>
        /// <param name="buffer"></param>
        /// <param name="times"></param>
        /// <param name="msInterval"></param>
        /// <returns></returns>
        public override Boolean SendAsync(Byte[] buffer, Int32 times, Int32 msInterval)
        {
            if (!Open())
            {
                return(false);
            }

            var count = buffer.Length;

            if (StatSend != null)
            {
                StatSend.Increment(count);
            }
            if (Log.Enable && LogSend)
            {
                WriteLog("SendAsync [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));
            }

            try
            {
                // 修改发送缓冲区
                if (Client.SendBufferSize < count)
                {
                    Client.SendBufferSize = count;
                }

                var ts = new SendStat();
                ts.Buffer   = buffer;
                ts.Times    = times - 1;
                ts.Interval = msInterval;

                if (count == 0)
                {
                    Client.Client.Send(new Byte[0]);
                }
                else
                {
                    Stream.BeginWrite(buffer, 0, count, OnSend, ts);
                }
            }
            catch (Exception ex)
            {
                if (!ex.IsDisposed())
                {
                    OnError("SendAsync", ex);

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

                    if (ThrowException)
                    {
                        throw;
                    }
                }

                return(false);
            }

            LastTime = DateTime.Now;

            return(true);
        }