- Merged SelectCore and PortCore into new SelectableCore Single well integrated Class - Rename SelectCore.h to SelectableCore.h - Create new TFileHandle structure for Ports/Sockets - Moved buffer and Input Timeout from FunctionCore to TFileHandle - Moved Read, Write and ProcessBuffer from FunctionCore to TFileHandle Bug Fixes: - malloc correct size for names - Calculate MaxFD correctly when closing FDs
121 lines
2.8 KiB
C++
121 lines
2.8 KiB
C++
/*
|
|
* Select.cpp
|
|
*
|
|
* Created on: 13 May 2016
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
// redA Libraries
|
|
#include "TimingCore.h"
|
|
#include <stdio.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;
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Clear Select File Descriptors
|
|
void SelectClear()
|
|
{
|
|
// Clear Select sets
|
|
FD_ZERO( &ReadTestFDS );
|
|
FD_ZERO( &WriteTestFDS );
|
|
|
|
// Reset maximum File Descriptor
|
|
MaxFD = 0;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Set Select timeout
|
|
void SelectConfig( long SelectTimeout )
|
|
{
|
|
// Set Timeout
|
|
SetInterval( &SelectTime, SelectTimeout );
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Add Select File Descriptor
|
|
void SelectAdd( int FD, bool Read, bool Write )
|
|
{
|
|
// Add Read select
|
|
if (Read)
|
|
FD_SET( FD, &ReadTestFDS );
|
|
|
|
// Add Write Select
|
|
if (Write)
|
|
FD_SET( FD, &WriteTestFDS );
|
|
|
|
// Check Maximum File Handle
|
|
if (MaxFD <= FD)
|
|
MaxFD = FD+1;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void SelectRemove( int FD, bool Read, bool Write )
|
|
{
|
|
int TestFD = 0;
|
|
|
|
// Remove from set for select read check
|
|
if (Read)
|
|
FD_CLR( FD, &ReadTestFDS);
|
|
|
|
// Remove from set for select write check
|
|
if (Write)
|
|
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;
|
|
}
|
|
}
|
|
MaxFD = TestFD+1;
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool SelectTest()
|
|
{
|
|
int Result = 0;
|
|
|
|
// Set Test sets
|
|
ReadFDS = ReadTestFDS;
|
|
WriteFDS = WriteTestFDS;
|
|
|
|
// Perform select
|
|
Result = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &SelectTime );
|
|
if (Result < 0)
|
|
{
|
|
printf( "Select operation failed (%s)\n", strerror(errno) );
|
|
return false;
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
//---------------------------------------------------------------------------
|