Important Update:
- BufferCore: - Added PeekCopy and PopCopy methods - SelectableCore: - Add status checking of Handle on Read and Write - Reject input if Handle not open - TimingCore: - Added method ClearStartTime
This commit is contained in:
@@ -120,6 +120,56 @@ int CBuffer::Peek( char ** Data, int PeekPos, int MaxLen )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Return "stitched" buffer data in temporary buffer
|
||||
int CBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
|
||||
{
|
||||
int BytesReturned = 0;
|
||||
int StartPos = 0;
|
||||
|
||||
// Check if any data
|
||||
if (!BufLen || !MaxLen) {
|
||||
if (*Data)
|
||||
(*Data)[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Validate start position
|
||||
if (PeekPos >= BufLen) {
|
||||
if (*Data)
|
||||
(*Data)[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
else if (BufStart + StartPos < BufSize) {
|
||||
StartPos = BufStart + PeekPos;
|
||||
}
|
||||
else {
|
||||
StartPos = BufStart + PeekPos - BufSize;
|
||||
}
|
||||
|
||||
// Validate MaxLen
|
||||
if ((MaxLen == -1) || (MaxLen > BufSize)) {
|
||||
MaxLen = BufSize;
|
||||
}
|
||||
BytesReturned = (MaxLen > BufLen - PeekPos)? (BufLen - PeekPos) : MaxLen;
|
||||
|
||||
// Allocate memory
|
||||
if (!*Data)
|
||||
*Data = (char*)malloc( BytesReturned + 1 );
|
||||
|
||||
// Copy Data from Buffer
|
||||
if (StartPos + BytesReturned <= BufSize) {
|
||||
memcpy( *Data, &Buffer[StartPos], BytesReturned );
|
||||
}
|
||||
else {
|
||||
memcpy( *Data, &Buffer[StartPos], BufSize-StartPos ); // Copy from end of buffer
|
||||
memcpy( &(*Data)[BufSize-StartPos], Buffer, (BytesReturned-(BufSize-StartPos)) ); // Copy rollover from start of buffer
|
||||
}
|
||||
(*Data)[BytesReturned] = 0;
|
||||
|
||||
return BytesReturned;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
int CBuffer::Clear( int ClearLen )
|
||||
{
|
||||
int BytesCleared;
|
||||
@@ -223,6 +273,23 @@ int CBuffer::Pop( char ** Data, int MaxLen )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Read first-in bytes from buffer
|
||||
// Return "stitched" buffer data in temporary buffer
|
||||
int CBuffer::PopCopy( char ** Data, int MaxLen )
|
||||
{
|
||||
int BytesWritten = 0;
|
||||
|
||||
// Read data
|
||||
BytesWritten = PeekCopy( Data, MaxLen );
|
||||
|
||||
// Clear bytes
|
||||
Clear( BytesWritten );
|
||||
|
||||
// Return temp buffer
|
||||
return BytesWritten;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Read File descriptor directly into rolling buffer
|
||||
int CBuffer::ReadFromFD( int Handle, int MaxRead )
|
||||
{
|
||||
|
||||
@@ -40,11 +40,13 @@ public:
|
||||
int Reset();
|
||||
int Set( const char * Data, int Len = -1 );
|
||||
int Peek( char ** Data, int PeekPos = 0, int MaxLen = -1 );
|
||||
int PeekCopy( char ** Data, int PeekPos = 0, int MaxLen = -1 );
|
||||
int Clear( int ClearLen = -1 );
|
||||
|
||||
// FiFo operations
|
||||
int Push( const char * Data, int Len = -1 );
|
||||
int Pop( char ** Data, int MaxLen = -1 );
|
||||
int PopCopy( char ** Data, int MaxLen = -1 );
|
||||
|
||||
// File operations
|
||||
int ReadFromFD( int Handle, int MaxRead = -1 );
|
||||
|
||||
@@ -731,7 +731,7 @@ bool CSelectableCore::Read( THandle * Handle )
|
||||
int BytesWaiting = -1;
|
||||
|
||||
// Validate
|
||||
if (!Handle) {
|
||||
if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -808,7 +808,7 @@ bool CSelectableCore::Write( THandle * Handle )
|
||||
int BytesWritten = 0;
|
||||
|
||||
// Validate
|
||||
if (!Handle) {
|
||||
if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1021,6 +1021,13 @@ int CSelectableCore::Input( const char * IOName, const char * Data, int Len )
|
||||
LogMessage( DebugLevel, dlHigh, "%s: Local IO '%s' - Input rejected, Input not found", Name, IOName );
|
||||
return 0;
|
||||
}
|
||||
// Check that handle is open
|
||||
else if (Handle->State != csOpen)
|
||||
{
|
||||
// Log event
|
||||
LogMessage( DebugLevel, dlHigh, "%s: Local IO '%s' - Input rejected, Handle not Open", Name, IOName );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Log event
|
||||
ShowOutput( DebugLevel, dlHigh, OutputDisplay, Data, Len, "%s: Local IO '%s' - IN:", Name, IOName );
|
||||
@@ -1099,7 +1106,7 @@ bool CSelectableCore::Process()
|
||||
// Auto manage handles
|
||||
if (Handle->Auto && ((Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)))
|
||||
{
|
||||
// Reopen handle
|
||||
// Complete opening process
|
||||
Open( Handle );
|
||||
}
|
||||
|
||||
@@ -1113,7 +1120,7 @@ bool CSelectableCore::Process()
|
||||
ProcessBuffer( Handle, true );
|
||||
|
||||
// Reset timer
|
||||
SetInterval( &(Handle->InStart), 0 );
|
||||
ClearStartTime( &(Handle->InStart) );
|
||||
}
|
||||
}
|
||||
Handle = Handle->Next;
|
||||
|
||||
@@ -33,6 +33,15 @@ void SetStartTime( timeval * StartTime )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Clear start time
|
||||
void ClearStartTime( timeval * StartTime )
|
||||
{
|
||||
// Get current time
|
||||
StartTime->tv_sec = 0;
|
||||
StartTime->tv_usec = 0;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Calculate TimePassed from Start Time (in milli-seconds)
|
||||
long TimePassed( timeval StartTime )
|
||||
{
|
||||
|
||||
@@ -16,8 +16,12 @@
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Manage as Interval
|
||||
void SetInterval( timeval *Time, long MilliSeconds );
|
||||
|
||||
// Manage as Timer
|
||||
void SetStartTime( timeval *StartTime );
|
||||
void ClearStartTime( timeval * StartTime );
|
||||
long TimePassed( timeval StartTime );
|
||||
bool Timeout( timeval StartTime, long MilliSeconds );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user