When writing network applications, the data buffer zone should be a common method. It is mainly used to build a memory zone to store sent data and received data; to make better use of the existing data buffer zone, a buffer pool is constructed to store the relevant data so that different connections can make better use of the buffer zone, saving the trouble of constantly constructing new buffer zones.
Buffer Zone
In fact, it is very easy to construct a buffer. You can split the byte array of the corresponding size as needed. Since it is used to store data, you need to implement the Read and Write methods. Let's take a look at the specific implementation.
Public class databuffer: idisposable {public byte [] data; private int mlength; private int mpostion = 0; internal int mcount = 0; Public databuffer (byte [] data) {DATA = data; mlength = data. length; mpostion = 0; mcount = data. length;} public databuffer (INT length) {mlength = length; Data = new byte [length];} public void from (array source, int index, int count) {array. copy (source, index, Data, 0, count); mpostion = 0; mcount = count;} public int write (byte [] data) {return write (data, 0 );} public int write (byte [] data, int index) {int COUNT = 0; If (mpostion + (data. length-index)> mlength) {COUNT = mlength-mpostion;} else {COUNT = data. length-index;} If (count> 0) {array. copy (data, index, Data, mpostion, count); mpostion + = count; mcount + = count;} return count;} publi C arraysegment <byte> Read (INT count) {int end = count; If (mpostion + count> mcount) End = mcount-mpostion; arraysegment <byte> result = new arraysegment <byte> (data, mpostion, end); mpostion + = end; return result;} public void seek () {seek (0 );} public void seek (INT postion) {mpostion = 0;} public arraysegment <byte> getsegment () {return New arraysegment <byte> (data, 0, mcount);} internal Bu Fferpool pool {Get; set;} public void dispose () {If (pool! = NULL) {mpostion = 0; mcount = 0; pool. Push (this );}}}
For ease of use, buffer implements the idisposable interface, which is used to put the buffer back into the pool when it is released.
Buffer provides two methods: write and read for writing and reading data. Due to the size limit of the buffer, a number of successful writes will be returned during writing; read returns an arraysegment <byte> to describe its location. In some cases, a data member is written to a different buffer zone. When read, it is stored in multiple buffers.
Buffer Pool
The buffer pool is used to issue and recycle a level punch area for reuse. The implementation of the pool is not complex. encapsulate a simple queue operation.
Public class bufferpool: idisposable {Private Static list <bufferpool> mpools = new list <bufferpool> (); Private Static int mindex = 0; public static void setup (INT pools, int buffers) {setup (pools, buffers, 2048);} public static void setup (INT pools, int buffers, int bufferlength) {lock (mpools) {for (INT I = 0; I <pools; I ++) {mpools. add (New bufferpool (buffers, bufferlength) ;}} public Static void clean () {lock (mpools) {foreach (bufferpool item in mpools) {item. dispose ();} mpools. clear () ;}} public static bufferpool getpool () {lock (mpools) {If (mindex = mpools. count) {mindex = 0;} return mpools [mindex];} queue <databuffer> mbuffers; private int mbufferlength; Public bufferpool (INT count, int bufferlength) {mbufferlength = bufferlength; mbuffers = new queue <databuf Fer> (count); For (INT I = 0; I <count; I ++) {mbuffers. enqueue (createbuffer (bufferlength);} private databuffer createbuffer (INT length) {databuffer item = new databuffer (length); item. pool = This; return item;} public databuffer POP () {lock (mbuffers) {return mbuffers. count> 0? Mbuffers. dequeue (): createbuffer (mbufferlength) ;}} public void push (databuffer buffer) {lock (mbuffers) {mbuffers. enqueue (buffer) ;}} private bool mdisposed = false; private void ondispose () {lock (mbuffers) {While (mbuffers. count> 0) {mbuffers. dequeue (). pool = NULL ;}} public void dispose () {lock (this) {If (! Mdisposed) {ondispose (); mdisposed = true ;}}}}
Bufferpool implements several static methods
Setup
The main purpose is to construct multiple buffer pools, the number of buffers, and the size of the buffer. Why are multiple pools considered? The main reason is that high concurrency is used for allocation and processing to reduce the load of the pool.
Clean
Used to clear the release Buffer Pool
Getpool
Evenly distributed buffer pool for users
A simple data buffer zone and data buffer pool have been implemented.ArticleThis section describes how to construct bufferwriter and bufferreader, write information into multiple buffers according to object requirements, and read information from multiple buffers to restore objects.