コード例 #1
0
        private void SendProcess()
        {
            int surplus = SendBuffer.Buffer.Length;

            while (cmdQueue.Count > 0)
            {
                if (NoComplateCmd != null)
                {
                    int noComplateLength = NoComplateCmd.Buffer.Length - NoComplateCmd.Offset;
                    if (noComplateLength <= SendBuffer.Buffer.Length)
                    {
                        SendBuffer.WriteBuffer(NoComplateCmd.Buffer, NoComplateCmd.Offset, noComplateLength);
                        surplus      -= noComplateLength;
                        NoComplateCmd = null;
                    }
                    else
                    {
                        SendBuffer.WriteBuffer(NoComplateCmd.Buffer, NoComplateCmd.Offset, SendBuffer.Buffer.Length);
                        NoComplateCmd.Offset += SendBuffer.Buffer.Length;
                        surplus -= SendBuffer.Buffer.Length;
                        break;//跳出当前循环
                    }
                }
                if (surplus >= intByteLength)
                {
                    var cmd          = cmdQueue.Dequeue();
                    var cmdAllLength = cmd.Buffer.Length + checkandCmdLength;
                    if (cmdAllLength <= surplus)
                    {
                        SendBuffer.WriteInt(cmd.Buffer.Length + shortByteLength, false); //写入总大小
                        SendBuffer.WriteShort(cmd.CommondId, false);                     //写入命令编号
                        SendBuffer.WriteBuffer(cmd.Buffer);                              //写入命令内容
                        surplus -= cmdAllLength;
                    }
                    else
                    {
                        SendBuffer.WriteInt(cmd.Buffer.Length, false); //写入总大小

                        surplus -= cmd.Buffer.Length;

                        if (surplus >= shortByteLength)
                        {
                            SendBuffer.WriteShort(cmd.CommondId, false); //写入命令编号
                            surplus -= shortByteLength;
                        }
                        if (surplus > 0)
                        {
                            SendBuffer.WriteBuffer(cmd.Buffer, cmd.Offset, surplus); //写入命令内容
                            cmd.Offset = surplus;
                        }
                        NoComplateCmd = cmd;//把未全部发送指令缓存
                    }
                }
                else
                {
                    break;
                }
            }
            if (surplus < SendBuffer.Buffer.Length)
            {
                Session.SendEventArgs.SetBuffer(SendBuffer.Buffer, 0, SendBuffer.DataSize);
                if (Session.ConnectSocket != null)
                {
                    try
                    {
                        bool willRaiseEvent = Session.ConnectSocket.SendAsync(Session.SendEventArgs);
                        if (!willRaiseEvent)
                        {
                            SendComplate(null, Session.SendEventArgs);
                        }
                    }
                    catch
                    {
                        DisConnect();
                    }
                }
                else
                {
                    Interlocked.Decrement(ref isSend);
                }
            }
            else
            {
                Interlocked.Decrement(ref isSend);
            }
        }