public void SendMsgCompleted(TcpSendMsg tcpSendMsg)
 {
     if (!tcpSendMsg.SendImmediate)
     {
         DequeueSendMsgPool(tcpSendMsg);
     }
     SendMsgPacket();
 }
 private void EnqueueSendMsgPool(TcpSendMsg tcpSendMsg)
 {
     while (true)
     {
         if (Interlocked.CompareExchange(ref sendMsgPoolState, 1, 0) == 0)
         {
             sendMsgPool.Add(tcpSendMsg);
             sendMsgPoolState = 0;
             return;
         }
     }
 }
        public void Init(ReceiveInvokeElement receiveInvokeElement, TcpSendMsg heartbeatMsg, int heartbeatInterval)
        {
            this.receiveIvokeElement       = receiveInvokeElement;
            tcpClient.ReceiveInvokeElement = receiveInvokeElement;

            if (heartbeatMsg != null)
            {
                heartbeatMsg.Encode();
                this.heartbeatMsg = heartbeatMsg;
            }

            this.heartbeatInterval  = heartbeatInterval;
            this.maxReceiveInterval = heartbeatInterval * 2;
        }
 private void DequeueSendMsgPool(TcpSendMsg tcpSendMsg)
 {
     if (sendMsgPool.Count > 0)
     {
         while (true)
         {
             if (Interlocked.CompareExchange(ref sendMsgPoolState, 2, 0) == 0)
             {
                 sendMsgPool.Remove(tcpSendMsg);
                 sendMsgPoolState = 0;
                 return;
             }
         }
     }
 }
        public void SendMessage(TcpSendMsg msg, bool immediate)
        {
            msg.Encode();
            msg.SendImmediate = immediate;

            if (Interlocked.Read(ref tcpSocketState) == 1)
            {
                if (immediate)
                {
                    tcpClient.BeginSend(msg.Packet, msg);
                }
                else
                {
                    EnqueueSendMsgPool(msg);
                    SendMsgPacket();
                }
            }
        }
 private bool PeekSendMsgPool(ref TcpSendMsg tcpSendMsg)
 {
     if (sendMsgPool.Count > 0)
     {
         while (true)
         {
             if (Interlocked.CompareExchange(ref sendMsgPoolState, 3, 0) == 0)
             {
                 if (sendMsgPool.Count > 0)
                 {
                     tcpSendMsg       = sendMsgPool[0];
                     sendMsgPoolState = 0;
                     return(true);
                 }
                 else
                 {
                     return(false);
                 }
             }
         }
     }
     return(false);
 }