void FlushBuffer(ref int surplus) { while (sendDataQueue.Count > 0) { if (noComplateBytes != null) { int noComplateLength = noComplateBytes.Length - noComplateOffset; if (noComplateLength <= surplus) { SendBuffer.WriteBuffer(noComplateBytes, noComplateOffset, noComplateLength); surplus -= noComplateLength; noComplateBytes = null; noComplateOffset = 0; } else { SendBuffer.WriteBuffer(noComplateBytes, noComplateOffset, surplus); noComplateOffset += surplus; surplus -= surplus; break; } } if (surplus >= intByteLength) { byte[] data; if (sendDataQueue.TryDequeue(out data)) { var PacketAllLength = data.Length + intByteLength; if (PacketAllLength <= surplus) { SendBuffer.WriteInt(data.Length, NetByteOrder); //写入总大小 SendBuffer.WriteBuffer(data); //写入命令内容 surplus -= PacketAllLength; } else { SendBuffer.WriteInt(data.Length, NetByteOrder); //写入总大小 surplus -= intByteLength;; if (surplus > 0) { SendBuffer.WriteBuffer(data, 0, surplus); //写入命令内容 noComplateOffset = surplus; } noComplateBytes = data;//把未全部发送指令缓存 break; } } } } }
public void SendProcess() { SendBuffer.DataSize = dbUIntByteLength; //清除已发送的包 int dataBufferSize = SendBuffer.Buffer.Length - dbUIntByteLength; int surplus = dataBufferSize; while (sendDataQueue.Count > 0) { if (NoComplateCmd != null) { int noComplateLength = NoComplateCmd.Data.Length - NoComplateCmd.Offset; if (noComplateLength <= surplus) { SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset, noComplateLength); surplus -= noComplateLength; NoComplateCmd = null; } else { SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset, surplus); NoComplateCmd.Offset += surplus; surplus -= surplus; break;//跳出当前循环 } } if (surplus >= uIntByteLength) { SendData data; if (sendDataQueue.TryDequeue(out data)) { var PacketAllLength = data.Data.Length + uIntByteLength; if (PacketAllLength <= surplus) { SendBuffer.WriteInt(data.Data.Length, false); //写入总大小 SendBuffer.WriteBuffer(data.Data); //写入命令内容 surplus -= PacketAllLength; } else { SendBuffer.WriteInt(data.Data.Length, false); //写入总大小 surplus -= uIntByteLength;; if (surplus > 0) { SendBuffer.WriteBuffer(data.Data, data.Offset, surplus); //写入命令内容 data.Offset = surplus; } NoComplateCmd = data;//把未全部发送指令缓存 break; } } } else { break; } } if (surplus < dataBufferSize) { if (Session.EndPoint != null) { Send(); } else { isSend = false; } } else { isSend = false; } }
public void SendProcess() { SendBuffer.Clear(); //清除已发送的包 int surplus = SendBuffer.Buffer.Length; while (sendDataQueue.Count > 0) { if (NoComplateCmd != null) { int noComplateLength = NoComplateCmd.Data.Length - NoComplateCmd.Offset; if (noComplateLength <= surplus) { SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset, noComplateLength); surplus -= noComplateLength; NoComplateCmd = null; } else { SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset, surplus); NoComplateCmd.Offset += surplus; surplus -= surplus; break;//跳出当前循环 } } if (surplus >= intByteLength) { SendData data; if (sendDataQueue.TryDequeue(out data)) { var PacketAllLength = data.Data.Length + intByteLength; if (PacketAllLength <= surplus) { SendBuffer.WriteInt(data.Data.Length, false); //写入总大小 SendBuffer.WriteBuffer(data.Data); //写入命令内容 surplus -= PacketAllLength; } else { SendBuffer.WriteInt(data.Data.Length, false); //写入总大小 surplus -= intByteLength;; if (surplus > 0) { SendBuffer.WriteBuffer(data.Data, data.Offset, surplus); //写入命令内容 data.Offset = surplus; } NoComplateCmd = data;//把未全部发送指令缓存 break; } } } else { break; } } if (surplus < SendBuffer.Buffer.Length) { if (Session.ConnectSocket != null) { Session.SendEventArgs.SetBuffer(SendBuffer.Buffer, 0, SendBuffer.DataSize); bool willRaiseEvent = Session.ConnectSocket.SendAsync(Session.SendEventArgs); if (!willRaiseEvent) { Session.SendComplate(); } } else { isSend = false; } } else { isSend = false; } }
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); } }