示例#1
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        public void Send(SocketToken sToken, byte[] buffer)
        {
            try
            {
                //从连接池中取出一个发送的对象
                SocketAsyncEventArgs tArgs = sendPool.Pop();
                //确保发送连接池不为空
                if (tArgs == null)
                {
                    throw new Exception("无可用的发送池");
                }

                tArgs.UserToken = sToken;
                if (!sendBuffer.WriteBuffer(buffer, tArgs))
                {
                    tArgs.SetBuffer(buffer, 0, buffer.Length);
                }

                if (!sToken.TokenSocket.SendAsync(tArgs))
                {
                    ProcessSent(tArgs);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="remoteEP"></param>
        public void Send(byte[] data, IPEndPoint remoteEP)
        {
            SocketAsyncEventArgs socketArgs = null;

            try
            {
                socketArgs = tokenPool.Pop();
                //如果发送对象池已经为空
                if (socketArgs == null)
                {
                    Initialize(maxCount, blocksize);
                    socketArgs = tokenPool.Pop();
                }
                socketArgs.RemoteEndPoint = remoteEP;

                if (!sentBufferPool.WriteBuffer(data, socketArgs))
                {
                    throw new Exception("设置发送缓冲区失败");
                }

                if (socketArgs.RemoteEndPoint != null)
                {
                    if (!clientSocket.SendToAsync(socketArgs))
                    {
                        ProcessSent(socketArgs);
                    }
                }
            }
            catch (Exception ex)
            {
                tokenPool.Push(socketArgs);

                throw ex;
            }
        }
        /// <summary>
        /// 异步发送
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="size"></param>
        /// <param name="isWaiting"></param>
        public void Send(byte[] buffer, int offset, int size, bool isWaiting = true)
        {
            ArraySegment <byte>[] segItems = sendBufferManager.BufferToSegments(buffer, offset, size);
            foreach (var seg in segItems)
            {
                SocketAsyncEventArgs tArgs = sendTokenManager.GetEmptyWait(isWaiting);
                if (tArgs == null)
                {
                    throw new Exception("发送缓冲池已用完,等待回收...");
                }

                tArgs.RemoteEndPoint = serverIpEndPoint;

                if (!sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                {
                    sendTokenManager.Set(tArgs);

                    throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sendBufferManager.BlockSize));
                }

                if (!cliSocket.SendToAsync(tArgs))
                {
                    ProcessSent(tArgs);
                }
            }
        }
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="buffer"></param>
        public void Send(byte[] buffer)
        {
            try
            {
                if (isConnected == false ||
                    clientSocket == null ||
                    clientSocket.Connected == false)
                {
                    return;
                }

                SocketAsyncEventArgs tArgs = tokenPool.Pop();
                if (tArgs == null)
                {
                    throw new Exception("发送连接池为空");
                }

                if (!sendBufferPool.WriteBuffer(buffer, tArgs))
                {
                    tArgs.SetBuffer(buffer, 0, buffer.Length);
                }

                tArgs.UserToken = tArgs;

                if (!clientSocket.SendAsync(tArgs))
                {
                    ProcessSentHandler(tArgs);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#5
0
        /// <summary>
        /// 根据偏移发送缓冲区数据
        /// </summary>
        /// <param name="sendSegment"></param>
        /// <param name="waiting"></param>
        public bool Send(SegmentOffset sendSegment, bool waiting = true)
        {
            try
            {
                if (IsClose())
                {
                    Close();
                    return(false);
                }

                var segItems    = _sBufferManager.BufferToSegments(sendSegment.Buffer, sendSegment.Offset, sendSegment.Size);
                var isWillEvent = true;

                foreach (var seg in segItems)
                {
                    var tArgs = GetSocketAsyncFromSendPool(waiting);
                    if (tArgs == null)
                    {
                        return(false);
                    }
                    if (!_sBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        _sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sBufferManager.BlockSize));
                    }
                    if (tArgs.UserToken == null)
                    {
                        ((SocketToken)tArgs.UserToken).TokenSocket = socket;
                    }

                    if (IsClose())
                    {
                        Close();
                        return(false);
                    }

                    isWillEvent &= socket.SendAsync(tArgs);
                    if (!isWillEvent)//can't trigger the io complated event to do
                    {
                        ProcessSentCallback(tArgs);
                    }

                    if (_sendTokenManager.Count < (_sendTokenManager.Capacity >> 2))
                    {
                        Thread.Sleep(2);
                    }
                }

                return(isWillEvent);
            }
            catch (Exception)
            {
                Close();

                throw;
            }
        }
示例#6
0
        public bool Send(SegmentOffsetToken segToken, bool waiting = true)
        {
            try
            {
                if (!segToken.SToken.TokenSocket.Connected)
                {
                    return(false);
                }

                var isWillEvent = true;

                var segItems = _sendBufferManager.BufferToSegments(
                    segToken.DataSegment.Buffer,
                    segToken.DataSegment.Offset,
                    segToken.DataSegment.Size);

                foreach (var seg in segItems)
                {
                    var tArgs = GetSocketAsyncFromSendPool(waiting, segToken.SToken.TokenSocket);
                    if (tArgs == null)
                    {
                        return(false);
                    }

                    tArgs.UserToken = segToken.SToken;
                    if (!_sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        _sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBufferManager.BlockSize));
                    }

                    if (!segToken.SToken.TokenSocket.Connected)
                    {
                        return(false);
                    }

                    isWillEvent &= segToken.SToken.SendAsync(tArgs);
                    if (!isWillEvent)
                    {
                        ProcessSentCallback(tArgs);
                    }

                    if (_sendTokenManager.Count < (_sendTokenManager.Capacity >> 2))
                    {
                        Thread.Sleep(5);
                    }
                }
                return(isWillEvent);
            }
            catch (Exception)
            {
                Close(segToken.SToken);

                throw;
            }
        }
示例#7
0
        /// <summary>
        /// 根据偏移发送缓冲区数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="size"></param>
        public void Send(byte[] buffer, int offset, int size, bool isWaiting = true)
        {
            try
            {
                if (isConnected == false ||
                    cliSocket == null ||
                    cliSocket.Connected == false)
                {
                    return;
                }

                ArraySegment <byte>[] segItems = sBufferManager.BufferToSegments(buffer, offset, size);
                foreach (var seg in segItems)
                {
                    var tArgs = sendTokenManager.GetEmptyWait(isWaiting);
                    if (tArgs == null)
                    {
                        throw new Exception("发送缓冲池已用完,等待回收超时...");
                    }

                    if (!sBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sBufferManager.BlockSize));
                    }
                    if (tArgs.UserToken == null)
                    {
                        ((SocketToken)tArgs.UserToken).TokenSocket = cliSocket;
                    }

                    if (!cliSocket.SendAsync(tArgs))
                    {
                        ProcessSentHandler(tArgs);
                    }

                    if (sendTokenManager.Count < (sendTokenManager.Capacity >> 1))
                    {
                        Thread.Sleep(5);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#8
0
        public bool Send(SegmentOffset sendSegment, bool waiting = true)
        {
            try
            {
                bool isWillEvent = true;
                ArraySegment <byte>[] segItems = sendBufferManager.BufferToSegments(sendSegment.buffer, sendSegment.offset, sendSegment.size);
                foreach (var seg in segItems)
                {
                    SocketAsyncEventArgs tArgs = sendTokenManager.GetEmptyWait((retry) =>
                    {
                        return(true);
                    }, waiting);

                    if (tArgs == null)
                    {
                        throw new Exception("发送缓冲池已用完,等待回收...");
                    }

                    tArgs.RemoteEndPoint = ipEndPoint;

                    if (!sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sendBufferManager.BlockSize));
                    }

                    isWillEvent &= socket.SendToAsync(tArgs);
                    if (!isWillEvent)
                    {
                        ProcessSent(tArgs);
                    }
                }
                return(isWillEvent);
            }
            catch (Exception ex)
            {
                Close();

                throw ex;
            }
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="size"></param>
        /// <param name="isWaiting"></param>
        /// <param name="remoteEP"></param>
        public void Send(byte[] buffer, int offset, int size, bool isWaiting, IPEndPoint remoteEP)
        {
            try
            {
                ArraySegment <byte>[] segItems = sendBufferManager.BufferToSegments(buffer, offset, size);
                foreach (var seg in segItems)
                {
                    var tArgs = sendTokenManager.GetEmptyWait(isWaiting);
                    if (tArgs == null)
                    {
                        throw new Exception("发送缓冲池已用完,等待回收超时...");
                    }

                    tArgs.RemoteEndPoint = remoteEP;
                    Socket s = SocketVersion(remoteEP);
                    tArgs.UserToken = s;

                    if (!sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sendBufferManager.BlockSize));
                    }

                    if (tArgs.RemoteEndPoint != null)
                    {
                        if (!s.SendToAsync(tArgs))
                        {
                            ProcessSent(tArgs);
                        }
                    }
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#10
0
        public bool Send(SegmentOffset sendSegment, bool waiting = true)
        {
            try
            {
                var isWillEvent = true;
                var segItems    = _sendBufferManager.BufferToSegments(sendSegment.Buffer, sendSegment.Offset, sendSegment.Size);
                foreach (var seg in segItems)
                {
                    var tArgs = _sendTokenManager.GetEmptyWait((retry) => true, waiting);

                    if (tArgs == null)
                    {
                        throw new Exception("发送缓冲池已用完,等待回收...");
                    }

                    tArgs.RemoteEndPoint = IpEndPoint;

                    if (!_sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        _sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBufferManager.BlockSize));
                    }

                    isWillEvent &= Socket.SendToAsync(tArgs);
                    if (!isWillEvent)
                    {
                        ProcessSent(tArgs);
                    }
                }
                return(isWillEvent);
            }
            catch (Exception)
            {
                Close();

                throw;
            }
        }
示例#11
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="dataSegment"></param>
        /// <param name="remoteEp"></param>
        /// <param name="waiting"></param>
        public bool Send(SegmentOffset dataSegment, IPEndPoint remoteEp, bool waiting)
        {
            var isWillEvent = true;

            var segItems = _sendBufferManager.BufferToSegments(dataSegment.Buffer, dataSegment.Offset, dataSegment.Size);

            foreach (var seg in segItems)
            {
                var tArgs = _sendTokenManager.GetEmptyWait(retry => true, waiting);

                if (tArgs == null)
                {
                    throw new Exception("发送缓冲池已用完,等待回收超时...");
                }

                tArgs.RemoteEndPoint = remoteEp;
                var s = SocketVersion(remoteEp);
                tArgs.UserToken = s;

                if (!_sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                {
                    _sendTokenManager.Set(tArgs);

                    throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", _sendBufferManager.BlockSize));
                }

                if (tArgs.RemoteEndPoint != null)
                {
                    isWillEvent &= s.SendToAsync(tArgs);
                    if (!isWillEvent)
                    {
                        ProcessSent(tArgs);
                    }
                }
                Thread.Sleep(5);
            }
            return(isWillEvent);
        }
        /// <summary>
        /// 异步发送数据
        /// </summary>
        public void Send(SocketToken sToken, byte[] buffer,
                         int offset, int size, bool isWaiting = true)
        {
            try
            {
                ArraySegment <byte>[] segItems = sendBufferManager.BufferToSegments(buffer, offset, size);
                foreach (var seg in segItems)
                {
                    var tArgs = sendTokenManager.GetEmptyWait(isWaiting);

                    if (tArgs == null)
                    {
                        throw new Exception("发送缓冲池已用完,等待回收超时...");
                    }

                    tArgs.UserToken = sToken;
                    if (!sendBufferManager.WriteBuffer(tArgs, seg.Array, seg.Offset, seg.Count))
                    {
                        sendTokenManager.Set(tArgs);

                        throw new Exception(string.Format("发送缓冲区溢出...buffer block max size:{0}", sendBufferManager.BlockSize));
                    }
                    if (!sToken.SendAsync(tArgs))
                    {
                        ProcessSent(tArgs);
                    }

                    if (sendTokenManager.Count < (sendTokenManager.Capacity >> 1))
                    {
                        Thread.Sleep(5);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }