公司項目中常常設計到串口通訊,TCP通訊,並且大多都是實時的大數據的傳輸,而後你們都知道協議通信確定涉及到什麼,封包、拆包、粘包、校驗……什麼鬼的概念一大堆,說簡單點兒就是要一個高效率可複用的緩存區。按照碼農的慣性思惟就是去百度、谷歌搜索看有沒有現成的東西能夠直接拿來用,然而我並無找到,好吧不是很難的東西本身實現一個唄。開扯……編程
環形隊列是在實際編程極爲有用的數據結構,它有以下特色:數組
它是一個首尾相連的FIFO的數據結構,採用數組的線性空間,數據組織簡單。能很快知道隊列是否滿爲空。能以很快速度的來存取數據。緩存
由於有簡單高效的緣由,甚至在硬件都實現了環形隊列。數據結構
鄙人新手這份代碼確定有不足之處,望你們指出交流,涉及到的多線程同步問題請調用者完成,不廢話直接上代碼。多線程
public class RingBufferManager { public byte[] Buffer { get; set; } // 存放內存的數組 public int DataCount { get; set; } // 寫入數據大小 public int DataStart { get; set; } // 數據起始索引 public int DataEnd { get; set; } // 數據結束索引 public RingBufferManager(int bufferSize) { DataCount = 0; DataStart = 0; DataEnd = 0; Buffer = new byte[bufferSize]; } public byte this[int index] { get { if (index >= DataCount) throw new Exception("環形緩衝區異常,索引溢出"); if (DataStart + index < Buffer.Length) { return Buffer[DataStart + index]; } else { return Buffer[(DataStart + index) - Buffer.Length]; } } } public int GetDataCount() // 得到當前寫入的字節數 { return DataCount; } public int GetReserveCount() // 得到剩餘的字節數 { return Buffer.Length - DataCount; } public void Clear() { DataCount = 0; } public void Clear(int count) // 清空指定大小的數據 { if (count >= DataCount) // 若是須要清理的數據大於現有數據大小,則所有清理 { DataCount = 0; DataStart = 0; DataEnd = 0; } else { if (DataStart + count >= Buffer.Length) { DataStart = (DataStart + count) - Buffer.Length; } else { DataStart += count; } DataCount -= count; } } public void WriteBuffer(byte[] buffer, int offset, int count) { Int32 reserveCount = Buffer.Length - DataCount; if (reserveCount >= count) // 可用空間夠使用 { if (DataEnd + count < Buffer.Length) // 數據沒到結尾 { Array.Copy(buffer, offset, Buffer, DataEnd, count); DataEnd += count; DataCount += count; } else // 數據結束索引超出結尾 循環到開始 { System.Diagnostics.Debug.WriteLine("緩存從新開始...."); Int32 overflowIndexLength = (DataEnd + count) - Buffer.Length; // 超出索引長度 Int32 endPushIndexLength = count - overflowIndexLength; // 填充在末尾的數據長度 Array.Copy(buffer, offset, Buffer, DataEnd, endPushIndexLength); DataEnd = 0; offset += endPushIndexLength; DataCount += endPushIndexLength; if (overflowIndexLength != 0) { Array.Copy(buffer, offset, Buffer, DataEnd, overflowIndexLength); } DataEnd += overflowIndexLength; // 結束索引 DataCount += overflowIndexLength; // 緩存大小 } } else { // 緩存溢出,不處理 } } public void ReadBuffer(byte[] targetBytes,Int32 offset, Int32 count) { if (count > DataCount) throw new Exception("環形緩衝區異常,讀取長度大於數據長度"); Int32 tempDataStart = DataStart; if (DataStart + count < Buffer.Length) { Array.Copy(Buffer, DataStart, targetBytes, offset, count); } else { Int32 overflowIndexLength = (DataStart + count) - Buffer.Length; // 超出索引長度 Int32 endPushIndexLength = count - overflowIndexLength; // 填充在末尾的數據長度 Array.Copy(Buffer, DataStart, targetBytes, offset, endPushIndexLength); offset += endPushIndexLength; if (overflowIndexLength != 0) { Array.Copy(Buffer, 0, targetBytes, offset, overflowIndexLength); } } } public void WriteBuffer(byte[] buffer) { WriteBuffer(buffer, 0, buffer.Length); } }
int len = sConn.Receive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, out se); if (len <= 0) throw new Exception("disconnect.."); if (len > 0) { lock (LockReceiveBuffer) { while (len + receiveBufferManager.DataCount > MAX_BUFFER_LEN) // 緩存溢出處理 { Monitor.Wait(LockReceiveBuffer,10000); } receiveBufferManager.WriteBuffer(receiveBuffer, 0, len); Monitor.PulseAll(LockReceiveBuffer); } }
lock (LockReceiveBuffer) { freame_byte = new byte[frameLen]; receiveBufferManager.ReadBuffer(freame_byte, 0, frameLen); receiveBufferManager.Clear(frameLen); }
TCP大數據連續測試一週沒出現問題內存問題。測試