- 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
177 lines
5.6 KiB
C++
177 lines
5.6 KiB
C++
/*
|
|
* Select.h
|
|
*
|
|
* Created on: 13 May 2016
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
#ifndef REDACORE_SELECTABLECORE_H_
|
|
#define REDACORE_SELECTABLECORE_H_
|
|
|
|
// redA Libraries
|
|
#include "FunctionCore.h"
|
|
|
|
// Standard C/C++ Libraries
|
|
#include <string.h>
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Types required for connections
|
|
typedef enum { ctNone = 0, ctPort = 1, ctServer = 2, ctRemoteClient = 3, ctClient = 4 } EConnectType;
|
|
const char ConnectTypeName[][15] = { "None", "Port", "Server", "RemoteClient", "Client" };
|
|
|
|
typedef enum { csNone = 0, csWaitingtoOpen = 1, csOpen = 2, csDataWaiting = 3, csClosed = 4, csFailed = 5 } EConnectState;
|
|
const char ConnectStateName[][15] = { "None", "WaitingToOpen", "Open", "DataWaiting", "Closed", "Failed" };
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Defines required to configure port
|
|
#define NO_PARITY 0
|
|
#define ODD_PARITY 1
|
|
#define EVEN_PARITY 2
|
|
#define MARK_PARITY 3
|
|
#define SPACE_PARITY 4
|
|
|
|
#define NO_FLOWCTRL 0
|
|
#define HW_FLOWCTRL 1
|
|
#define SW_FLOWCTRL 2
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
void SelectConfig( long Timeout );
|
|
void SelectClear();
|
|
void SelectAdd( int FD, bool Read, bool Write );
|
|
void SelectRemove( int FD, bool Read, bool Write );
|
|
bool SelectTest();
|
|
bool SelectCheck( int FD, bool &Read, bool &Write );
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
typedef struct SHandle THandle;
|
|
struct SHandle {
|
|
// Description
|
|
char * Name;
|
|
EConnectType Type;
|
|
|
|
char * FileName;
|
|
|
|
char * Address;
|
|
int PortNo;
|
|
bool KeepAlive;
|
|
|
|
int FD;
|
|
EConnectState State;
|
|
|
|
// Buffers
|
|
CBuffer * InBuffer;
|
|
CBuffer * OutBuffer;
|
|
|
|
// Input Markers
|
|
char * InMarker;
|
|
int InMarkerLen;
|
|
|
|
// Input Timer
|
|
timeval InStart;
|
|
long InTimeout; // millisecs
|
|
|
|
THandle * Parent;
|
|
THandle * Next;
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
class CSelectableCore : public CFunctionCore
|
|
{
|
|
protected:
|
|
// Device Interfaces
|
|
THandle * FirstHandle;
|
|
|
|
// Managing File Handles
|
|
bool RemoveHandle( THandle * Handle );
|
|
bool DestroyHandle( THandle * Handle );
|
|
|
|
// Finding files
|
|
inline THandle * GetHandle( const char * HandleName )
|
|
{
|
|
if (!HandleName) return NULL;
|
|
THandle * Handle = FirstHandle;
|
|
while ( Handle && strcmp( HandleName, Handle->Name ))
|
|
Handle = Handle->Next;
|
|
return Handle;
|
|
}
|
|
inline THandle * GetHandle( int FD )
|
|
{
|
|
if (FD < 0) return NULL;
|
|
THandle * Handle = FirstHandle;
|
|
while ( Handle && (FD != Handle->FD))
|
|
Handle = Handle->Next;
|
|
return Handle;
|
|
}
|
|
|
|
// Port Operations
|
|
virtual int OpenPort( THandle * Handle );
|
|
|
|
// Socket Operations
|
|
virtual int OpenServerSocket( THandle * Handle );
|
|
//virtual int OpenRemoteClientSocket( THandle * Handle );
|
|
virtual int OpenClientSocket( THandle * Handle );
|
|
|
|
// Mutual Operations
|
|
virtual bool Close( THandle * Handle, bool CloseChildren = false );
|
|
virtual bool Read( THandle * Handle );
|
|
virtual bool Write( THandle * Handle );
|
|
|
|
int WriteToFD( int FD, const char * Data, int Len );
|
|
|
|
// Buffer operations
|
|
virtual bool ProcessBuffer( THandle * Handle, bool Force );
|
|
|
|
public:
|
|
// Life Cycle
|
|
CSelectableCore( const char * Name );
|
|
~CSelectableCore();
|
|
|
|
// Configuration
|
|
THandle * CreateHandle( const char * HandleName );
|
|
bool SetBuffers( const char * HandleName, int InBufSize, int OutBufSize, int InTimeout, const char * InMarker, int InMarkerLen );
|
|
bool SerialConfig( const char * HandleName, int Baud, short DataBits, short StopBits, short Parity, short FlowCtrl, int Wait );
|
|
|
|
// Device Interface
|
|
bool SetPortHandle( const char * HandleName, const char * FileName );
|
|
bool SetSocketHandle( const char * HandleName, EConnectType Type, const char * Address, const int PortNo, bool KeepAlive );
|
|
bool ClearHandle( const char * HandleName );
|
|
|
|
virtual int Open( const char * HandleName );
|
|
virtual int OpenRemoteClientSocket( THandle * Handle );
|
|
|
|
virtual bool Close( const char * HandleName, bool CloseChildren = false ) { return (Close( GetHandle( HandleName ), CloseChildren )); };
|
|
virtual bool Close( int FD, bool CloseChildren = false ) { return (Close( GetHandle( FD ), CloseChildren )); };
|
|
|
|
virtual bool Read( const char * HandleName ) { return (Read( GetHandle( HandleName ))); };
|
|
virtual bool Read( int FD ) { return (Read( GetHandle( FD ))); };
|
|
|
|
virtual bool Write( const char * HandleName ) { return (Write( GetHandle( HandleName ))); };
|
|
virtual bool Write( int FD ) { return (Write( GetHandle( FD ))); };
|
|
|
|
inline int GetFD( const char * HandleName ) {
|
|
THandle * Handle = GetHandle( HandleName );
|
|
return ((Handle)? Handle->FD : -1);
|
|
};
|
|
inline EConnectType GetType( const char * HandleName ) {
|
|
THandle * Handle = GetHandle( HandleName );
|
|
return ((Handle)? Handle->Type : ctNone);
|
|
};
|
|
inline EConnectState GetState( const char * HandleName ) {
|
|
THandle * Handle = GetHandle( HandleName );
|
|
return ((Handle)? Handle->State : csNone);
|
|
};
|
|
|
|
// Function Interface
|
|
virtual int Input( int FD, const char * Buffer, int BufLen );
|
|
virtual bool Process();
|
|
};
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif /* REDACORE_SELECTABLECORE_H_ */
|