Ejemplo n.º 1
0
        private void sendCallback(IAsyncResult asyncSend)
        {
            SendMsg sendBuff = asyncSend.AsyncState as SendMsg;

            socket.EndSend(asyncSend);
            if (this.netWorkState != NetWorkState.Running)
            {
                this._isSending = false;
                return;
            }

            lock (this._sendQueue)
            {
                if (this._sendQueue.Count == 0)
                {
                    this._isSending = false;
                }
                else
                {
                    sendBuff = this._sendQueue.Dequeue();
                    byte[] thisSend = sendBuff.GetSendBuff();
                    socket.BeginSend(thisSend, 0, thisSend.Length, SocketFlags.None, new AsyncCallback(sendCallback), sendBuff);
                }
            }
        }
Ejemplo n.º 2
0
        public bool Send(SendMsg buffer)
        {
            bool ret = false;

            byte[]  thisSend = null;
            SendMsg sendBuff = buffer;

            do
            {
                if (!this.CanSend())
                {
                    break;
                }

                lock (_sendQueue)
                {
                    //发送队列为空,且当前不在发送过程中
                    if (this._sendQueue.Count == 0 && !this._isSending)
                    {
                        thisSend = buffer.GetSendBuff();
                    }
                    else
                    {
                        this._sendQueue.Enqueue(buffer);
                        if (!this._isSending)     // 当前没在发送则开始发送
                        {
                            sendBuff = this._sendQueue.Dequeue();
                            thisSend = sendBuff.GetSendBuff();
                        }
                    }

                    if (thisSend != null)
                    {
                        socket.BeginSend(thisSend, 0, thisSend.Length, SocketFlags.None, new AsyncCallback(sendCallback), sendBuff);
                        this._isSending = true;
                    }
                }
                ret = true;
            } while (false);
            return(ret);
        }