Communication between processes involves a ring buffer. This article describes a simple circular buffer. Because it is simple, you can directly put the code below. If you are interested, you can give some constructive suggestions.
Class Structure:
// Configure //-------------------------------------------------------------------------------------------------
// Data exchange zone
Class CBuf
{
//////////////////////////////////////// //////////////////////////////////////// ///////////////////
/// @ Attributes
//////////////////////////////////////// //////////////////////////////////////// ///////////////////
Private:
INT32 iBufLen; // maximum capacity
UCHAR * pBufArea; // swap operation handle
INT32 iRead, iWrite; // read, write cursor
INT32 iByteCnt; // actual length
Public:
// Initialize the memory swap zone based on the specified capacity
CBuf (INT32 BufLen );
// Destructor to release the memory
Virtual ~ CBuf ();
//////////////////////////////////////// //////////////////////////////////////// ///////////////////
/// @ Functions
//////////////////////////////////////// //////////////////////////////////////// ///////////////////
Public:
// Function:
// Reset the swap zone and set iByteCnt to zero. iRead is equal to iWrite.
// Parameters:
// Return:
Void Reset ();
// Function:
// Obtain the actual capacity
// Parameters:
// Return:
INT32 ByteCnt ();
// Function:
// Obtain the remaining writeable capacity
// Parameters:
// Return:
INT32 ByteEmpty ();
// Function:
// Clear, iRead iWrite iByteCnt reset to zero
// Parameters:
// Return:
Void Clear ();
// Function:
// Read data and remove the data from the data exchange zone
// Parameters:
// _ Out UCHAR * pBuf [out] Read data
// _ In int const iCnt [in] Expected read data volume
// Return:
// Actual length of data to be read
// Exception handling:
// When the actual data length is smaller than the expected data size, the actual data length prevails.
INT32 Get (UCHAR * pBuf, int const iCnt );
// Function:
// Write data
// Parameters:
// _ Out UCHAR * pBuf [in] data to be written
// _ In int const iCnt [in] Expected write data volume
// Return:
// Actual length of written data
// Description;
// The expected data volume to be written is larger than the remaining space in the SWAp zone. Take the remaining space as the standard.
INT32 Put (UCHAR * pBuf, int const iCnt );
// Function:
// Read data, but do not remove the data from the data exchange zone
// Parameters:
// _ Out UCHAR * pBuf [out] Read data
// _ In int const iCnt [in] Expected read data volume
// Return:
// Actual length of data to be read
// Description:
// When the actual data length is smaller than the expected data size, the actual data length prevails.
INT32 (UCHAR * pBuf, int const iCnt );
};
Method body implementation:
1 // -------------------------------------------------- constructor -----------------------------------------//
2 CBuf: CBuf (INT32 BufLen)
3 {
4 iBufLen = BufLen;
5 pBufArea = new UCHAR [iBufLen];
6 iRead = iWrite = 0;
7 iByteCnt = 0;
8}
9
10 // -------------------------------------------------- destructor -----------------------------------------//
11 CBuf ::~ CBuf ()
12 {
13 delete [] pBufArea;
14}
15
16 // ---------------------------------------------- obtain the actual capacity --------------------------------------//
17 INT32 CBuf: ByteCnt ()
18 {
19 return (iByteCnt );
20}
21
22 // ---------------------------------------------- obtain the remaining capacity --------------------------------------//
23 INT32 CBuf: ByteEmpty () // number of empty cell lines
24 {
25 return (iBufLen-iByteCnt );
26}
27
28 // ------------------------------------------------------ clear -------------------------------------------//
29 void CBuf: Clear ()
30 {
31 iRead = iWrite = 0;
32 iByteCnt = 0;
33 return;
34}
35
36 // -------------------------------------------------- read data -----------------------------------------//
37 INT32 CBuf: Get (UCHAR * pBuf, int const iCnt)
38 {
39 int iRealCnt;
40
41 iRealCnt = iCnt;
42 if (iByteCnt <iRealCnt) // iByteCnt must be associated with Put ()
43 iRealCnt = iByteCnt; // obtain the actual minimum value of iRealCnt.
44
45 for (int I = 0; I <iRealCnt; I ++)
46 {
47 pBuf [I] = pBufArea [iRead];
48 iRead = (iRead + 1) % iBufLen;
49 iByteCnt --;
50}
51
52 return iRealCnt;
53}
54
55 // -------------------------------------------------- write data -----------------------------------------//
56 INT32 CBuf: Put (UCHAR * pBuf, int const iCnt)
57 {
58 int iRealCnt;
59
60 iRealCnt = iCnt;
61 if (iBufLen-iByteCnt <iRealCnt) // calculate the remaining bytes
62 iRealCnt = iBufLen-iByteCnt;
63
64 for (int I = 0; I <iRealCnt; I ++)
65 {
66 pBufArea [iWrite] = pBuf [I];
67 iWrite = (iWrite + 1) % iBufLen;
68 iByteCnt ++;
69}
70
71 return iRealCnt;
72}
73
74 // ----------------------------------------------------- reset --------------------------------------------//
75 void CBuf: Reset ()
76 {
77 iByteCnt = 0;
78 iRead = iWrite;
79}
80
81 // ----------------------------------- get data, but do not move the read cursor ----------------------------------------//
82 INT32 CBuf: Peek (UCHAR * pBuf, int const iCnt)
83 {
84 int iRealCnt;
85
86 iRealCnt = iCnt;
87 if (iByteCnt <iRealCnt) // iByteCnt must be associated with Put ()
88 iRealCnt = iByteCnt; // obtain the actual minimum value of iRealCnt.
89
90 int iCurRead = iRead;
91 for (int I = 0; I <iRealCnt; I ++)
92 {
93 pBuf [I] = pBufArea [iCurRead];
94 iCurRead = (iCurRead + 1) % iBufLen;
95}
96
97 return iRealCnt;
98}