From d18e046e578a74f365ee8e61770ec9b14d9327fd Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Sun, 4 Nov 2018 17:31:12 +0200 Subject: [PATCH] Important Update: - SelectableCore: - Bug fix: correct ConnectTypeNames - Pass handle reference to Select when adding file discriptor. - Rename Select vars Handle -> SelectHandle - Show type and name of handle on Select logs --- SelectCore.cpp | 78 +++++++++++++++++++++++++++------------------- SelectableCore.cpp | 20 ++++++------ SelectableCore.h | 5 +-- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/SelectCore.cpp b/SelectCore.cpp index a60bea1..65a1155 100644 --- a/SelectCore.cpp +++ b/SelectCore.cpp @@ -84,51 +84,61 @@ void CSelect::Clear() //--------------------------------------------------------------------------- // Add Select File Descriptor -void CSelect::Add( int FD, bool Read, bool Write, CSelectableCore * Function ) +void CSelect::Add( int FD, bool Read, bool Write, THandle * Handle, CSelectableCore * Function ) { - TSelectHandle ** Handle = NULL; + TSelectHandle ** SelectHandle = NULL; - // Check if Handle already exists - Handle = &FirstHandle; - while (*Handle && ((*Handle)->FD != FD)) { - Handle = &((*Handle)->Next); + // Check if SelectHandle already exists + SelectHandle = &FirstHandle; + while (*SelectHandle && ((*SelectHandle)->FD != FD)) { + SelectHandle = &((*SelectHandle)->Next); } - if (!*Handle) { + if (!*SelectHandle) { // Create if not exist - *Handle = (TSelectHandle*)malloc( sizeof(TSelectHandle) ); - memset( *Handle, 0, sizeof(TSelectHandle) ); + *SelectHandle = (TSelectHandle*)malloc( sizeof(TSelectHandle) ); + memset( *SelectHandle, 0, sizeof(TSelectHandle) ); // Set Parameters - (*Handle)->FD = FD; - (*Handle)->Function = Function; + (*SelectHandle)->FD = FD; + (*SelectHandle)->Handle = Handle; + (*SelectHandle)->Function = Function; + + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Created", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } - else if ((*Handle)->Function != Function) { + else if ((*SelectHandle)->Function != Function) { // Old handle for another function, not yet removed, remove from read/write lists Remove( FD, true, true ); // Overwrite Parameters - (*Handle)->Function = Function; + (*SelectHandle)->Handle = Handle; + (*SelectHandle)->Function = Function; + + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Re-used", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } // Add Read select - if (Read && !(*Handle)->Read) { - (*Handle)->Read = true; + if (Read && !(*SelectHandle)->Read) { + (*SelectHandle)->Read = true; FD_SET( FD, &ReadTestFDS ); // Log event - if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Add Read", FD ); + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Add Read", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } // Add Write Select - if (Write && !(*Handle)->Write) { - (*Handle)->Write = true; + if (Write && !(*SelectHandle)->Write) { + (*SelectHandle)->Write = true; FD_SET( FD, &WriteTestFDS ); // Log event - if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Add Write", FD ); + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Add Write", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } - // Check Maximum File Handle + // Check Maximum File SelectHandle if (MaxFD <= FD) MaxFD = FD+1; } @@ -136,33 +146,37 @@ void CSelect::Add( int FD, bool Read, bool Write, CSelectableCore * Function ) void CSelect::Remove( int FD, bool Read, bool Write ) { - TSelectHandle ** Handle = NULL; + TSelectHandle ** SelectHandle = NULL; + THandle * Handle = NULL; - // Check if Handle already exists - Handle = &FirstHandle; - while (*Handle && ((*Handle)->FD != FD)) { - Handle = &((*Handle)->Next); + // Check if SelectHandle already exists + SelectHandle = &FirstHandle; + while (*SelectHandle && ((*SelectHandle)->FD != FD)) { + SelectHandle = &((*SelectHandle)->Next); } // Check if found - if (!*Handle) + if (!*SelectHandle) return; + Handle = (*SelectHandle)->Handle; // Remove from set for select read check - if (Read && (*Handle)->Read) { - (*Handle)->Read = false; + if (Read && (*SelectHandle)->Read) { + (*SelectHandle)->Read = false; FD_CLR( FD, &ReadTestFDS); // Log event - if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Remove Read", FD ); + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Remove Read", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } // Remove from set for select write check - if (Write && (*Handle)->Write) { - (*Handle)->Write = false; + if (Write && (*SelectHandle)->Write) { + (*SelectHandle)->Write = false; FD_CLR( FD, &WriteTestFDS); // Log event - if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Remove Write", FD ); + if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d], %s, %s - Remove Write", FD, + ConnectTypeName[((Handle)? Handle->Type : 0)], ((Handle && Handle->Name)? Handle->Name : "") ); } // Handle will be removed in Test() if both Read & Write flags are false } diff --git a/SelectableCore.cpp b/SelectableCore.cpp index 9f64495..0628eb5 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -582,7 +582,7 @@ int CSelectableCore::OpenSerialPort( THandle * Handle ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, true, false, this ); + Selector->Add( Handle->FD, true, false, Handle, this ); } // Set state @@ -622,7 +622,7 @@ int CSelectableCore::OpenLinePrinterPort( THandle * Handle ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, true, false, this ); + Selector->Add( Handle->FD, true, false, Handle, this ); } // Set state @@ -718,7 +718,7 @@ int CSelectableCore::OpenForkPipe( THandle * Handle ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, false, true, this ); + Selector->Add( Handle->FD, false, true, Handle, this ); } // Set state @@ -910,7 +910,7 @@ int CSelectableCore::OpenServerSocket( THandle * Handle, bool DelayResolve ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, true, false, this ); + Selector->Add( Handle->FD, true, false, Handle, this ); } // Set state @@ -993,7 +993,7 @@ int CSelectableCore::OpenRemoteClientSocket( THandle * Handle ) // Add to Select Lists if (Selector) { - Selector->Add( (*RemoteClient)->FD, true, true, this ); + Selector->Add( (*RemoteClient)->FD, true, true, Handle, this ); } return (*RemoteClient)->FD; @@ -1079,7 +1079,7 @@ int CSelectableCore::OpenClientSocket( THandle * Handle, bool DelayResolve ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, true, true, this ); + Selector->Add( Handle->FD, true, true, Handle, this ); } // Set status @@ -1093,7 +1093,7 @@ int CSelectableCore::OpenClientSocket( THandle * Handle, bool DelayResolve ) // Add to Select Lists if (Selector) { - Selector->Add( Handle->FD, true, true, this ); + Selector->Add( Handle->FD, true, true, Handle, this ); } // Set status @@ -1278,7 +1278,7 @@ bool CSelectableCore::Read( THandle * Handle ) // Add to Select Lists if (Selector) { - Selector->Add( ClientFD, true, true, this ); + Selector->Add( ClientFD, true, true, Handle, this ); } return true; } @@ -1681,7 +1681,7 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len // Add to select write list if (BytesWritten && Selector) { - Selector->Add( ChildHandle->FD, false, true, this ); + Selector->Add( ChildHandle->FD, false, true, ChildHandle, this ); } } else @@ -1713,7 +1713,7 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len // Add to select write list if (BytesWritten && Selector) { - Selector->Add( Handle->FD, false, true, this ); + Selector->Add( Handle->FD, false, true, Handle, this ); } } else diff --git a/SelectableCore.h b/SelectableCore.h index 3b87adf..b9bd9e1 100644 --- a/SelectableCore.h +++ b/SelectableCore.h @@ -18,7 +18,7 @@ // Types required for connections typedef enum { ctNone = 0, ctSerial = 1, ctLinePrinter = 2, ctServer = 3, ctRemoteClient = 4, ctClient = 5, ctForkPipe = 6 } EConnectType; -const char ConnectTypeName[][15] = { "None", "Port", "ForkPipe", "Server", "RemoteClient", "Client" }; +const char ConnectTypeName[][15] = { "None", "SerialPort", "ParallelPort", "Server", "RemoteClient", "Client", "ForkPipe" }; typedef enum { csNone = 0, csOpenRequest = 1, csWaitingtoOpen = 2, csOpen = 3, csDataWaiting = 4, csClosed = 5, csFailed = 6 } EConnectState; const char ConnectStateName[][15] = { "None", "OpenRequest", "WaitingToOpen", "Open", "DataWaiting", "Closed", "Failed" }; @@ -58,6 +58,7 @@ struct SSelectHandle { bool Write; // Event Object + THandle * Handle; CSelectableCore * Function; // List @@ -160,7 +161,7 @@ public: // Manage FDs void Clear(); - void Add( int FD, bool Read, bool Write, CSelectableCore * Function = NULL); + void Add( int FD, bool Read, bool Write, THandle * Handle, CSelectableCore * Function = NULL); void Remove( int FD, bool Read, bool Write ); // Testing FDs