Major Update:

- 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
This commit is contained in:
Charl Wentzel
2016-05-23 14:35:15 +02:00
parent 0d1c46ac53
commit dcfbd85efa
11 changed files with 1116 additions and 953 deletions

View File

@@ -12,7 +12,6 @@
// Standard C/C++ Libraries
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
@@ -23,21 +22,15 @@ CFunctionCore::CFunctionCore( const char * FunctionName )
{
// Set name
if (FunctionName) {
Name = (char*)malloc( strlen(FunctionName) );
Name = (char*)malloc( strlen(FunctionName)+1 );
strcpy( Name, FunctionName );
}
else {
Name = NULL;
}
// In buffer
Buffer = new CBuffer( 20 );
// IO Functions
OutFunction = NULL;
// Input Timeout
InStart.tv_sec = 0;
InStart.tv_usec = 0;
InTimeout = 100; // millisecs
}
//---------------------------------------------------------------------------
@@ -47,20 +40,17 @@ CFunctionCore::~CFunctionCore()
if (Name) {
free( Name );
}
if (Buffer) {
delete Buffer;
}
}
//---------------------------------------------------------------------------
// Manual Data Input/Output
int CFunctionCore::Input( int InputID, char * Buffer, int MaxLen )
int CFunctionCore::Input( int InputID, const char * Buffer, int MaxLen )
{
return 0;
}
//---------------------------------------------------------------------------
int CFunctionCore::Output( int OutputID, char * Buffer, int Len )
int CFunctionCore::Output( int OutputID, const char * Buffer, int Len )
{
return 0;
}
@@ -81,81 +71,4 @@ bool CFunctionCore::AddOutput( int OutputID, CFunctionCore * InFunction, int Inp
}
//---------------------------------------------------------------------------
bool CFunctionCore::Process()
{
// Misc
long Duration = 0;
if (Buffer->Len() > 0)
{
// Check duration since last PortIn
Duration = TimePassed( InStart );
if (Duration > InTimeout)
{
// Process Input
ProcessBuffer( true );
// Reset timer
SetInterval( &InStart, 0 );
}
}
return true;
}
//---------------------------------------------------------------------------
// Device Interface
bool CFunctionCore::Read( int fd )
{
int BytesRead = 0;
// Read File directly into buffer
if (!(BytesRead = Buffer->ReadFromFD( fd )))
return false;
// Process Buffer
ProcessBuffer( false );
// Reset timer
SetStartTime( &InStart );
return true;
}
//---------------------------------------------------------------------------
bool CFunctionCore::Write( int fd )
{
return false;
}
//---------------------------------------------------------------------------
int CFunctionCore::WriteToFD( int FD, char * Data, int Len )
{
int BytesWritten = 0;
int TotalWritten = 0;
int DataRemain = 0;
// Check if buffer created
if ((FD == -1) || !Data) {
return 0;
}
// Read Data into buffer
DataRemain = (Len == -1)? strlen(Data) : Len;
while (DataRemain)
{
// Read from file descriptor
BytesWritten = write( FD, Data, DataRemain );
if (BytesWritten <= 0)
break;
// Update Data Pointers
TotalWritten += BytesWritten;
DataRemain -= BytesWritten;
}
return TotalWritten;
}
//---------------------------------------------------------------------------