Introduction
The CRingBuffer
class was designed to be a fast and convenient way of collecting incoming data from a socket, then retrieving it one text line at a time. It intentionally does no dynamic buffer allocation/deallocation during runtime, so as to avoid memory fragmentation and other issues.
It was created in Visual C++ 6 as part of the creation of separate POP3 and SMTP servers. The entire class is contained in a single header file, and uses CString
in one function (and as such, requires MFC, although it would be trivial to remove that dependency).
The CRingBuffer Interface
CRingBuffer();
virtual ~CRingBuffer();
BOOL Create( int iBufSize );
void Destroy();
int GetMaxReadSize();
int GetMaxWriteSize();
BOOL WriteBinary( char * pBuf, int nBufLen );
BOOL ReadBinary( char * pBuf, int nBufLen );
BOOL PeekChar( int iPos, char & ch );
BOOL FindChar( char chLookFor, int & riPos );
BOOL ReadTextLine( CString & strLine );
How To Use It
As previously mentioned, this class is designed for server applications. It probably has very little use outside of that scope. That said, here is an extremely simplified example of how to use it.
#include "RingBuffer.h"
using namespace Stardust;
#define INCOMING_RING_BUFFER_SIZE (1024*16) // or whatever
CRingBuffer m_ringbuf;
m_ringbuf.Create( INCOMING_RING_BUFFER_SIZE + 1 );
char m_chInBuf[ INCOMING_BUFFER_SIZE + 1 ];
...
int iNumberOfBytesRead = 0;
while( READ_INCOMING(m_chInBuf,INCOMING_BUFFER_SIZE,&iNumberOfBytesRead,0)
&& iNumberOfBytesRead > 0 )
{
m_ringbuf.WriteBinary(m_chInBuf,iNumberOfBytesRead);
CString strLine;
while( m_ringbuf.ReadTextLine(strLine) )
{
strLine.TrimRight("\r\n");
if( !ON_INCOMING( strLine ) )
return FALSE;
}
}
In the above example, READ_INCOMING
and ON_INCOMING
are placeholders for functions that read and accept incoming data respectively. READ_INCOMING
reads raw incoming data from the socket while ON_INCOMING
accepts incoming data from the socket, one line at a time.
I'm a former Microsoft employee (ack!) who was the Primary Developer of Microsoft Scenes 1.0 and the Lead Developer of Microsoft Scenes 2.0. While there, I also worked on the TrueType Font Embedding Library, wrote the Win32 GetUnicodeCodepoints API (which mysteriously disappeared into the void), worked on Font Assistant, Word Assistant, and Office Assistant (long forgotten applications), and did pioneering work on what would eventually become Microsoft PictureIt!.
In 1995, I left Microsoft and founded Stardust Software which I currently own and operate.
My programming experience is primarily in the C/C++/MFC world, secondarily in ASP/JavaScript... with a little COM/ATL sprinkled on top.
Or something like all of that...