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:
Charl Wentzel
2016-07-06 15:17:10 +02:00
parent 1ced40260d
commit 6fee4d0eac
5 changed files with 93 additions and 4 deletions

View File

@@ -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 CBuffer::Clear( int ClearLen )
{ {
int BytesCleared; 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 // Read File descriptor directly into rolling buffer
int CBuffer::ReadFromFD( int Handle, int MaxRead ) int CBuffer::ReadFromFD( int Handle, int MaxRead )
{ {

View File

@@ -40,11 +40,13 @@ public:
int Reset(); int Reset();
int Set( const char * Data, int Len = -1 ); int Set( const char * Data, int Len = -1 );
int Peek( char ** Data, int PeekPos = 0, int MaxLen = -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 ); int Clear( int ClearLen = -1 );
// FiFo operations // FiFo operations
int Push( const char * Data, int Len = -1 ); int Push( const char * Data, int Len = -1 );
int Pop( char ** Data, int MaxLen = -1 ); int Pop( char ** Data, int MaxLen = -1 );
int PopCopy( char ** Data, int MaxLen = -1 );
// File operations // File operations
int ReadFromFD( int Handle, int MaxRead = -1 ); int ReadFromFD( int Handle, int MaxRead = -1 );

View File

@@ -731,7 +731,7 @@ bool CSelectableCore::Read( THandle * Handle )
int BytesWaiting = -1; int BytesWaiting = -1;
// Validate // Validate
if (!Handle) { if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) {
return false; return false;
} }
@@ -808,7 +808,7 @@ bool CSelectableCore::Write( THandle * Handle )
int BytesWritten = 0; int BytesWritten = 0;
// Validate // Validate
if (!Handle) { if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) {
return false; 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 ); LogMessage( DebugLevel, dlHigh, "%s: Local IO '%s' - Input rejected, Input not found", Name, IOName );
return 0; 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 // Log event
ShowOutput( DebugLevel, dlHigh, OutputDisplay, Data, Len, "%s: Local IO '%s' - IN:", Name, IOName ); ShowOutput( DebugLevel, dlHigh, OutputDisplay, Data, Len, "%s: Local IO '%s' - IN:", Name, IOName );
@@ -1099,7 +1106,7 @@ bool CSelectableCore::Process()
// Auto manage handles // Auto manage handles
if (Handle->Auto && ((Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed))) if (Handle->Auto && ((Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)))
{ {
// Reopen handle // Complete opening process
Open( Handle ); Open( Handle );
} }
@@ -1113,7 +1120,7 @@ bool CSelectableCore::Process()
ProcessBuffer( Handle, true ); ProcessBuffer( Handle, true );
// Reset timer // Reset timer
SetInterval( &(Handle->InStart), 0 ); ClearStartTime( &(Handle->InStart) );
} }
} }
Handle = Handle->Next; Handle = Handle->Next;

View File

@@ -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) // Calculate TimePassed from Start Time (in milli-seconds)
long TimePassed( timeval StartTime ) long TimePassed( timeval StartTime )
{ {

View File

@@ -16,8 +16,12 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Manage as Interval
void SetInterval( timeval *Time, long MilliSeconds ); void SetInterval( timeval *Time, long MilliSeconds );
// Manage as Timer
void SetStartTime( timeval *StartTime ); void SetStartTime( timeval *StartTime );
void ClearStartTime( timeval * StartTime );
long TimePassed( timeval StartTime ); long TimePassed( timeval StartTime );
bool Timeout( timeval StartTime, long MilliSeconds ); bool Timeout( timeval StartTime, long MilliSeconds );