// 压入发送数据 public bool PushSendData(UInt16 msgCommand, byte[] dataBuffer, Int32 bufferOffset, UInt16 bufferLength) { SendDataBuffer sendBuffer = null; lock (sendBufferListLock) { if (sendBufferList.Count() > 0) { sendBuffer = sendBufferList[sendBufferList.Count() - 1]; if (sendBuffer != null && sendBuffer.GetFreeSpace() < (bufferLength + NetGlobalData.GetInstance().GetMsgHeadSize())) { sendBuffer = null; } } if (sendBuffer == null) { sendBuffer = DataBufferPool.GetInstance().MallocSendBuffer(); if (sendBuffer == null) { return(false); } // 添加新的 Buffer sendBufferList.Add(sendBuffer); } return(sendBuffer.PushSendData(msgCommand, dataBuffer, bufferOffset, bufferLength)); } }
// 发送消息成功(对写入线程和发送线程分离) public bool SendComplete(Int32 sendLength) { lock (sendBufferListLock) { if (!isAsyncSending || sendBufferList.Count() <= 0) { Trace.Assert(isAsyncSending, "isAsyncSending is false"); Trace.Assert(sendBufferList.Count() > 0, "sendBufferList is empty"); return(false); } SendDataBuffer sendBuffer = sendBufferList[0]; if (sendBuffer == null) { Trace.Assert(false, "sendBufferList element is null"); return(false); } // 释放多余的缓冲区 bool retValue = sendBuffer.SendComplete(sendLength); if (sendBufferList.Count() > 1 && sendBuffer.IsEmpty()) { DataBufferPool.GetInstance().FreeSendBuffer(sendBuffer); sendBufferList.RemoveAt(0); } // 重置发送标记 isAsyncSending = false; return(retValue); } }
// 初始化对象 public bool Init() { sendBufferListLock = new object(); if (sendBufferListLock == null) { return(false); } sendBufferList = new List <SendDataBuffer>(); if (sendBufferList == null) { sendBufferListLock = null; return(false); } SendDataBuffer sendBuffer = DataBufferPool.GetInstance().MallocSendBuffer(); if (sendBuffer == null) { sendBufferList = null; sendBufferListLock = null; return(false); } sendBufferList.Add(sendBuffer); return(true); }
// 释放对象 public void Release() { if (sendBufferList != null) { foreach (SendDataBuffer sendBuffer in sendBufferList) { if (sendBuffer != null) { DataBufferPool.GetInstance().FreeSendBuffer(sendBuffer); } } sendBufferList.Clear(); sendBufferList = null; } sendBufferListLock = null; }
// 重置所有发送缓冲区 public void Reset() { Trace.Assert(!isAsyncSending, "isAsyncSending is true"); isAsyncSending = false; if (sendBufferList != null) { // 释放多余的发送缓冲区 while (sendBufferList.Count() > 1) { if (sendBufferList[0] != null) { DataBufferPool.GetInstance().FreeSendBuffer(sendBufferList[0]); } sendBufferList.RemoveAt(0); } if (sendBufferList.Count() > 0 && sendBufferList[0] != null) { sendBufferList[0].Reset(); } } }