Ejemplo n.º 1
0
        private static void ShowMethod(IntPtr mt)
        {
            XTrace.WriteLine("ShowMethod: {0}", mt.ToString("x"));
            var buf = new Byte[8];
            Marshal.Copy(mt, buf, 0, buf.Length);
            //XTrace.WriteLine(buf.ToHex("-"));

            var ip = new IntPtr((Int64)buf.ToUInt64());
            XTrace.WriteLine("{0}", ip.ToString("x"));

            if (ip.ToInt64() <= 0x1000000 || ip.ToInt64() > 0x800000000000L) return;

            buf = new Byte[32];
            Marshal.Copy(ip, buf, 0, buf.Length);
            XTrace.WriteLine(buf.ToHex("-"));
        }
Ejemplo n.º 2
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 (Disposed) throw new ObjectDisposedException(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.Connected)
                    {
                        if (Log.Enable && LogSend) WriteLog("Send [{0}]: {1}", count, buffer.ToHex(0, Math.Min(count, 32)));

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

                        sp.SendTo(buffer, offset, count, SocketFlags.None, Remote.EndPoint);
                    }
                }

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

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

                    if (ThrowException) throw;
                }
                return false;
            }
        }
Ejemplo n.º 3
0
        internal Boolean SendAsync_(Byte[] buffer, 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)));

            LastTime = DateTime.Now;

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

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

                // 考虑到超长数据包,拆分为多个包
                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;
        }
Ejemplo n.º 4
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;
        }