From e5f8a8347ad6418888e1cfeb4ea82e70d3023aca Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Tue, 12 Oct 2021 10:14:35 -0500 Subject: [PATCH] CharBufferCore: - Bug fix - FindStr() for input marker not matching correctly SeletableBare/Core: - Reformat code - eliminate & compact {} where needed --- CharBufferCore.cpp | 17 +- SelectableBare.cpp | 197 +++++---------- SelectableCore.cpp | 607 +++++++++++++++++---------------------------- 3 files changed, 304 insertions(+), 517 deletions(-) diff --git a/CharBufferCore.cpp b/CharBufferCore.cpp index 8d0f15c..ee06dc6 100644 --- a/CharBufferCore.cpp +++ b/CharBufferCore.cpp @@ -266,10 +266,10 @@ int CRollingBuffer::PeekCopy( char ** Data, int PeekPos, int MaxLen ) // Look for first occurrence of character in buffer bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundPos, int StartPos ) { - char * CheckPos = NULL; - char * CheckLimit = NULL; - char * MatchPos = NULL; - char * MatchLimit = NULL; + const char * CheckPos = NULL; + const char * CheckLimit = NULL; + const char * MatchPos = NULL; + const char * MatchLimit = NULL; // Check if buffer exists if (!BufSize || !SearchStr || !SearchLen) { @@ -288,8 +288,8 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP CheckLimit = &Buffer[BufSize-1]; // Set Match start point and limit - MatchPos = (char*)SearchStr; - MatchLimit = (char*)&SearchStr[SearchLen-1]; + MatchPos = SearchStr; + MatchLimit = &SearchStr[SearchLen-1]; // Search for char FoundPos = StartPos; @@ -307,9 +307,10 @@ bool CRollingBuffer::FindStr( const char * SearchStr, int SearchLen, int &FoundP MatchPos++; } } - else { + else if (MatchPos != SearchStr){ // Reset match point - MatchPos = (char*)SearchStr; + FoundPos -= MatchPos - SearchStr; + MatchPos = SearchStr; } // Next char diff --git a/SelectableBare.cpp b/SelectableBare.cpp index 931a70b..0d34eee 100644 --- a/SelectableBare.cpp +++ b/SelectableBare.cpp @@ -46,12 +46,10 @@ CSelectableBare::~CSelectableBare() THandle * NextHandle = NULL; // Destroy File Handles - while (FirstHandle) - { + while (FirstHandle) { // Close handle if open - if ((FirstHandle->State == csOpen) || (FirstHandle->State == csWaitingtoOpen)) { + if ((FirstHandle->State == csOpen) || (FirstHandle->State == csWaitingtoOpen)) Close( FirstHandle, false ); - } NextHandle = FirstHandle->Next; DestroyHandle( FirstHandle ); @@ -82,8 +80,7 @@ THandle * CSelectableBare::CreateHandle( const char * HandleName, bool CreateCh Handle = &((*Handle)->Next); // Create if necessary - if (!*Handle) - { + if (!*Handle) { // Create File handle at end of list *Handle = new THandle; @@ -103,9 +100,8 @@ THandle * CSelectableBare::CreateHandle( const char * HandleName, bool CreateCh } // Create Matching Channel - if (CreateChannel) { + if (CreateChannel) (*Handle)->Channel = AddChannel( HandleName, CH_off ); - } return *Handle; } @@ -116,19 +112,17 @@ bool CSelectableBare::RemoveHandle( THandle * Handle ) THandle ** HandlePtr = NULL; // Validate - if (!Handle || (Handle->State == csOpen) || (Handle->State == csWaitingtoOpen)) { + if (!Handle || (Handle->State == csOpen) || (Handle->State == csWaitingtoOpen)) return false; - } // Find in List HandlePtr = &FirstHandle; - while (*HandlePtr && (*HandlePtr != Handle)) { + while (*HandlePtr && (*HandlePtr != Handle)) HandlePtr = &((*HandlePtr)->Next); - } + // Remove from list if found - if (*HandlePtr) { + if (*HandlePtr) *HandlePtr = (*HandlePtr)->Next; - } // Log event 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 ) { // Validate - if (!Handle) { + if (!Handle) return false; - } // Set callback 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 ) { // Validate - if (!Handle) { + if (!Handle) return false; - } // Set params 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 ) { // Validate - if (!Handle) { + if (!Handle) return false; - } // Input Buffer if (Handle->InBuffer) { delete Handle->InBuffer; Handle->InBuffer = NULL; } - if (InBufSize) { + if (InBufSize) Handle->InBuffer = (CRollingBuffer*) new CRollingBuffer( InBufSize ); - } // Set Input Timeout Handle->InTimeout = InTimeout; @@ -253,18 +243,16 @@ bool CSelectableBare::SetInBuffer( THandle * Handle, int InBufSize, int InTimeou bool CSelectableBare::SetOutBuffer( THandle * Handle, int OutBufSize ) { // Validate - if (!Handle) { + if (!Handle) return false; - } // Output Buffer if (Handle->OutBuffer) { delete Handle->OutBuffer; Handle->OutBuffer = NULL; } - if (OutBufSize) { + if (OutBufSize) Handle->OutBuffer = (CRollingBuffer*) new CRollingBuffer( OutBufSize ); - } return true; } @@ -277,44 +265,38 @@ bool CSelectableBare::ProcessInputBuffer( THandle * Handle, bool Force ) char * Data = NULL; // Check if buffered data - if (!Handle || !Handle->InBuffer || !Handle->InBuffer->Len()) { + if (!Handle || !Handle->InBuffer || !Handle->InBuffer->Len()) return false; - } // Check if forced processed - if (Force || (!Handle->InTimeout && !Handle->InMarkerLen)) - { + if (Force || (!Handle->InTimeout && !Handle->InMarkerLen)) { // Show Packet Len = Handle->InBuffer->Peek( &Data ); if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-T:", ProcessName, Name, Handle->Name ); // 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 ); - } else { + else Output( Handle->Channel, NULL, true, Data, Len ); - } // Clear processed bytes from buffer Handle->InBuffer->Clear( Len ); } - else - { + else { // 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 Len = Handle->InBuffer->Peek( &Data, 0, Pos+Handle->InMarkerLen ); if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - IN-M:", ProcessName, Name, Handle->Name ); // 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 ); - } else { + else Output( Handle->Channel, NULL, true, Data, Len ); - } // Clear processed bytes from buffer Handle->InBuffer->Clear( Len ); @@ -330,15 +312,13 @@ int CSelectableBare::Open( THandle * Handle ) // Validate - if (!Handle || (Handle->Type == ctNone)) { + if (!Handle || (Handle->Type == ctNone)) return -1; - } else if (Handle->State == csOpen) { + else if (Handle->State == csOpen) return Handle->FD; - } // Check if port exits - if (access( Handle->Path, F_OK ) != 0) - { + if (access( Handle->Path, F_OK ) != 0) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Path not found [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -347,8 +327,7 @@ int CSelectableBare::Open( THandle * Handle ) // Open Port Handle->FD = open( Handle->Path, O_RDWR ); - if (Handle->FD == -1) - { + if (Handle->FD == -1) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Path [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -360,9 +339,8 @@ int CSelectableBare::Open( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set timer (for re-open or auto-close) SetStartTime( &Handle->LastAction ); @@ -387,9 +365,8 @@ bool CSelectableBare::Close( THandle * Handle, bool QuickReopen ) // Remove from Select List if (!Fail && Selector) { - if (Handle->Type != ctUDPremote) { + if (Handle->Type != ctUDPremote) Selector->Remove( Handle->FD, true, true ); - } } // Reset FD @@ -418,17 +395,15 @@ bool CSelectableBare::Read( THandle * Handle ) int BytesWaiting = -1; // 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; - } // Log Read Event if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Read Event", ProcessName, Name, Handle->Name ); // Check if socket ready (non-block open in progress) - if (Handle->State == csWaitingtoOpen) - { + if (Handle->State == csWaitingtoOpen) { Open( Handle ); // Reset Timer (for auto-close) @@ -440,8 +415,7 @@ bool CSelectableBare::Read( THandle * Handle ) ioctl( Handle->FD, FIONREAD, &BytesWaiting ); // Error on port - if (BytesWaiting < 0) - { + if (BytesWaiting < 0) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)", ProcessName, Name, Handle->Name, strerror(errno) ); @@ -451,13 +425,11 @@ bool CSelectableBare::Read( THandle * Handle ) } // Validate - if (Handle->State != csOpen) { + if (Handle->State != csOpen) return false; - } // Read File directly into buffer - if (Handle->InBuffer) - { + if (Handle->InBuffer) { errno = 0; BytesRead = Handle->InBuffer->ReadFromFD( Handle->FD, BytesWaiting ); @@ -471,10 +443,9 @@ bool CSelectableBare::Read( THandle * Handle ) ProcessName, Name, Handle->Name, BytesRead, BytesWaiting ); } - if (BytesRead != 0) { - // Process Buffer + // Process Buffer + if (BytesRead != 0) ProcessInputBuffer( Handle, false ); - } } // Reset timer @@ -495,16 +466,13 @@ bool CSelectableBare::Write( THandle * Handle ) return false; // 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? - if (!Handle->AutoManage) - { + if (!Handle->AutoManage) { // Must be opened manually return false; } - else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) - { + else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { // Attempt to re-open port Open( Handle ); } @@ -514,8 +482,7 @@ bool CSelectableBare::Write( THandle * Handle ) if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Write Event", ProcessName, Name, Handle->Name ); - if (Handle->State == csWaitingtoOpen) - { + if (Handle->State == csWaitingtoOpen) { // Complete socket open process Open( Handle ); @@ -523,15 +490,12 @@ bool CSelectableBare::Write( THandle * Handle ) SetStartTime( &(Handle->LastAction) ); // Remove from set for select write - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } return true; } - else if (Handle->State == csOpen) - { - if (Handle->OutBuffer) - { + else if (Handle->State == csOpen) { + if (Handle->OutBuffer) { // Write directly to handle / socket errno = 0; BytesWritten = Handle->OutBuffer->WriteToFD( Handle->FD ); @@ -549,29 +513,25 @@ bool CSelectableBare::Write( THandle * Handle ) ProcessName, Name, Handle->Name, BytesWritten, Len ); } - if (BytesWritten != 0) - { + if (BytesWritten != 0) { // Update Buffer Handle->OutBuffer->Clear( (BytesWritten > 0)? BytesWritten : -BytesWritten ); // negative value reported if error occurred // Check if Buffer empty if (!Handle->OutBuffer->Len()) { // Remove from Select Write list - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } } // Reset timeout SetStartTime( &(Handle->LastAction) ); } } - else - { + else { // No Output buffer to write from, so remove from Write list - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } } return true; } @@ -588,13 +548,11 @@ int CSelectableBare::ReadFromFD( int FD, char * Data, int MaxLen ) bool Error = false; // Check if buffer created - if ((FD == -1) || (MaxLen < 1)) { + if ((FD == -1) || (MaxLen < 1)) return 0; - } // Read Data into buffer - while (DataRemain) - { + while (DataRemain) { // Read from file descriptor BytesRead = read( FD, &Data[TotalRead], DataRemain ); if ((BytesRead < 0)) { @@ -607,9 +565,8 @@ int CSelectableBare::ReadFromFD( int FD, char * Data, int MaxLen ) TotalRead += BytesRead; DataRemain -= BytesRead; - if (DataRemain) { + if (DataRemain) usleep( 500 ); - } } 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; // Check if buffer created - if ((FD == -1) || !DataRemain) { + if ((FD == -1) || !DataRemain) return 0; - } // Read Data into buffer - while (DataRemain) - { + while (DataRemain) { // Read from file descriptor BytesWritten = write( FD, &Data[TotalWritten], DataRemain ); if ((BytesWritten <= 0) && (!Force || (errno != EAGAIN))) { @@ -642,9 +597,8 @@ int CSelectableBare::WriteToFD( int FD, const char * Data, int Len, bool Force ) TotalWritten += BytesWritten; DataRemain -= BytesWritten; - if (DataRemain) { + if (DataRemain) usleep( 500 ); - } } 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; // Validate - if (!ChannelName || !Data) { + if (!ChannelName || !Data) return 0; - } // Get Channel if (!(Channel = GetChannel( ChannelName ))) { @@ -683,10 +636,8 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co // Find Linked handle Handle = FirstHandle; - while( Handle ) - { - if (Handle->Channel && !strcasecmp( ChannelName, Handle->Channel->Name )) - { + while( Handle ) { + if (Handle->Channel && !strcasecmp( ChannelName, Handle->Channel->Name )) { // Input to Handle TempWritten = OutputHandle( Handle, Data, Len ); BytesWritten = (TempWritten > BytesWritten)? TempWritten : BytesWritten; @@ -711,18 +662,15 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len int BytesWritten = 0; int DataLen = (Len != -1)? Len : (Data)? strlen(Data) : 0; - if ((Handle->State != csOpen)) - { + if ((Handle->State != csOpen)) { // Check if auto-managed handle - if (!Handle->AutoManage) - { + if (!Handle->AutoManage) { // 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)", ProcessName, Name, Handle->Name ); return 0; } - else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) - { + else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { // Complete opening process Open( Handle ); @@ -743,8 +691,7 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len 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", ProcessName, Name, Handle->Name, TimeLeft( Handle->LastAction, Handle->ReopenDelay) ); return 0; @@ -752,23 +699,19 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len } // Check packet length - if (Len == -1) { + if (Len == -1) Len = strlen( Data ); - } // Decide where to put data - if (Handle->OutBuffer) - { + if (Handle->OutBuffer) { // Write to buffer BytesWritten = Handle->OutBuffer->Push( true, Data, Len ); // Add to select write list - if (BytesWritten && Selector) { + if (BytesWritten && Selector) Selector->Add( Handle->FD, false, true, Handle, this ); - } } - else - { + else { // Write directly to handle / socket errno = 0; BytesWritten = WriteToFD( Handle->FD, Data, Len, true ); @@ -786,10 +729,9 @@ int CSelectableBare::OutputHandle( THandle * Handle, const char * Data, int Len ProcessName, Name, Handle->Name, BytesWritten, DataLen ); } - if (BytesWritten != 0) { - // Reset timeout + // Reset timeout + if (BytesWritten != 0) SetStartTime( &(Handle->LastAction) ); - } } return BytesWritten; @@ -802,8 +744,7 @@ bool CSelectableBare::Process() // Check all handles Handle = FirstHandle; - while (Handle) - { + while (Handle) { // Auto manage handles if (Handle->State == csPrepared) { // Proceed to open @@ -811,9 +752,8 @@ bool CSelectableBare::Process() } else if ((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) { // Try to re-open port after delay - if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { + if (Timeout( Handle->LastAction, Handle->ReopenDelay )) Open( Handle ); - } } else if (Handle->Channel && (Handle->Channel->InState == CH_off) && Handle->AutoManage) { // Set channel to standby @@ -835,9 +775,8 @@ bool CSelectableBare::Process() // Check for auto close (but not on servers) if ((Handle->State == csOpen) && Handle->AutoManage && !Handle->Persistent) { // Close port after timeout - if (Timeout( Handle->LastAction, Handle->CloseTimeout )) { + if (Timeout( Handle->LastAction, Handle->CloseTimeout )) Close( Handle, true ); - } } Handle = Handle->Next; } diff --git a/SelectableCore.cpp b/SelectableCore.cpp index 4336761..8dd0858 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -77,8 +77,7 @@ CSelectableCore::~CSelectableCore() bool Result; // Destroy File Handles - while (FirstHandle) - { + while (FirstHandle) { // Close active resolve request if (FirstHandle->ResolveReq) { if ((Result = gai_cancel( FirstHandle->ResolveReq->Request )) != 0) { @@ -123,26 +122,24 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig ) // Load Handles HandleConfig = FunctionConfig->GetChFirstChild( "Handles", true ); - while (HandleConfig) - { + while (HandleConfig) { // Create Handle and channel link Handle = CreateHandle( HandleConfig->GetName(), false ); Handle->Channel = GetChannel( HandleConfig->GetChStr( "Channel" ) ); // Load handle specifics Type = (char*)HandleConfig->GetChStr( "Type", "TCPclient", true ); - if (!strcasecmp( Type, "Serial" )) - { - if ((PortName = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( PortName ))) { + if (!strcasecmp( Type, "Serial" )) { + // Get port address + if ((PortName = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( PortName ))) Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value - } else { + else Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value - } SetSerialHandle( Handle, Address ); // Update configuration if specified - if ((SerialConfig = HandleConfig->GetChild( "Port/SerialConfig", false ))) - { + if ((SerialConfig = HandleConfig->GetChild( "Port/SerialConfig", false ))) { + ParityText = (char*)SerialConfig->GetChStr( "Parity", "none", true ); if (!strcasecmp( ParityText, "none" )) Parity = NO_PARITY; @@ -175,67 +172,70 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig ) Persitent = true; } } - else if (!strcasecmp( Type, "LinePrinter" )) - { - if ((PortName = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { + else if (!strcasecmp( Type, "LinePrinter" )) { + // Get port address + if ((PortName = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value - } else { + else Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value - } + SetLinePrinterHandle( Handle, Address ); Persitent = true; } - else if (!strcasecmp( Type, "UNIXserver" )) - { - if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { + else if (!strcasecmp( Type, "UNIXserver" )) { + // Get UNIX socket handle + if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value - } else { + else Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value - } + Queue = HandleConfig->GetChInt( "Socket/Queue", 2, true ); SetUnixHandle( Handle, ctUNIXserver, Address, Queue ); Persitent = true; } - else if (!strcasecmp( Type, "UNIXclient" )) - { - if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { + else if (!strcasecmp( Type, "UNIXclient" )) { + // Get UNIX socket handle + if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value - } else { + else Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value - } + SetUnixHandle( Handle, ctUNIXclient, Address, 0 ); Persitent = false; } - else if (!strcasecmp( Type, "UDPserver" )) - { + else if (!strcasecmp( Type, "UDPserver" )) { + // Get UDP address & port if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value - } else { + } + else { Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value } SetSocketHandle( Handle, ctUDPserver, Address, strlcase(Port), 0 ); Persitent = true; } - else if (!strcasecmp( Type, "UDPclient" )) - { + else if (!strcasecmp( Type, "UDPclient" )) { + // Get UDP address & port if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value - } else { + } + else { Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value } SetSocketHandle( Handle, ctUDPclient, Address, strlcase(Port), 0 ); Persitent = false; } - else if (!strcasecmp( Type, "TCPserver" )) - { + else if (!strcasecmp( Type, "TCPserver" )) { + // Get TCP address & port if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value - } else { + } + else { Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value } @@ -243,12 +243,13 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig ) SetSocketHandle( Handle, ctTCPserver, Address, strlcase(Port), Queue ); Persitent = true; } - else if (!strcasecmp( Type, "TCPclient" )) - { + else if (!strcasecmp( Type, "TCPclient" )) { + // Get TCP address & port if ((PortName = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, PortName ))) { Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value - } else { + } + else { Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value } @@ -256,6 +257,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig ) Persitent = false; } else if (!strcasecmp( Type, "ForkPipe" )) { + // Get fork pipe handle Address = (char*)HandleConfig->GetChStr( "Fork/ExecPath", NULL, true ); // Get default value SetForkPipeHandle( Handle, Address ); Persitent = true; @@ -330,9 +332,8 @@ bool CSelectableCore::DestroyHandle( THandle * Handle ) bool CSelectableCore::ClearHandle( THandle * Handle ) { // Validate - if (!Handle) { + if (!Handle) return false; - } // Reset Type related parameters if (Handle->Path) { @@ -366,9 +367,8 @@ bool CSelectableCore::ClearHandle( THandle * Handle ) bool CSelectableCore::SetSerialHandle( THandle * Handle, const char * FileName ) { // Validate - if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctSerial)) || !FileName) { + if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctSerial)) || !FileName) return false; - } // Set Type Handle->Type = ctSerial; @@ -387,9 +387,8 @@ bool CSelectableCore::SetSerialHandle( THandle * Handle, const char * FileName ) bool CSelectableCore::SetSerialHandleConfig( THandle * Handle, int Baudrate, short DataBits, short Parity, short StopBits, short FlowCtrl, int DataWait ) { // Validate - if (!Handle || (Handle->Type != ctSerial)) { + if (!Handle || (Handle->Type != ctSerial)) return false; - } Handle->InBaudrate = Baudrate; Handle->OutBaudrate = Baudrate; @@ -406,9 +405,8 @@ bool CSelectableCore::SetSerialHandleConfig( THandle * Handle, int Baudrate, sho bool CSelectableCore::SetLinePrinterHandle( THandle * Handle, const char * FileName ) { // Validate - if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctLinePrinter)) || !FileName) { + if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctLinePrinter)) || !FileName) return false; - } // Set Type Handle->Type = ctLinePrinter; @@ -427,9 +425,8 @@ bool CSelectableCore::SetLinePrinterHandle( THandle * Handle, const char * FileN bool CSelectableCore::SetForkPipeHandle( THandle * Handle, const char * ExecPath ) { // Validate - if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctForkPipe)) || !ExecPath) { + if (!Handle || ((Handle->Type != ctNone) && (Handle->Type != ctForkPipe)) || !ExecPath) return false; - } // Set Type Handle->Type = ctForkPipe; @@ -450,17 +447,15 @@ bool CSelectableCore::SetUnixHandle( THandle * Handle, EConnectType Type, const // Validate if (!Handle || !FileName || !((Type == ctUNIXserver) || (Type == ctUNIXclient) || (Type == ctUNIXremote)) || - !((Handle->Type == ctNone) || (Handle->Type == Type)) ) { + !((Handle->Type == ctNone) || (Handle->Type == Type)) ) return false; - } // Set Type Handle->Type = Type; // Clear File Name - if (Handle->Path) { + if (Handle->Path) free( Handle->Path ); - } // Set name Handle->Path = strdup( FileName ); @@ -478,12 +473,11 @@ bool CSelectableCore::SetSocketHandle( THandle * Handle, EConnectType Type, con // Validate if (!Handle || !HostName || !PortName || !((Type == ctUDPserver) || (Type == ctUDPclient) || (Type == ctUDPremote) || (Type == ctTCPserver) || (Type == ctTCPclient) || (Type == ctTCPremote)) || - !((Handle->Type == ctNone) || (Handle->Type == Type)) ) { + !((Handle->Type == ctNone) || (Handle->Type == Type)) ) return false; - } // Set Type - Handle->Type = Type; + Handle->Type = Type; // Clear HostName & Port Name if (Handle->HostName) @@ -512,15 +506,13 @@ bool CSelectableCore::SetSocketHandle( THandle * Handle, EConnectType Type, con THandle * CSelectableCore::OpenSerialPort( THandle * Handle ) { // Validate - if (!Handle || (Handle->Type == ctNone)) { + if (!Handle || (Handle->Type == ctNone)) return NULL; - } else if (Handle->State == csOpen) { + else if (Handle->State == csOpen) return Handle; - } // Check if port exits - if (access( Handle->Path, F_OK ) != 0) - { + if (access( Handle->Path, F_OK ) != 0) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Serial Port not found [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -529,8 +521,7 @@ THandle * CSelectableCore::OpenSerialPort( THandle * Handle ) // Open Port Handle->FD = open( Handle->Path, O_RDWR ); - if (Handle->FD == -1) - { + if (Handle->FD == -1) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Serial Port [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -557,9 +548,8 @@ THandle * CSelectableCore::OpenSerialPort( THandle * Handle ) Handle->DataBits, Handle->Parity, Handle->StopBits, Handle->FlowCtrl ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -570,15 +560,13 @@ THandle * CSelectableCore::OpenSerialPort( THandle * Handle ) THandle * CSelectableCore::OpenLinePrinterPort( THandle * Handle ) { // Validate - if (!Handle || (Handle->Type == ctNone)) { + if (!Handle || (Handle->Type == ctNone)) return NULL; - } else if (Handle->State == csOpen) { + else if (Handle->State == csOpen) return Handle; - } // Check if port exits - if (access( Handle->Path, F_OK ) != 0) - { + if (access( Handle->Path, F_OK ) != 0) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Printer Port not found [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -587,8 +575,7 @@ THandle * CSelectableCore::OpenLinePrinterPort( THandle * Handle ) // Open Port Handle->FD = open( Handle->Path, O_RDWR ); - if (Handle->FD == -1) - { + if (Handle->FD == -1) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Printer Port [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -600,9 +587,8 @@ THandle * CSelectableCore::OpenLinePrinterPort( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -618,15 +604,13 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) int Count = 0; // Validate - if (!Handle || (Handle->Type == ctNone)) { + if (!Handle || (Handle->Type == ctNone)) return NULL; - } else if (Handle->State == csOpen) { + else if (Handle->State == csOpen) return Handle; - } // Validate Exec path - if (!Handle->Path || !*(Handle->Path)) - { + if (!Handle->Path || !*(Handle->Path)) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - No path specified for Exec", ProcessName, Name, Handle->Name ); @@ -634,8 +618,7 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) } // Create Pipe - if ((pipe( pipefd ) == -1) || (pipefd[0] == -1) || (pipefd[1] == -1)) - { + if ((pipe( pipefd ) == -1) || (pipefd[0] == -1) || (pipefd[1] == -1)) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not open Pipe (%s)", ProcessName, Name, Handle->Name, strerror(errno) ); @@ -644,8 +627,7 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) // Fork process Handle->ChildPID = fork(); - if (Handle->ChildPID < 0) - { + if (Handle->ChildPID < 0) { // Fork has failed // Close both ends of the pipe close( pipefd[0] ); @@ -657,8 +639,7 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) ProcessName, Name, Handle->Name, strerror(errno) ); return NULL; } - else if (Handle->ChildPID > 0) - { + else if (Handle->ChildPID > 0) { // Fork success - this is parent // Close Read-end of pipe, but keep the Write-end close( pipefd[0] ); @@ -668,8 +649,7 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Process forked successfully", ProcessName, Name, Handle->Name ); } - else - { + else { // Fork success - this is child // Close Write-end of pipe close( pipefd[1] ); @@ -702,9 +682,8 @@ THandle * CSelectableCore::OpenForkPipe( THandle * Handle ) } // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, false, true, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -718,11 +697,10 @@ THandle * CSelectableCore::OpenUNIXserverSocket( THandle * Handle ) struct sockaddr_un address; // Validate - if (Handle->Type != ctUNIXserver) { + if (Handle->Type != ctUNIXserver) return NULL; - } else if (Handle->State == csOpen) { + else if (Handle->State == csOpen) return Handle; - } // Remove old socket if (unlink( Handle->Path ) && (errno != ENOENT)) { @@ -736,8 +714,7 @@ THandle * CSelectableCore::OpenUNIXserverSocket( THandle * Handle ) } // Create socket - if ((Handle->FD = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0) - { + if ((Handle->FD = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create new UNIX Server socket [%s] (%s)", ProcessName, Name, Handle->Name, Handle->Path, strerror(errno) ); @@ -758,8 +735,7 @@ THandle * CSelectableCore::OpenUNIXserverSocket( THandle * Handle ) addr_len = sizeof(address); // Bind socket - if (bind( Handle->FD, (struct sockaddr*)&address, addr_len ) < 0) - { + if (bind( Handle->FD, (struct sockaddr*)&address, addr_len ) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to bind UNIX Server socket [%s] (%s)", ProcessName, Name, Handle->Name, Handle->Path, strerror(errno) ); @@ -773,8 +749,7 @@ THandle * CSelectableCore::OpenUNIXserverSocket( THandle * Handle ) }; // Create que for 5 connections - if (listen( Handle->FD, Handle->Queue ) < 0) - { + if (listen( Handle->FD, Handle->Queue ) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to listen on UNIX Server socket [%s] (%s)", ProcessName, Name, Handle->Name, Handle->Path, strerror(errno) ); @@ -792,9 +767,8 @@ THandle * CSelectableCore::OpenUNIXserverSocket( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -808,16 +782,12 @@ THandle * CSelectableCore::OpenUNIXclientSocket( THandle * Handle ) struct sockaddr_un address; // Check state - if (Handle->State == csOpen) { - // Already open - return Handle; - } + if (Handle->State == csOpen) + return Handle; // Already open - if (Handle->State != csWaitingtoOpen) - { + if (Handle->State != csWaitingtoOpen) { // Create socket - if ((Handle->FD = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0) - { + if ((Handle->FD = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create new UNIX Client socket [%s] (%s)", ProcessName, Name, Handle->Name, Handle->Path, strerror(errno) ); @@ -838,31 +808,27 @@ THandle * CSelectableCore::OpenUNIXclientSocket( THandle * Handle ) addr_len = sizeof(address); // Try to connect to address - if (!connect( Handle->FD, (struct sockaddr*)&address, addr_len )) - { + if (!connect( Handle->FD, (struct sockaddr*)&address, addr_len )) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - UNIX Client waiting to connect [%s]", ProcessName, Name, Handle->Name, Handle->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, true, Handle, this ); - } // Set status HandleState( Handle, csWaitingtoOpen ); return Handle; } - else - { + else { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - UNIX Client could not connect [%s] (%s)", ProcessName, Name, Handle->Name, Handle->Path, strerror(errno) ); // Remove from Select List - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, true, true ); - } // Close socket close( Handle->FD ); @@ -873,15 +839,13 @@ THandle * CSelectableCore::OpenUNIXclientSocket( THandle * Handle ) return NULL; } } - else - { + else { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - UNIX Client connected [%s]", ProcessName, Name, Handle->Name, Handle->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, true, Handle, this ); - } // Set status HandleState( Handle, csOpen ); @@ -900,17 +864,14 @@ THandle * CSelectableCore::OpenUNIXremoteSocket( THandle * Handle ) struct sockaddr_un address; // Validate - if (!Handle) { + if (!Handle) return NULL; - } // Check Handle type - if (Handle->Type == ctUNIXserver) - { + if (Handle->Type == ctUNIXserver) { // Accept connection on current socket addr_len = sizeof( address ); - if ((ClientFD = accept( Handle->FD, (struct sockaddr *)&address, &addr_len)) == -1) - { + if ((ClientFD = accept( Handle->FD, (struct sockaddr *)&address, &addr_len)) == -1) { // Log Event if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - UNIX Server failed to accept blocking connection (%s)", @@ -929,9 +890,8 @@ THandle * CSelectableCore::OpenUNIXremoteSocket( THandle * Handle ) // Get end of client list RemoteClient = &FirstHandle; - while (*RemoteClient) { + while (*RemoteClient) RemoteClient = &((*RemoteClient)->Next); - } // Create Remote Client Handle sprintf( ClientName, "%s-%d", Handle->Name, ClientFD ); @@ -960,22 +920,18 @@ THandle * CSelectableCore::OpenUNIXremoteSocket( THandle * Handle ) ProcessName, Name, Handle->Name, (*RemoteClient)->Path ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( (*RemoteClient)->FD, true, true, *RemoteClient, this ); - } return *RemoteClient; } - else if (Handle->Type == ctUNIXremote) - { + else if (Handle->Type == ctUNIXremote) { // Check state - if (Handle->State == csOpen) - { + if (Handle->State == csOpen) { // Already open return Handle; } - else if (Handle->State == csWaitingtoOpen) - { + else if (Handle->State == csWaitingtoOpen) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Remote UNIX Client connected [%s]", ProcessName, Name, Handle->Name, Handle->Path ); @@ -1036,7 +992,8 @@ bool CSelectableCore::ResolveAddress( THandle * Handle ) if ((Handle->Type == ctTCPserver) || (Handle->Type == ctTCPclient)) { Hints->ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 Hints->ai_socktype = SOCK_STREAM; - } else { + } + else { Hints->ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 Hints->ai_socktype = SOCK_DGRAM; } @@ -1153,17 +1110,15 @@ bool CSelectableCore::HandleResolve( THandle * Handle ) THandle * CSelectableCore::OpenUDPserverSocket( THandle * Handle ) { // Validate Handle - if (Handle->Type != ctUDPserver) { + if (Handle->Type != ctUDPserver) return NULL; - } // Resolve Host & Port Names if (!ResolveAddress( Handle )) return NULL; // Create socket - if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) - { + if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create UDP socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1178,8 +1133,7 @@ THandle * CSelectableCore::OpenUDPserverSocket( THandle * Handle ) fcntl( Handle->FD, F_SETFL, flags | O_NONBLOCK ); // Bind socket - if (bind( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen ) < 0) - { + if (bind( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen ) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to bind UDP socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1199,9 +1153,8 @@ THandle * CSelectableCore::OpenUDPserverSocket( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -1216,9 +1169,8 @@ THandle * CSelectableCore::OpenUDPremoteSocket( THandle * Handle, char * ClientA char ClientName[100]; // Validate - if (!Handle || (Handle->Type != ctUDPserver)) { + if (!Handle || (Handle->Type != ctUDPserver)) return NULL; - } // Check if Remote client already exists ClientCount = 1; @@ -1227,9 +1179,8 @@ THandle * CSelectableCore::OpenUDPremoteSocket( THandle * Handle, char * ClientA RemoteClient = &((*RemoteClient)->Next); ClientCount++; } - if (*RemoteClient) { + if (*RemoteClient) return *RemoteClient; - } // Create Remote Client Handle sprintf( ClientName, "%s-%d", Handle->Name, ClientCount ); @@ -1258,9 +1209,8 @@ THandle * CSelectableCore::OpenUDPremoteSocket( THandle * Handle, char * ClientA ProcessName, Name, Handle->Name, ClientAddress, ClientPort ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( (*RemoteClient)->FD, true, true, *RemoteClient, this ); - } return *RemoteClient; } @@ -1269,18 +1219,15 @@ THandle * CSelectableCore::OpenUDPremoteSocket( THandle * Handle, char * ClientA THandle * CSelectableCore::OpenUDPclientSocket( THandle * Handle ) { // Check state - if (Handle->State == csOpen) { - // Already open - return Handle; - } + if (Handle->State == csOpen) + return Handle; // Already open // Resolve IP Address if (!ResolveAddress( Handle )) return NULL; // Create File descriptor - if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) - { + if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create UDP Client socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1298,9 +1245,8 @@ THandle * CSelectableCore::OpenUDPclientSocket( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, true, Handle, this ); - } // Set status HandleState( Handle, csOpen ); @@ -1322,17 +1268,15 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) int TCPcnt_opt = 3; // Validate Handle - if (Handle->Type != ctTCPserver) { + if (Handle->Type != ctTCPserver) return NULL; - } // Resolve Host & Port Names if (!ResolveAddress( Handle )) return NULL; // Create socket - if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) - { + if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create TCP Server socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1344,8 +1288,7 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) // Configure connection if ((setsockopt( Handle->FD, SOL_SOCKET, SO_LINGER, &ServerLinger_opt, sizeof(ServerLinger_opt)) == -1) || - (setsockopt( Handle->FD, SOL_SOCKET, SO_REUSEADDR, &Reuse_opt, sizeof(Reuse_opt)) == -1)) - { + (setsockopt( Handle->FD, SOL_SOCKET, SO_REUSEADDR, &Reuse_opt, sizeof(Reuse_opt)) == -1)) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not set socket options [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1359,8 +1302,7 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) if ((setsockopt( Handle->FD, SOL_SOCKET, SO_KEEPALIVE, &KeepAlive_opt, sizeof(KeepAlive_opt)) == -1) || (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPIDLE, &TCPidle_opt, sizeof(TCPidle_opt)) == -1) || (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPCNT, &TCPcnt_opt, sizeof(TCPcnt_opt)) == -1) || - (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPINTVL, &TCPint_opt, sizeof(TCPint_opt)) == -1) ) - { + (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPINTVL, &TCPint_opt, sizeof(TCPint_opt)) == -1)) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not set KeepAlive options [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1375,8 +1317,7 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) fcntl( Handle->FD, F_SETFL, flags | O_NONBLOCK ); // Bind socket - if (bind( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen ) < 0) - { + if (bind( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen ) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to bind TCP Server socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1391,8 +1332,7 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) }; // Create que for 5 connections - if (listen( Handle->FD, Handle->Queue ) < 0) - { + if (listen( Handle->FD, Handle->Queue ) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to listen on TCP Server socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1411,9 +1351,8 @@ THandle * CSelectableCore::OpenTCPserverSocket( THandle * Handle ) ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, false, Handle, this ); - } // Set state HandleState( Handle, csOpen ); @@ -1433,17 +1372,14 @@ THandle * CSelectableCore::OpenTCPremoteSocket( THandle * Handle ) struct sockaddr_in address; // Validate - if (!Handle) { + if (!Handle) return NULL; - } // Check Handle type - if (Handle->Type == ctTCPserver) - { + if (Handle->Type == ctTCPserver) { // Accept connection on current socket addr_len = sizeof( address ); - if ((ClientFD = accept( Handle->FD, (struct sockaddr *)&address, &addr_len)) == -1) - { + if ((ClientFD = accept( Handle->FD, (struct sockaddr *)&address, &addr_len)) == -1) { // Log Event if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - TCP Server failed to accept blocking connection (%s)", @@ -1466,9 +1402,8 @@ THandle * CSelectableCore::OpenTCPremoteSocket( THandle * Handle ) // Get end of client list RemoteClient = &FirstHandle; - while (*RemoteClient) { + while (*RemoteClient) RemoteClient = &((*RemoteClient)->Next); - } // Create Remote Client Handle sprintf( ClientName, "%s-%d", Handle->Name, ClientFD ); @@ -1497,22 +1432,18 @@ THandle * CSelectableCore::OpenTCPremoteSocket( THandle * Handle ) ProcessName, Name, Handle->Name, ClientAddress ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( (*RemoteClient)->FD, true, true, *RemoteClient, this ); - } return *RemoteClient; } - else if (Handle->Type == ctTCPremote) - { + else if (Handle->Type == ctTCPremote) { // Check state - if (Handle->State == csOpen) - { + if (Handle->State == csOpen) { // Already open return Handle; } - else if (Handle->State == csWaitingtoOpen) - { + else if (Handle->State == csWaitingtoOpen) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Remote TCP Client connected [%s]", ProcessName, Name, Handle->Name, Handle->HostName ); @@ -1536,20 +1467,16 @@ THandle * CSelectableCore::OpenTCPclientSocket( THandle * Handle ) int TCPsyn_opt = 3; // Max SYN (connect retries) before open fails // Check state - if (Handle->State == csOpen) { - // Already open - return Handle; - } + if (Handle->State == csOpen) + return Handle; // Already open - if (Handle->State != csWaitingtoOpen) - { + if (Handle->State != csWaitingtoOpen) { // Resolve IP Address if (!ResolveAddress( Handle )) return NULL; // Create File descriptor - if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) - { + if ((Handle->FD = socket( Handle->AddressInfo->ai_family, Handle->AddressInfo->ai_socktype, Handle->AddressInfo->ai_protocol )) < 0) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Failed to create TCP Client socket [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1568,8 +1495,7 @@ THandle * CSelectableCore::OpenTCPclientSocket( THandle * Handle ) (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPIDLE, &TCPidle_opt, sizeof(TCPidle_opt)) == -1) || (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPCNT, &TCPcnt_opt, sizeof(TCPcnt_opt)) == -1) || (setsockopt( Handle->FD, SOL_TCP, TCP_KEEPINTVL, &TCPint_opt, sizeof(TCPint_opt)) == -1) || - (setsockopt( Handle->FD, SOL_TCP, TCP_SYNCNT, &TCPsyn_opt, sizeof(TCPsyn_opt)) == -1) ) - { + (setsockopt( Handle->FD, SOL_TCP, TCP_SYNCNT, &TCPsyn_opt, sizeof(TCPsyn_opt)) == -1)) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Could not set KeepAlive options [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); @@ -1584,45 +1510,39 @@ THandle * CSelectableCore::OpenTCPclientSocket( THandle * Handle ) } // Try to connect to address - if (!connect( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen )) - { + if (!connect( Handle->FD, Handle->AddressInfo->ai_addr, Handle->AddressInfo->ai_addrlen )) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - TCP Client connected [%s:%s]", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, true, Handle, this ); - } // Set status HandleState( Handle, csOpen ); return Handle; } - else if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINPROGRESS) || (errno == EALREADY)) - { + else if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINPROGRESS) || (errno == EALREADY)) { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - TCP Client waiting to connect [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( Handle->FD, true, true, Handle, this ); - } // Set status HandleState( Handle, csWaitingtoOpen ); return Handle; } - else - { + else { // Log Event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - TCP Client could not connect [%s:%s] (%s)", ProcessName, Name, Handle->Name, Handle->HostName, Handle->PortName, strerror(errno) ); // Remove from Select List - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, true, true ); - } // Close socket close( Handle->FD ); @@ -1640,9 +1560,8 @@ int CSelectableCore::Open( THandle * Handle ) THandle * NewHandle = NULL; // Validate - if (!Handle) { + if (!Handle) return -1; - } // Open correctly switch (Handle->Type) { @@ -1695,20 +1614,16 @@ bool CSelectableCore::Close( THandle * Handle, bool QuickReopen ) return false; // Close Children - if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUDPserver) || (Handle->Type == ctUNIXserver)) - { + if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUDPserver) || (Handle->Type == ctUNIXserver)) { ChildHandle = FirstHandle; - while (ChildHandle) - { - if (ChildHandle->Parent == Handle) - { + while (ChildHandle) { + if (ChildHandle->Parent == Handle) { // Close and remove handle NextHandle = ChildHandle->Next; Close( ChildHandle, false ); ChildHandle = NextHandle; } - else - { + else { // Skip Handle ChildHandle = ChildHandle->Next; } @@ -1722,17 +1637,15 @@ bool CSelectableCore::Close( THandle * Handle, bool QuickReopen ) } // Close Handle - if (Handle->Type == ctUDPremote) { + if (Handle->Type == ctUDPremote) Fail = false; - } else { + else Fail = (close( Handle->FD ))? true : false; - } // Remove from Select List if (!Fail && Selector) { - if (Handle->Type != ctUDPremote) { + if (Handle->Type != ctUDPremote) Selector->Remove( Handle->FD, true, true ); - } } // Reset FD @@ -1745,8 +1658,7 @@ bool CSelectableCore::Close( THandle * Handle, bool QuickReopen ) SetStartTime( &(Handle->LastAction) ); // Show action - switch (Handle->Type) - { + switch (Handle->Type) { case ctSerial: if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Serial Port %s [%s]", ProcessName, Name, Handle->Name, ((Fail)? "failed" : "closed"), Handle->Path ); @@ -1839,50 +1751,45 @@ bool CSelectableCore::Read( THandle * Handle ) char UDPport[20] = ""; // 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; - } // Log Read Event if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Read Event", ProcessName, Name, Handle->Name ); // Check for closing/opening event on Socket - if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUNIXserver)) - { + if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUNIXserver)) { // Incoming client request - if (Handle->Type == ctTCPserver) { + if (Handle->Type == ctTCPserver) ClientHandle = OpenTCPremoteSocket( Handle ); - } else if (Handle->Type == ctUNIXserver) { + else if (Handle->Type == ctUNIXserver) ClientHandle = OpenUNIXremoteSocket( Handle ); - } - if (ClientHandle == NULL) { + + if (ClientHandle == NULL) return false; - } + // Reset Timer SetStartTime( &(Handle->LastAction) ); // Add to Select Lists - if (Selector) { + if (Selector) Selector->Add( ClientHandle->FD, true, true, ClientHandle, this ); - } return true; } else if ((Handle->Type == ctTCPremote) || (Handle->Type == ctTCPclient) || - (Handle->Type == ctUNIXremote) || (Handle->Type == ctUNIXclient) ) - { + (Handle->Type == ctUNIXremote) || (Handle->Type == ctUNIXclient)) { // Check if socket ready (non-block open in progress) - if (Handle->State == csWaitingtoOpen) - { - if (Handle->Type == ctTCPremote) { + if (Handle->State == csWaitingtoOpen) { + if (Handle->Type == ctTCPremote) OpenTCPremoteSocket( Handle ); - } else if (Handle->Type == ctTCPclient) { + else if (Handle->Type == ctTCPclient) OpenTCPclientSocket( Handle ); - } else if (Handle->Type == ctUNIXremote) { + else if (Handle->Type == ctUNIXremote) OpenUNIXremoteSocket( Handle ); - } else if (Handle->Type == ctUNIXclient) { + else if (Handle->Type == ctUNIXclient) OpenUNIXclientSocket( Handle ); - } + // Reset Timer (for auto-close) SetStartTime( &(Handle->LastAction) ); @@ -1893,8 +1800,7 @@ bool CSelectableCore::Read( THandle * Handle ) ioctl( Handle->FD, FIONREAD, &BytesWaiting ); // Error or EOF from server - if (BytesWaiting < 1) - { + if (BytesWaiting < 1) { // Log if there is an error if (Log && (BytesWaiting < 1)) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)", @@ -1904,19 +1810,16 @@ bool CSelectableCore::Read( THandle * Handle ) Close( Handle, false ); // Destroy Client - if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote)) { + if ((Handle->Type == ctTCPremote) || (Handle->Type == ctUNIXremote)) RemoveHandle( Handle ); - } return false; } } - else if ((Handle->Type == ctUDPserver) || (Handle->Type == ctUDPclient) || (Handle->Type == ctUDPremote)) - { + else if ((Handle->Type == ctUDPserver) || (Handle->Type == ctUDPclient) || (Handle->Type == ctUDPremote)) { // Check if anything to read ioctl( Handle->FD, FIONREAD, &BytesWaiting ); - if (BytesWaiting < 0) - { + if (BytesWaiting < 0) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)", ProcessName, Name, Handle->Name, strerror(errno) ); @@ -1930,26 +1833,22 @@ bool CSelectableCore::Read( THandle * Handle ) UDPbuffer = (char*)malloc( BytesWaiting+1 ); BytesRead = ReadFromUDP( Handle, (char*)UDPaddress, (char*)UDPport, UDPbuffer, BytesWaiting ); - if (!errno && (Handle->Type == ctUDPserver)) - { + if (!errno && (Handle->Type == ctUDPserver)) { // Create/Find Incoming client ClientHandle = OpenUDPremoteSocket( Handle, UDPaddress, UDPport ); - if (ClientHandle == NULL) { + if (ClientHandle == NULL) return false; - } } // Reset Timer SetStartTime( &(Handle->LastAction) ); } - else if (Handle->Type == ctSerial) - { + else if (Handle->Type == ctSerial) { // Check if anything to read ioctl( Handle->FD, FIONREAD, &BytesWaiting ); // Error on port - if (BytesWaiting < 0) - { + if (BytesWaiting < 0) { if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Data waiting error (%s)", ProcessName, Name, Handle->Name, strerror(errno) ); @@ -1958,8 +1857,7 @@ bool CSelectableCore::Read( THandle * Handle ) return false; } } - else if (Handle->Type == ctLinePrinter) - { + else if (Handle->Type == ctLinePrinter) { // // Check if anything to read // ioctl( Handle->FD, FIONREAD, &BytesWaiting ); <-- Not valid ioctl for lp port // @@ -1976,17 +1874,14 @@ bool CSelectableCore::Read( THandle * Handle ) } // Validate - if (Handle->State != csOpen) { + if (Handle->State != csOpen) return false; - } // Read File directly into buffer - if (Handle->InBuffer) - { + if (Handle->InBuffer) { if ((Handle->Type == ctUDPserver) || (Handle->Type == ctUDPclient) || (Handle->Type == ctUDPremote)) { - if (BytesRead) { + if (BytesRead) Handle->InBuffer->Push( true, UDPbuffer, abs(BytesRead) ); - } if (UDPbuffer) free( UDPbuffer ); } else { @@ -2004,10 +1899,9 @@ bool CSelectableCore::Read( THandle * Handle ) ProcessName, Name, Handle->Name, BytesRead, BytesWaiting ); } - if (BytesRead != 0) { - // Process Buffer + // Process Buffer + if (BytesRead != 0) ProcessInputBuffer( Handle, false ); - } } // Reset timer @@ -2028,16 +1922,13 @@ bool CSelectableCore::Write( THandle * Handle ) return false; // 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? - if (!Handle->AutoManage) - { + if (!Handle->AutoManage) { // Must be opened manually return false; } - else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) - { + else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { // Attempt to re-open port Open( Handle ); } @@ -2047,31 +1938,27 @@ bool CSelectableCore::Write( THandle * Handle ) if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Handle '%s' - Write Event", ProcessName, Name, Handle->Name ); - if (Handle->State == csWaitingtoOpen) - { + if (Handle->State == csWaitingtoOpen) { // Complete socket open process - if (Handle->Type == ctTCPremote) { + if (Handle->Type == ctTCPremote) OpenTCPremoteSocket( Handle ); - } else if (Handle->Type == ctTCPclient) { + else if (Handle->Type == ctTCPclient) OpenTCPclientSocket( Handle ); - } else if (Handle->Type == ctUNIXremote) { + else if (Handle->Type == ctUNIXremote) OpenUNIXremoteSocket( Handle ); - } else if (Handle->Type == ctUNIXclient) { + else if (Handle->Type == ctUNIXclient) OpenUNIXclientSocket( Handle ); - } + // Reset Timer (for auto-close) SetStartTime( &(Handle->LastAction) ); // Remove from set for select write - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } return true; } - else if (Handle->State == csOpen) - { - if (Handle->OutBuffer) - { + else if (Handle->State == csOpen) { + if (Handle->OutBuffer) { // Write directly to handle / socket errno = 0; if ((Handle->Type == ctUDPclient) || (Handle->Type == ctUDPremote)) { @@ -2107,17 +1994,15 @@ bool CSelectableCore::Write( THandle * Handle ) ProcessName, Name, Handle->Name, BytesWritten, Len ); } - if (BytesWritten != 0) - { + if (BytesWritten != 0) { // Update Buffer Handle->OutBuffer->Clear( (BytesWritten > 0)? BytesWritten : -BytesWritten ); // negative value reported if error occurred // Check if Buffer empty if (!Handle->OutBuffer->Len()) { // Remove from Select Write list - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } } // Reset timeout @@ -2127,9 +2012,8 @@ bool CSelectableCore::Write( THandle * Handle ) else { // No Output buffer to write from, so remove from Write list - if (Selector) { + if (Selector) Selector->Remove( Handle->FD, false, true ); - } } return true; } @@ -2146,9 +2030,8 @@ int CSelectableCore::ReadFromUDP( THandle * Handle, char * RemoteAddr, char * Re socklen_t AddrLen = sizeof(Addr); // Check if buffer created - if (!Handle || (Handle->FD == -1) || !RemoteAddr || !RemotePort || (MaxLen < 1)) { + if (!Handle || (Handle->FD == -1) || !RemoteAddr || !RemotePort || (MaxLen < 1)) return 0; - } // Get datagram Addr.sin_family = AF_UNSPEC; // use AF_INET6 to force IPv6 @@ -2173,9 +2056,8 @@ int CSelectableCore::WriteToUDP( THandle * Handle, const char * Data, int Len, b int DataLen = (Len != -1)? Len : (Data)? strlen(Data) : 0; // Check if buffer created - if (!Handle || (Handle->FD == -1) || !DataLen) { + if (!Handle || (Handle->FD == -1) || !DataLen) return 0; - } // Set Options BytesWritten = sendto( Handle->FD, Data, DataLen, MSG_NOSIGNAL /*| ((!Force)? MSG_DONTWAIT : 0)*/, @@ -2191,18 +2073,15 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len int BytesWritten = 0; int DataLen = (Len != -1)? Len : (Data)? strlen(Data) : 0; - if ((Handle->State != csOpen)) - { + if ((Handle->State != csOpen)) { // Check if auto-managed handle - if (!Handle->AutoManage) - { + if (!Handle->AutoManage) { // 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)", ProcessName, Name, Handle->Name ); return 0; } - else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) - { + else if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { // Complete opening process Open( Handle ); @@ -2223,8 +2102,7 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len 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", ProcessName, Name, Handle->Name, TimeLeft( Handle->LastAction, Handle->ReopenDelay) ); return 0; @@ -2232,40 +2110,31 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len } // Check packet length - if (Len == -1) { + if (Len == -1) Len = strlen( Data ); - } - if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUNIXserver)) - { + if ((Handle->Type == ctTCPserver) || (Handle->Type == ctUNIXserver)) { // Cannot write to server socket, so Update Remote Client connections individually ChildHandle = FirstHandle; - while (ChildHandle) - { + while (ChildHandle) { // Check if child - if (ChildHandle->Parent == Handle) - { + if (ChildHandle->Parent == Handle) { // Decide where to put data - if (ChildHandle->OutBuffer) - { + if (ChildHandle->OutBuffer) { // Write to buffer BytesWritten = ChildHandle->OutBuffer->Push( true, Data, Len ); // Add to select write list - if (BytesWritten && Selector) { + if (BytesWritten && Selector) Selector->Add( ChildHandle->FD, false, true, ChildHandle, this ); - } } - else - { + else { // Write directly to handle / socket errno = 0; - if ((Handle->Type == ctUDPclient)|| (Handle->Type == ctUDPremote)) { + if ((Handle->Type == ctUDPclient)|| (Handle->Type == ctUDPremote)) BytesWritten = WriteToUDP( ChildHandle, Data, Len, true ); - } - else { + else BytesWritten = WriteToFD( ChildHandle->FD, Data, Len, true ); - } if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - OUT:", ProcessName, Name, ChildHandle->Name ); @@ -2280,10 +2149,9 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len ProcessName, Name, ChildHandle->Name, BytesWritten, DataLen ); } - if (BytesWritten != 0) { - // Reset timeout + // Reset timeout + if (BytesWritten != 0) SetStartTime( &(ChildHandle->LastAction) ); - } } } // Next @@ -2293,29 +2161,23 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len // Cannot verify individually, so assume all bytes was written BytesWritten = Len; } - else - { + else { // Decide where to put data - if (Handle->OutBuffer) - { + if (Handle->OutBuffer) { // Write to buffer BytesWritten = Handle->OutBuffer->Push( true, Data, Len ); // Add to select write list - if (BytesWritten && Selector) { + if (BytesWritten && Selector) Selector->Add( Handle->FD, false, true, Handle, this ); - } } - else - { + else { // Write directly to handle / socket errno = 0; - if ((Handle->Type == ctUDPclient)|| (Handle->Type == ctUDPremote)) { + if ((Handle->Type == ctUDPclient)|| (Handle->Type == ctUDPremote)) BytesWritten = WriteToUDP( Handle, Data, Len, true ); - } - else { + else BytesWritten = WriteToFD( Handle->FD, Data, Len, true ); - } if (Log) Log->Output( LogLevel, dlHigh, LogOutput, Data, Len, "%s/%s: Handle '%s' - OUT:", ProcessName, Name, Handle->Name ); @@ -2330,10 +2192,9 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len ProcessName, Name, Handle->Name, BytesWritten, DataLen ); } - if (BytesWritten != 0) { - // Reset timeout + // Reset timeout + if (BytesWritten != 0) SetStartTime( &(Handle->LastAction) ); - } } } @@ -2351,9 +2212,8 @@ bool CSelectableCore::WriteSerialConfig( THandle * Handle ) int mcs = 0; // Get Handle - if (!Handle || (Handle->Type != ctSerial)) { + if (!Handle || (Handle->Type != ctSerial)) return false; - } // Flush Data from port tcflush( Handle->FD, TCIFLUSH ); @@ -2367,8 +2227,7 @@ bool CSelectableCore::WriteSerialConfig( THandle * Handle ) return false; // Process the baud rate - switch (Handle->InBaudrate) - { + switch (Handle->InBaudrate) { case 921600: inbaud = B921600; break; case 576000: inbaud = B576000; break; case 460800: inbaud = B460800; break; @@ -2397,8 +2256,7 @@ bool CSelectableCore::WriteSerialConfig( THandle * Handle ) default: inbaud = B9600; break; } - switch (Handle->OutBaudrate) - { + switch (Handle->OutBaudrate) { case 921600: outbaud = B921600; break; case 576000: outbaud = B576000; break; case 460800: outbaud = B460800; break; @@ -2438,14 +2296,13 @@ bool CSelectableCore::WriteSerialConfig( THandle * Handle ) // Process the data bits newtio.c_cflag &= ~CSIZE; - switch (Handle->DataBits) - { - case 5: newtio.c_cflag |= CS5; break; - case 6: newtio.c_cflag |= CS6; break; - case 7: newtio.c_cflag |= CS7; break; - case 8: newtio.c_cflag |= CS8; break; + switch (Handle->DataBits) { + case 5: newtio.c_cflag |= CS5; break; + case 6: newtio.c_cflag |= CS6; break; + case 7: newtio.c_cflag |= CS7; break; + case 8: newtio.c_cflag |= CS8; break; - default: newtio.c_cflag |= CS8; break; + default: newtio.c_cflag |= CS8; break; } // Set other flags @@ -2504,8 +2361,7 @@ bool CSelectableCore::WriteSerialConfig( THandle * Handle ) newtio.c_cflag &= ~CRTSCTS; // Set Options (second time) - if (tcsetattr( Handle->FD, TCSANOW, &newtio ) != 0) - { + if (tcsetattr( Handle->FD, TCSANOW, &newtio ) != 0) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Port not configured", ProcessName, Name, Handle->Name ); @@ -2529,9 +2385,8 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle ) struct termios getTermios; // Validate handle - if (!Handle || (Handle->Type != ctSerial)) { + if (!Handle || (Handle->Type != ctSerial)) return false; - } // Get port setup tcgetattr( Handle->FD, &getTermios ); @@ -2539,8 +2394,7 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle ) outspeed = cfgetospeed( &getTermios ); // Get Baud Rate - switch (inspeed) - { + switch (inspeed) { case B0 : Handle->InBaudrate = 0; break; case B50 : Handle->InBaudrate = 50; break; case B75 : Handle->InBaudrate = 75; break; @@ -2562,8 +2416,7 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle ) case B230400: Handle->InBaudrate = 230400; break; default: Handle->InBaudrate = 0; break; } - switch (outspeed) - { + switch (outspeed) { case B0 : Handle->OutBaudrate = 0; break; case B50 : Handle->OutBaudrate = 50; break; case B75 : Handle->OutBaudrate = 75; break; @@ -2588,8 +2441,7 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle ) // Get Data Bits bits = getTermios.c_cflag & CSIZE; - switch (bits) - { + switch (bits) { case CS5: Handle->DataBits = 5; break; case CS6: Handle->DataBits = 6; break; case CS7: Handle->DataBits = 7; break; @@ -2630,17 +2482,14 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[ int Len; // Validate - if (!ExecPath || !*ExecPath) { + if (!ExecPath || !*ExecPath) return false; - } // Split params MatchPos = (char*)ExecPath; - for (;;) - { + for (;;) { // Look for whitespace - if (!ParamStarted) - { + if (!ParamStarted) { // Quotes starts quoted parameter if (*MatchPos == '"') { ParamStarted = true; @@ -2653,8 +2502,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[ StartPos = MatchPos; } } - else if (OpenQuotes) - { + else if (OpenQuotes) { // Another quote ends parameter if (*MatchPos == '"') { Len = MatchPos-StartPos-1; // Skip end quote @@ -2666,8 +2514,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[ OpenQuotes = false; } } - else - { + else { // Whitespace ends parameter if ((*MatchPos == ' ') || (*MatchPos == 0)) { Len = MatchPos-StartPos;