- 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
182 lines
4.3 KiB
C++
182 lines
4.3 KiB
C++
/*
|
|
* Select.cpp
|
|
*
|
|
* Created on: 13 May 2016
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
// redA Libraries
|
|
#include "TimingCore.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include "SelectableCore.h"
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// 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 CSelect::Clear()
|
|
{
|
|
// Clear Select sets
|
|
FD_ZERO( &ReadTestFDS );
|
|
FD_ZERO( &WriteTestFDS );
|
|
|
|
// Reset maximum File Descriptor
|
|
MaxFD = 0;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Add Select File Descriptor
|
|
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) {
|
|
(*Handle)->Read = true;
|
|
FD_SET( FD, &ReadTestFDS );
|
|
}
|
|
|
|
// Add Write Select
|
|
if (Write) {
|
|
(*Handle)->Write = true;
|
|
FD_SET( FD, &WriteTestFDS );
|
|
}
|
|
|
|
// Check Maximum File Handle
|
|
if (MaxFD <= FD)
|
|
MaxFD = FD+1;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void CSelect::Remove( int FD, bool Read, bool Write )
|
|
{
|
|
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) {
|
|
(*Handle)->Read = false;
|
|
FD_CLR( FD, &ReadTestFDS);
|
|
}
|
|
|
|
// Remove from set for select write check
|
|
if (Write) {
|
|
(*Handle)->Write = false;
|
|
FD_CLR( FD, &WriteTestFDS);
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool CSelect::Test()
|
|
{
|
|
TSelectHandle * Handle = NULL;
|
|
int Events = 0;
|
|
|
|
// Set Test sets
|
|
ReadFDS = ReadTestFDS;
|
|
WriteFDS = WriteTestFDS;
|
|
|
|
// Perform select
|
|
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)Events;
|
|
}
|
|
//---------------------------------------------------------------------------
|