Important Update:
- Converted Select functions into new class CSelectCore - Move Read/Write code from main() to SelectableCore Read()/Write() - Pass CSelectCore object to CSelectableCore on create - Updated SelectCore Read/Write lists directly from SelectableCore - SelectCore->Test() checks all FDs directly and call Read/Write functions - Improved checking/validating for methods in SelectableCore
This commit is contained in:
155
SelectCore.cpp
155
SelectCore.cpp
@@ -8,24 +8,40 @@
|
||||
// redA Libraries
|
||||
#include "TimingCore.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "SelectableCore.h"
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Select Variables
|
||||
fd_set ReadTestFDS;
|
||||
fd_set WriteTestFDS;
|
||||
fd_set ReadFDS;
|
||||
fd_set WriteFDS;
|
||||
int MaxFD = 0;
|
||||
timeval SelectTime;
|
||||
// Create Select
|
||||
CSelect::CSelect( long SelectTimeout )
|
||||
{
|
||||
// Clear List
|
||||
FirstHandle = NULL;
|
||||
|
||||
// Clear Select sets
|
||||
FD_ZERO( &ReadTestFDS );
|
||||
FD_ZERO( &WriteTestFDS );
|
||||
|
||||
// Reset maximum File Descriptor
|
||||
MaxFD = 0;
|
||||
|
||||
// Set Timeout
|
||||
SetInterval( &Timeout, SelectTimeout );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Destroy Select
|
||||
CSelect::~CSelect()
|
||||
{
|
||||
return;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Clear Select File Descriptors
|
||||
void SelectClear()
|
||||
void CSelect::Clear()
|
||||
{
|
||||
// Clear Select sets
|
||||
FD_ZERO( &ReadTestFDS );
|
||||
@@ -36,24 +52,39 @@ void SelectClear()
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Set Select timeout
|
||||
void SelectConfig( long SelectTimeout )
|
||||
{
|
||||
// Set Timeout
|
||||
SetInterval( &SelectTime, SelectTimeout );
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Add Select File Descriptor
|
||||
void SelectAdd( int FD, bool Read, bool Write )
|
||||
void CSelect::Add( int FD, bool Read, bool Write, CSelectableCore * Function )
|
||||
{
|
||||
TSelectHandle ** Handle = NULL;
|
||||
|
||||
// Check if Handle already exists
|
||||
Handle = &FirstHandle;
|
||||
while (*Handle && ((*Handle)->FD != FD)) {
|
||||
Handle = &((*Handle)->Next);
|
||||
}
|
||||
|
||||
// Create if not exist
|
||||
if (!*Handle) {
|
||||
// Create
|
||||
*Handle = (TSelectHandle*)malloc( sizeof(TSelectHandle) );
|
||||
memset( *Handle, 0, sizeof(TSelectHandle) );
|
||||
|
||||
// Set Parameters
|
||||
(*Handle)->FD = FD;
|
||||
(*Handle)->Function = Function;
|
||||
}
|
||||
|
||||
// Add Read select
|
||||
if (Read)
|
||||
if (Read) {
|
||||
(*Handle)->Read = true;
|
||||
FD_SET( FD, &ReadTestFDS );
|
||||
}
|
||||
|
||||
// Add Write Select
|
||||
if (Write)
|
||||
if (Write) {
|
||||
(*Handle)->Write = true;
|
||||
FD_SET( FD, &WriteTestFDS );
|
||||
}
|
||||
|
||||
// Check Maximum File Handle
|
||||
if (MaxFD <= FD)
|
||||
@@ -61,60 +92,90 @@ void SelectAdd( int FD, bool Read, bool Write )
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
void SelectRemove( int FD, bool Read, bool Write )
|
||||
void CSelect::Remove( int FD, bool Read, bool Write )
|
||||
{
|
||||
int TestFD = 0;
|
||||
TSelectHandle ** Handle = NULL;
|
||||
TSelectHandle * NextHandle = NULL;
|
||||
int TestFD = 0;
|
||||
|
||||
// Check if Handle already exists
|
||||
Handle = &FirstHandle;
|
||||
while (*Handle && ((*Handle)->FD != FD)) {
|
||||
Handle = &((*Handle)->Next);
|
||||
}
|
||||
// Check if found
|
||||
if (!*Handle)
|
||||
return;
|
||||
|
||||
// Remove from set for select read check
|
||||
if (Read)
|
||||
if (Read) {
|
||||
(*Handle)->Read = false;
|
||||
FD_CLR( FD, &ReadTestFDS);
|
||||
}
|
||||
|
||||
// Remove from set for select write check
|
||||
if (Write)
|
||||
if (Write) {
|
||||
(*Handle)->Write = false;
|
||||
FD_CLR( FD, &WriteTestFDS);
|
||||
}
|
||||
|
||||
// Check Maximum file handle
|
||||
if (FD == MaxFD-1) {
|
||||
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
||||
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
||||
break;
|
||||
// Check if to remove from list
|
||||
if (!(*Handle)->Read && !(*Handle)->Write)
|
||||
{
|
||||
// Remove from list
|
||||
NextHandle = (*Handle)->Next;
|
||||
free( *Handle );
|
||||
*Handle = NextHandle;
|
||||
|
||||
// Update Maximum Test FD
|
||||
if (FD == MaxFD-1) {
|
||||
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
|
||||
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
MaxFD = TestFD+1;
|
||||
}
|
||||
MaxFD = TestFD+1;
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool SelectTest()
|
||||
bool CSelect::Test()
|
||||
{
|
||||
int Result = 0;
|
||||
TSelectHandle * Handle = NULL;
|
||||
int Events = 0;
|
||||
|
||||
// Set Test sets
|
||||
ReadFDS = ReadTestFDS;
|
||||
WriteFDS = WriteTestFDS;
|
||||
|
||||
// Perform select
|
||||
Result = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &SelectTime );
|
||||
if (Result < 0)
|
||||
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &Timeout );
|
||||
if (Events < 0)
|
||||
{
|
||||
printf( "Select operation failed (%s)\n", strerror(errno) );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check all descriptors for events
|
||||
Handle = FirstHandle;
|
||||
while (Handle)
|
||||
{
|
||||
// Check read Event
|
||||
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
|
||||
Handle->Function->Read( Handle->FD );
|
||||
}
|
||||
|
||||
// Check Write Event
|
||||
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
|
||||
Handle->Function->Write( Handle->FD );
|
||||
}
|
||||
|
||||
// Next
|
||||
Handle = Handle->Next;
|
||||
}
|
||||
|
||||
// return success
|
||||
return (bool)Result;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Add Select File Descriptor
|
||||
bool SelectCheck( int FD, bool &Read, bool &Write )
|
||||
{
|
||||
// Add Read select
|
||||
Read = (bool)(FD_ISSET( FD, &ReadFDS ));
|
||||
|
||||
// Add Write Select
|
||||
Write = (bool)(FD_ISSET( FD, &WriteFDS ));
|
||||
|
||||
return (Read || Write);
|
||||
return (bool)Events;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user