CharBufferCore:

- Bug fix - FindStr() for input marker not matching correctly
SeletableBare/Core:
- Reformat code - eliminate & compact {} where needed
This commit is contained in:
2021-10-12 10:14:35 -05:00
parent c4fc72ca23
commit e5f8a8347a
3 changed files with 304 additions and 517 deletions

View File

@@ -266,10 +266,10 @@ int CRollingBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen )
// Look for first occurrence of character in buffer // Look for first occurrence of character in buffer
bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos, int StartPos ) bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos, int StartPos )
{ {
char * CheckPos = NULL; const char * CheckPos = NULL;
char * CheckLimit = NULL; const char * CheckLimit = NULL;
char * MatchPos = NULL; const char * MatchPos = NULL;
char * MatchLimit = NULL; const char * MatchLimit = NULL;
// Check if buffer exists // Check if buffer exists
if (!BufSize || !SearchStr || !SearchLen) { if (!BufSize || !SearchStr || !SearchLen) {
@@ -288,8 +288,8 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP
CheckLimit = &Buffer[BufSize-1]; CheckLimit = &Buffer[BufSize-1];
// Set Match start point and limit // Set Match start point and limit
MatchPos = (char*)SearchStr; MatchPos = SearchStr;
MatchLimit = (char*)&SearchStr[SearchLen-1]; MatchLimit = &SearchStr[SearchLen-1];
// Search for char // Search for char
FoundPos = StartPos; FoundPos = StartPos;
@@ -307,9 +307,10 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP
MatchPos++; MatchPos++;
} }
} }
else { else if (MatchPos != SearchStr){
// Reset match point // Reset match point
MatchPos = (char*)SearchStr; FoundPos -= MatchPos - SearchStr;
MatchPos = SearchStr;
} }
// Next char // Next char

View File

