// SerialBuffer.h: interface for the CSerialBuffer class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_)
#define AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string>
#include "robbase.h"



class ROBBASE_API  CSerialBuffer  
{
	//std::string m_szInternalBuffer;
	char*  m_szInternalBuffer;

	CRITICAL_SECTION	m_csLock;
	bool	m_abLockAlways;
	long	m_iCurPos;
	long    m_alBytesUnRead;
	long    m_nReadBufferSize;
	
	void	ClearAndReset(HANDLE& hEventToReset);
public:
	inline void LockBuffer() {::EnterCriticalSection(&m_csLock );}
	inline void UnLockBuffer() {::LeaveCriticalSection(&m_csLock );} 
	 
	
	CSerialBuffer( );
	virtual ~CSerialBuffer();

	BOOL   Init(int buffSize);
	void   Release();

	//---- public interface --
	void AddData( char ch ) ;
	//void AddData( std::string& szData ) ;
	//void AddData( std::string& szData,int iLen ) ;
	void AddData( char *strData,int iLen ) ;
	const char* GetData() {return m_szInternalBuffer;}

	void		Flush();

	long	Read_N( char* pszData,long alCount,HANDLE& hEventToReset);
    bool	Read_Upto( char* pszData,char chTerm,long  &alBytesRead ,HANDLE& hEventToReset);
	//bool	Read_Available( char* pszData, int size, HANDLE & hEventToReset);
	bool    Read_Upto_FIX(char* pszData, char chTerm,long  &alBytesRead,HANDLE & hEventToReset);


	inline long GetSize() {return m_alBytesUnRead;}
	inline bool IsEmpty() {return m_alBytesUnRead == 0; }

};

#endif // !defined(AFX_SerialBuffer_H__FD164A5F_9F09_451B_8E42_180999B4DB9A__INCLUDED_)
