Example #1
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 #2
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);
        }