@@ -46,12 +46,10 @@ CSelectableBare::~CSelectableBare()
THandle * NextHandle = NULL; THandle * NextHandle = NULL;
// Destroy File Handles // Destroy File Handles
while (FirstHandle) while (FirstHandle) {
{
// Close handle if open // Close handle if open
if ((FirstHandle->State == csOpen) || (FirstHandle->State == csWaitingtoOpen)) { if ((FirstHandle->State == csOpen) || (FirstHandle->State == csWaitingtoOpen))
Close( FirstHandle, false ); Close( FirstHandle, false );
}
NextHandle = FirstHandle->Next; NextHandle = FirstHandle->Next;
DestroyHandle( FirstHandle ); DestroyHandle( FirstHandle );
@@ -82,8 +80,7 @@ THandle * CSelectableBare::CreateHandle( const char * HandleName, bool CreateCh
Handle = &((*Handle)->Next); Handle = &((*Handle)->Next);
// Create if necessary // Create if necessary
if (!*Handle) if (!*Handle) {
{
// Create File handle at end of list // Create File handle at end of list
*Handle = new THandle; *Handle = new THandle;
@@ -103,9 +100,8 @@ THandle * CSelectableBare::CreateHandle( const char * HandleName, bool CreateCh
} }
// Create Matching Channel // Create Matching Channel
if (CreateChannel) { if (CreateChannel)
(*Handle)->Channel = AddChannel( HandleName, CH_off ); (*Handle)->Channel = AddChannel( HandleName, CH_off );
}
return *Handle; return *Handle;
} }
@@ -116,19 +112,17 @@ bool CSelectableBare::RemoveHandle( THandle * Handle )
THandle ** HandlePtr = NULL; THandle ** HandlePtr = NULL;
// Validate // Validate
if (!Handle || (Handle->State == csOpen) || (Handle->State == csWaitingtoOpen)) { if (!Handle || (Handle->State == csOpen) || (Handle->State == csWaitingtoOpen))
return false; return false;
}
// Find in List // Find in List
HandlePtr = &FirstHandle; HandlePtr = &FirstHandle;
while (*HandlePtr && (*HandlePtr != Handle)) { while (*HandlePtr && (*HandlePtr != Handle))
HandlePtr = &((*HandlePtr)->Next); HandlePtr = &((*HandlePtr)->Next);
}
// Remove from list if found // Remove from list if found
if (*HandlePtr) { if (*HandlePtr)
*HandlePtr = (*HandlePtr)->Next; *HandlePtr = (*HandlePtr)->Next;
}
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Removed", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Removed",
@@ -193,9 +187,8 @@ bool CSelectableBare::HandleState( THandle * Handle, EConnectState State )
bool CSelectableBare::SetCallback( THandle * Handle, EConnectState pState, FHandleCallback pCallback ) bool CSelectableBare::SetCallback( THandle * Handle, EConnectState pState, FHandleCallback pCallback )
{ {
// Validate // Validate
if (!Handle) { if (!Handle)
return false; return false;
}
// Set callback // Set callback
Handle->StateCallback[ (int)pState ] = pCallback; Handle->StateCallback[ (int)pState ] = pCallback;
@@ -206,9 +199,8 @@ bool CSelectableBare::SetCallback( THandle * Handle, EConnectState pState, FHand
bool CSelectableBare::SetAutoManage( THandle * Handle, bool AutoManage, bool Persistent, int ReopenDelay, int CloseTimeout ) bool CSelectableBare::SetAutoManage( THandle * Handle, bool AutoManage, bool Persistent, int ReopenDelay, int CloseTimeout )
{ {
// Validate // Validate
if (!Handle) { if (!Handle)
return false; return false;
}
// Set params // Set params
Handle->AutoManage = AutoManage; Handle->AutoManage = AutoManage;
@@ -222,18 +214,16 @@ bool CSelectableBare::SetAutoManage( THandle * Handle, bool AutoManage, bool Per
bool CSelectableBare::SetInBuffer( THandle * Handle, int InBufSize, int InTimeout, const char * InMarker, int InMarkerLen ) bool CSelectableBare::SetInBuffer( THandle * Handle, int InBufSize, int InTimeout, const char * InMarker, int InMarkerLen )
{ {
// Validate // Validate
if (!Handle) { if (!Handle)
return false; return false;
}
// Input Buffer // Input Buffer
if (Handle->InBuffer) { if (Handle->InBuffer) {
delete Handle->InBuffer; delete Handle->InBuffer;
Handle->InBuffer = NULL; Handle->InBuffer = NULL;
} }
if (InBufSize) { if (InBufSize)
Handle->InBuffer = (CRollingBuffer*) new CRollingBuffer( InBufSize ); Handle->InBuffer = (CRollingBuffer*) new CRollingBuffer( InBufSize );
}
// Set Input Timeout // Set Input Timeout
Handle->InTimeout = InTimeout; Handle->InTimeout = InTimeout;
@@ -253,18 +243,16 @@ bool CSelectableBare::SetInBuffer( THandle * Handle, int InBufSize, int InTimeou
bool CSelectableBare::SetOutBuffer( THandle * Handle, int OutBufSize ) bool CSelectableBare::SetOutBuffer( THandle * Handle, int OutBufSize )
{ {
// Validate // Validate
if (!Handle) { if (!Handle)
return false; return false;
}
// Output Buffer // Output Buffer
if (Handle->OutBuffer) { if (Handle->OutBuffer) {
delete Handle->OutBuffer; delete Handle->OutBuffer;
Handle->OutBuffer = NULL; Handle->OutBuffer = NULL;
} }
if (OutBufSize) { if (OutBufSize)
Handle->OutBuffer = (CRollingBuffer*) new CRollingBuffer( OutBufSize ); Handle->OutBuffer = (CRollingBuffer*) new CRollingBuffer( OutBufSize );
}
return true; return true;
} }
@@ -277,44 +265,38 @@ bool CSelectableBare::ProcessInputBuffer( THandle * Handle, bool Force )
char * Data = NULL; char * Data = NULL;
// Check if buffered data // Check if buffered data
if (!Handle || !Handle->InBuffer || !Handle->InBuffer->Len()) { if (!Handle || !Handle->InBuffer || !Handle->InBuffer->Len())
return false; return false;
}
// Check if forced processed // Check if forced processed
if (Force || (!Handle->InTimeout && !Handle->InMarkerLen)) if (Force || (!Handle->InTimeout && !Handle->InMarkerLen)) {
{
// Show Packet // Show Packet
Len = Handle->InBuffer->Peek( &Data ); Len = Handle->InBuffer->Peek( &Data );
if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-T:", if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-T:",
ProcessName, Name, Handle->Name ); ProcessName, Name, Handle->Name );
// Write buffer to Outputs // Write buffer to Outputs
if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote)) { if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote))
Output( Handle->Parent->Channel, NULL, true, Data, Len ); Output( Handle->Parent->Channel, NULL, true, Data, Len );
} else { else
Output( Handle->Channel, NULL, true, Data, Len ); Output( Handle->Channel, NULL, true, Data, Len );
}
// Clear processed bytes from buffer // Clear processed bytes from buffer
Handle->InBuffer->Clear( Len ); Handle->InBuffer->Clear( Len );
} }
else else {
{
// Search for end of packet marker // Search for end of packet marker
while (Handle->InBuffer->FindStr( Handle->InMarker, Handle->InMarkerLen, Pos )) while (Handle->InBuffer->FindStr( Handle->InMarker, Handle->InMarkerLen, Pos )) {
{
// Show Packet // Show Packet
Len = Handle->InBuffer->Peek( &Data, 0, Pos+Handle->InMarkerLen ); Len = Handle->InBuffer->Peek( &Data, 0, Pos+Handle->InMarkerLen );
if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-M:", if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-M:",
ProcessName, Name, Handle->Name ); ProcessName, Name, Handle->Name );
// Write buffer to Outputs // Write buffer to Outputs
if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote)) { if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote))
Output( Handle->Parent->Channel, NULL, true, Data, Len ); Output( Handle->Parent->Channel, NULL, true, Data, Len );
} else { else
Output( Handle->Channel, NULL, true, Data, Len ); Output( Handle->Channel, NULL, true, Data, Len );
}
// Clear processed bytes from buffer // Clear processed bytes from buffer
Handle->InBuffer->Clear( Len ); Handle->InBuffer->Clear( Len );
@@ -330,15 +312,13 @@ int CSelectableBare::Open( THandle * Handle )
// Validate // Validate
if (!Handle || (Handle->Type == ctNone)) { if (!Handle || (Handle->Type == ctNone))
return -1; return -1;
} else if (Handle->State == csOpen) { else if (Handle->State == csOpen)
return Handle->FD; return Handle->FD;
}
// Check if port exits // Check if port exits
if (access( Handle->Path, F_OK ) != 0) if (access( Handle->Path, F_OK ) != 0) {
{
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Path not found [%s]", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Path not found [%s]",
ProcessName, Name, Handle->Name, Handle->Path ); ProcessName, Name, Handle->Name, Handle->Path );
@@ -347,8 +327,7 @@ int CSelectableBare::Open( THandle * Handle )
// Open Port // Open Port
Handle->FD = open( Handle->Path, O_RDWR ); Handle->FD = open( Handle->Path, O_RDWR );
if (Handle->FD == -1) if (Handle->FD == -1) {
{
// Log event // Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Path [%s]", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Path [%s]",
ProcessName, Name, Handle->Name, Handle->Path ); ProcessName, Name, Handle->Name, Handle->Path );
@@ -360,9 +339,8 @@ int CSelectableBare::Open( THandle * Handle )
ProcessName, Name, Handle->Name, Handle->Path ); ProcessName, Name, Handle->Name, Handle->Path );
// Add to Select Lists // Add to Select Lists
if (Selector) { if (Selector)
Selector->Add( Handle->FD, true, false, Handle, this ); Selector->Add( Handle->FD, true, false, Handle, this );
}
// Set timer (for re-open or auto-close) // Set timer (for re-open or auto-close)
SetStartTime( &Handle->LastAction ); SetStartTime( &Handle->LastAction );
@@ -387,10 +365,9 @@ bool CSelectableBare::Close( THandle * Handle, bool QuickReopen )
// Remove from Select List // Remove from Select List
if (!Fail && Selector) { if (!Fail && Selector) {
if (Handle->Type != ctUDPremote) { if (Handle->Type != ctUDPremote)
Selector->Remove( Handle->FD, true, true ); Selector->Remove( Handle->FD, true, true );
} }
}
// Reset FD // Reset FD
Handle->FD = ((Fail)? Handle->FD : -1); Handle->FD = ((Fail)? Handle->FD : -1);
@@ -418,17 +395,15 @@ bool CSelectableBare::Read( THandle * Handle )
int BytesWaiting = -1; int BytesWaiting = -1;
// Validate // Validate
if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) { if (!Handle || (Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed))
return false; return false;
}
// Log Read Event // Log Read Event
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Read Event", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Read Event",
ProcessName, Name, Handle->Name ); ProcessName, Name, Handle->Name );
// Check if socket ready (non-block open in progress) // Check if socket ready (non-block open in progress)
if (Handle->State == csWaitingtoOpen) if (Handle->State == csWaitingtoOpen) {
{
Open( Handle ); Open( Handle );
// Reset Timer (for auto-close) // Reset Timer (for auto-close)
@@ -440,8 +415,7 @@ bool CSelectableBare::Read( THandle * Handle )
ioctl( Handle->FD, FIONREAD, &BytesWaiting ); ioctl( Handle->FD, FIONREAD, &BytesWaiting );
// Error on port // Error on port
if (BytesWaiting < 0) if (BytesWaiting < 0) {
{
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)",
ProcessName, Name, Handle->Name, strerror(errno) ); ProcessName, Name, Handle->Name, strerror(errno) );
@@ -451,13 +425,11 @@ bool CSelectableBare::Read( THandle * Handle )
} }
// Validate // Validate
if (Handle->State != csOpen) { if (Handle->State != csOpen)
return false; return false;
}
// Read File directly into buffer // Read File directly into buffer
if (Handle->InBuffer) if (Handle->InBuffer) {
{
errno = 0; errno = 0;
BytesRead = Handle->InBuffer->ReadFromFD( Handle->FD, BytesWaiting ); BytesRead = Handle->InBuffer->ReadFromFD( Handle->FD, BytesWaiting );
@@ -471,11 +443,10 @@ bool CSelectableBare::Read( THandle * Handle )
ProcessName, Name, Handle->Name, BytesRead, BytesWaiting ); ProcessName, Name, Handle->Name, BytesRead, BytesWaiting );
} }
if (BytesRead != 0) {
// Process Buffer // Process Buffer
if (BytesRead != 0)
ProcessInputBuffer( Handle, false ); ProcessInputBuffer( Handle, false );
} }
}
// Reset timer // Reset timer
SetStartTime( &(Handle->InStart) ); SetStartTime( &(Handle->InStart) );
@@ -495,16 +466,13 @@ bool CSelectableBare::Write( THandle * Handle )
return false; return false;
// Is Handle open? // Is Handle open?
if ((Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) if ((Handle->State == csNone) || (Handle->State == csFailed) || (Handle->State == csClosed)) {
{
// May it be opened? // May it be opened?
if (!Handle->AutoManage) if (!Handle->AutoManage) {
{
// Must be opened manually // Must be opened manually
return false; return false;
} }
else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) {
{
// Attempt to re-open port // Attempt to re-open port
Open( Handle ); Open( Handle );
} }
@@ -514,8 +482,7 @@ bool CSelectableBare::Write( THandle * Handle )
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Write Event", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Write Event",
ProcessName, Name, Handle->Name ); ProcessName, Name, Handle->Name );
if (Handle->State == csWaitingtoOpen) if (Handle->State == csWaitingtoOpen) {
{
// Complete socket open process // Complete socket open process
Open( Handle ); Open( Handle );
@@ -523,15 +490,12 @@ bool CSelectableBare::Write( THandle * Handle )
SetStartTime( &(Handle->LastAction) ); SetStartTime( &(Handle->LastAction) );
// Remove from set for select write // Remove from set for select write
if (Selector) { if (Selector)
Selector->Remove( Handle->FD, false, true ); Selector->Remove( Handle->FD, false, true );
}
return true; return true;
} }
else if (Handle->State == csOpen) else if (Handle->State == csOpen) {
{ if (Handle->OutBuffer) {
if (Handle->OutBuffer)
{
// Write directly to handle / socket // Write directly to handle / socket
errno = 0; errno = 0;
BytesWritten = Handle->OutBuffer->WriteToFD( Handle->FD ); BytesWritten = Handle->OutBuffer->WriteToFD( Handle->FD );
@@ -549,30 +513,26 @@ bool CSelectableBare::Write( THandle * Handle )
ProcessName, Name, Handle->Name, BytesWritten, Len ); ProcessName, Name, Handle->Name, BytesWritten, Len );
} }
if (BytesWritten != 0) if (BytesWritten != 0) {
{
// Update Buffer // Update Buffer
Handle->OutBuffer->Clear( (BytesWritten > 0)? BytesWritten : -BytesWritten ); // negative value reported if error occurred Handle->OutBuffer->Clear( (BytesWritten > 0)? BytesWritten : -BytesWritten ); // negative value reported if error occurred
// Check if Buffer empty // Check if Buffer empty
if (!Handle->OutBuffer->Len()) { if (!Handle->OutBuffer->Len()) {
// Remove from Select Write list // Remove from Select Write list
if (Selector) { if (Selector)
Selector->Remove( Handle->FD, false, true ); Selector->Remove( Handle->FD, false, true );
} }
}
// Reset timeout // Reset timeout
SetStartTime( &(Handle->LastAction) ); SetStartTime( &(Handle->LastAction) );
} }
} }
else else {
{
// No Output buffer to write from, so remove from Write list // No Output buffer to write from, so remove from Write list
if (Selector) { if (Selector)
Selector->Remove( Handle->FD, false, true ); Selector->Remove( Handle->FD, false, true );
} }
}
return true; return true;
} }
@@ -588,13 +548,11 @@ int CSelectableBare::ReadFromFD( int FD, char * Data, int MaxLen )
bool Error = false; bool Error = false;
// Check if buffer created // Check if buffer created
if ((FD == -1) || (MaxLen < 1)) { if ((FD == -1) || (MaxLen < 1))
return 0; return 0;
}
// Read Data into buffer // Read Data into buffer
while (DataRemain) while (DataRemain) {
{
// Read from file descriptor // Read from file descriptor
BytesRead = read( FD, &Data[TotalRead], DataRemain ); BytesRead = read( FD, &Data[TotalRead], DataRemain );
if ((BytesRead < 0)) { if ((BytesRead < 0)) {
@@ -607,10 +565,9 @@ int CSelectableBare::ReadFromFD( int FD, char * Data, int MaxLen )
TotalRead += BytesRead; TotalRead += BytesRead;
DataRemain -= BytesRead; DataRemain -= BytesRead;
if (DataRemain) { if (DataRemain)
usleep( 500 ); usleep( 500 );
} }
}
return (Error)? -TotalRead : TotalRead; // Report negative total on error return (Error)? -TotalRead : TotalRead; // Report negative total on error
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -623,13 +580,11 @@ int CSelectableBare::WriteToFD( int FD, const char * Data, int Len, bool Force )
bool Error = false; bool Error = false;
// Check if buffer created // Check if buffer created
if ((FD == -1) || !DataRemain) { if ((FD == -1) || !DataRemain)
return 0; return 0;
}
// Read Data into buffer // Read Data into buffer
while (DataRemain) while (DataRemain) {
{
// Read from file descriptor // Read from file descriptor
BytesWritten = write( FD, &Data[TotalWritten], DataRemain ); BytesWritten = write( FD, &Data[TotalWritten], DataRemain );
if ((BytesWritten <= 0) && (!Force || (errno != EAGAIN))) { if ((BytesWritten <= 0) && (!Force || (errno != EAGAIN))) {
@@ -642,10 +597,9 @@ int CSelectableBare::WriteToFD( int FD, const char * Data, int Len, bool Force )
TotalWritten += BytesWritten; TotalWritten += BytesWritten;
DataRemain -= BytesWritten; DataRemain -= BytesWritten;
if (DataRemain) { if (DataRemain)
usleep( 500 ); usleep( 500 );
} }
}
return (Error)? -TotalWritten : TotalWritten; // Report negative total on error return (Error)? -TotalWritten : TotalWritten; // Report negative total on error
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -659,9 +613,8 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co
int BytesWritten = 0; int BytesWritten = 0;
// Validate // Validate
if (!ChannelName || !Data) { if (!ChannelName || !Data)
return 0; return 0;
}
// Get Channel // Get Channel
if (!(Channel = GetChannel( ChannelName ))) { if (!(Channel = GetChannel( ChannelName ))) {
@@ -683,10 +636,8 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co
// Find Linked handle // Find Linked handle
Handle = FirstHandle; Handle = FirstHandle;
while( Handle ) while( Handle ) {
{ if (Handle->Channel && !strcasecmp( ChannelName, Handle->Channel->Name )) {
if (Handle->Channel && !strcasecmp( ChannelName, Handle->Channel->Name ))
{
// Input to Handle // Input to Handle
TempWritten = OutputHandle( Handle, Data, Len ); TempWritten = OutputHandle( Handle, Data, Len );
BytesWritten = (TempWritten > BytesWritten)? TempWritten : BytesWritten; BytesWritten = (TempWritten > BytesWritten)? TempWritten : BytesWritten;
@@ -711,18 +662,15 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len
int BytesWritten = 0; int BytesWritten = 0;
int DataLen = (Len != -1)? Len : (Data)? strlen(Data) : 0; int DataLen = (Len != -1)? Len : (Data)? strlen(Data) : 0;
if ((Handle->State != csOpen)) if ((Handle->State != csOpen)) {
{
// Check if auto-managed handle // Check if auto-managed handle
if (!Handle->AutoManage) if (!Handle->AutoManage) {
{
// Handle is not open or auto-managed // Handle is not open or auto-managed
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Input rejected, Handle not Open (not auto-managed)", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Input rejected, Handle not Open (not auto-managed)",
ProcessName, Name, Handle->Name ); ProcessName, Name, Handle->Name );
return 0; return 0;
} }
else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) {
{
// Complete opening process // Complete opening process
Open( Handle ); Open( Handle );
@@ -743,8 +691,7 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len
return 0; return 0;
} }
} }
else else {
{
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Input rejected, Retry (auto-managed) Handle re-open in %d ms", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Input rejected, Retry (auto-managed) Handle re-open in %d ms",
ProcessName, Name, Handle->Name, TimeLeft( Handle->LastAction, Handle->ReopenDelay) ); ProcessName, Name, Handle->Name, TimeLeft( Handle->LastAction, Handle->ReopenDelay) );
return 0; return 0;
@@ -752,23 +699,19 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len
} }
// Check packet length // Check packet length
if (Len == -1) { if (Len == -1)
Len = strlen( Data ); Len = strlen( Data );
}
// Decide where to put data // Decide where to put data
if (Handle->OutBuffer) if (Handle->OutBuffer) {
{
// Write to buffer // Write to buffer
BytesWritten = Handle->OutBuffer->Push( true, Data, Len ); BytesWritten = Handle->OutBuffer->Push( true, Data, Len );
// Add to select write list // Add to select write list
if (BytesWritten && Selector) { if (BytesWritten && Selector)
Selector->Add( Handle->FD, false, true, Handle, this ); Selector->Add( Handle->FD, false, true, Handle, this );
} }
} else {
else
{
// Write directly to handle / socket // Write directly to handle / socket
errno = 0; errno = 0;
BytesWritten = WriteToFD( Handle->FD, Data, Len, true ); BytesWritten = WriteToFD( Handle->FD, Data, Len, true );
@@ -786,11 +729,10 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len
ProcessName, Name, Handle->Name, BytesWritten, DataLen ); ProcessName, Name, Handle->Name, BytesWritten, DataLen );
} }
if (BytesWritten != 0) {
// Reset timeout // Reset timeout
if (BytesWritten != 0)
SetStartTime( &(Handle->LastAction) ); SetStartTime( &(Handle->LastAction) );
} }
}
return BytesWritten; return BytesWritten;
} }
@@ -802,8 +744,7 @@ bool CSelectableBare::Process()
// Check all handles // Check all handles
Handle = FirstHandle; Handle = FirstHandle;
while (Handle) while (Handle) {
{
// Auto manage handles // Auto manage handles
if (Handle->State == csPrepared) { if (Handle->State == csPrepared) {
// Proceed to open // Proceed to open
@@ -811,10 +752,9 @@ bool CSelectableBare::Process()
} }
else if ((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) { else if ((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) {
// Try to re-open port after delay // Try to re-open port after delay
if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { if (Timeout( Handle->LastAction, Handle->ReopenDelay ))
Open( Handle ); Open( Handle );
} }
}
else if (Handle->Channel && (Handle->Channel->InState == CH_off) && Handle->AutoManage) { else if (Handle->Channel && (Handle->Channel->InState == CH_off) && Handle->AutoManage) {
// Set channel to standby // Set channel to standby
SetChannelInState( Handle->Channel, CH_standby ); SetChannelInState( Handle->Channel, CH_standby );
@@ -835,10 +775,9 @@ bool CSelectableBare::Process()
// Check for auto close (but not on servers) // Check for auto close (but not on servers)
if ((Handle->State == csOpen) && Handle->AutoManage && !Handle->Persistent) { if ((Handle->State == csOpen) && Handle->AutoManage && !Handle->Persistent) {
// Close port after timeout // Close port after timeout
if (Timeout( Handle->LastAction, Handle->CloseTimeout )) { if (Timeout( Handle->LastAction, Handle->CloseTimeout ))
Close( Handle, true ); Close( Handle, true );
} }
}
Handle = Handle->Next; Handle = Handle->Next;
} }
return true; return true;

File diff suppressed because it is too large Load Diff