C#環形緩沖區(隊列)完整完成。本站提示廣大學習愛好者:(C#環形緩沖區(隊列)完整完成)文章只能為提供參考,不一定能成為您想要的結果。以下是C#環形緩沖區(隊列)完整完成正文
公司項目中常常設計到串口通訊,TCP通訊,並且年夜多都是及時的年夜數據的傳輸,然後年夜家都曉得協定通信確定觸及到甚麼,封包、拆包、粘包、校驗……甚麼鬼的概念一年夜堆,說簡略點兒就是要一個高效力可復用的緩存區。依照碼農的慣性思想就是去百度、谷歌搜刮看有無現成的器械可以直接拿來用,但是我並沒有找到,好吧不是很難的器械本身完成一個呗。開扯……
為何要用環形隊列?
環形隊列是在現實編程極其有效的數據構造,它有以下特色:
它是一個首尾相連的FIFO的數據構造,采取數組的線性空間,數據組織簡略。能很快曉得隊列能否滿為空。能以很疾速度的來存取數據。
由於有簡略高效的緣由,乃至在硬件都完成了環形隊列。
C#完整完成(可直接應用)
不才老手這份代碼確定有缺乏的地方,望年夜家指出交換,觸及到的多線程同步成績請挪用者完成,不空話直接上代碼。
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年夜數據持續測試一周沒湧現成績內存成績。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。