Initial Commit:
- Create single library for all Core RedA functions/classes - Working version
This commit is contained in:
123
SelectCore.cpp
Normal file
123
SelectCore.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Select.cpp
|
||||
*
|
||||
* Created on: 13 May 2016
|
||||
* Author: wentzelc
|
||||
*/
|
||||
|
||||
// redA Libraries
|
||||
#include "TimingCore.h"
|
||||
#include "SelectCore.h"
|
||||
|
||||
// Standard C/C++ Libraries
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.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 )
|
||||
{
|
||||
// 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) {
|
||||
for (int test = MaxFD-1; test >= 0; test--) {
|
||||
if (FD_ISSET( test, &ReadTestFDS ) || FD_ISSET( test, &WriteTestFDS )) {
|
||||
MaxFD = test+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
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);
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user