Major update:

- Converted CSocketCore to Class/Object
- Implemented CFunctionCore as base class for CPortCore and CSocketCore
- Moved common functions to CFunctionCore and renamed
  eg. Maintain() -> Process()
- Pass Objects directly to each other as OutputFunction
- New Input() method allows data transfer between objects
This commit is contained in:
Charl Wentzel
2016-05-19 15:10:55 +02:00
parent e222d51997
commit 0d1c46ac53
8 changed files with 370 additions and 205 deletions

View File

@@ -29,38 +29,31 @@
//---------------------------------------------------------------------------
// Variables
char * ServerName = NULL;
char ServerAddress[25] = "";
int PortNo = 0;
char ClientAddress[25] = "";
CSocketCore::CSocketCore( const char * ServerName ) :
CFunctionCore( ServerName )
{
ServerAddress[0] = 0;
PortNo = 0;
ClientAddress[0] = 0;
int ServerHandle = -1;
ESocketState ServerState = ssNone;
int ClientHandle = -1;
ESocketState ClientState = ssNone;
char SockInBuffer[500] = "";
int SockInBufLen = 500;
int SockInLen = 0;
// SockIn Timer
timeval SockInStart = { 0, 0 };
long SockInTimeout = 100; // millisecs
int SockBytesRead = 0;
int SockBytesWaiting = 0;
ServerHandle = -1;
ServerState = ssNone;
ClientHandle = -1;
ClientState = ssNone;
}
//---------------------------------------------------------------------------
int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, bool KeepAlive )
CSocketCore::~CSocketCore()
{
}
//---------------------------------------------------------------------------
int CSocketCore::OpenTCPserverSocket( const char *pAddress, int pPortNo, bool KeepAlive )
{
socklen_t addr_len;
struct sockaddr_in address;
ServerName = (char*)malloc( strlen(pName)+1 );
strcpy( ServerName, pName );
strcpy( ServerAddress, pAddress );
PortNo = pPortNo;
@@ -87,7 +80,7 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
// Create socket
if ((ServerHandle = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf( "Server Socket: %s [%d] -> Failed to create server socket (%s)\n", ServerName, PortNo, strerror(errno) );
printf( "Server Socket: %s [%d] -> Failed to create server socket (%s)\n", Name, PortNo, strerror(errno) );
ServerState = ssFailed;
return -1;
};
@@ -96,7 +89,7 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
if ((setsockopt( ServerHandle, SOL_SOCKET, SO_LINGER, &ServerLinger_opt, sizeof(ServerLinger_opt)) == -1) ||
(setsockopt( ServerHandle, SOL_SOCKET, SO_REUSEADDR, &Reuse_opt, sizeof(Reuse_opt)) == -1))
{
printf( "Server Socket: %s [%d] -> Could not set socket options (%s)\n", ServerName, PortNo, strerror(errno) );
printf( "Server Socket: %s [%d] -> Could not set socket options (%s)\n", Name, PortNo, strerror(errno) );
ServerState = ssFailed;
return -1;
}
@@ -108,7 +101,7 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
(setsockopt( ServerHandle, SOL_TCP, TCP_KEEPCNT, &TCPcnt_opt, sizeof(TCPcnt_opt)) == -1) ||
(setsockopt( ServerHandle, SOL_TCP, TCP_KEEPINTVL, &TCPint_opt, sizeof(TCPint_opt)) == -1) ))
{
printf( "Server Socket: %s [%d] -> Could not set socket keepalive options (%s)\n", ServerName, PortNo, strerror(errno) );
printf( "Server Socket: %s [%d] -> Could not set socket keepalive options (%s)\n", Name, PortNo, strerror(errno) );
ServerState = ssFailed;
return -1;
}
@@ -116,7 +109,7 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
// Bind socket
if (bind( ServerHandle, (struct sockaddr *)&address, addr_len ) < 0)
{
printf( "Server Socket: %s [%d] -> Failed to bind server to socket (%s)\n", ServerName, PortNo, strerror(errno) );
printf( "Server Socket: %s [%d] -> Failed to bind server to socket (%s)\n", Name, PortNo, strerror(errno) );
close( ServerHandle );
ServerState = ssFailed;
return -1;
@@ -125,7 +118,7 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
// Create que for 5 connections
if (listen( ServerHandle, 5 ) < 0)
{
printf( "Server Socket: %s [%d] -> Failed to create server socket listen que (%s)\n", ServerName, PortNo, strerror(errno) );
printf( "Server Socket: %s [%d] -> Failed to create server socket listen que (%s)\n", Name, PortNo, strerror(errno) );
close( ServerHandle );
ServerState = ssFailed;
return -1;
@@ -133,12 +126,12 @@ int OpenTCPserverSocket( const char *pName, const char *pAddress, int pPortNo, b
// Server open
ServerState = ssOpen;
printf( "Server Socket: %s [%d] -> Socket binded and listening\n", ServerName, PortNo );
printf( "Server Socket: %s [%d] -> Socket binded and listening\n", Name, PortNo );
return ServerHandle;
}
//---------------------------------------------------------------------------
int OpenTCPinClientSocket()
int CSocketCore::OpenTCPinClientSocket()
{
socklen_t addr_len;
struct sockaddr_in address;
@@ -152,9 +145,9 @@ int OpenTCPinClientSocket()
if ((ClientHandle = accept( ServerHandle, (struct sockaddr *)&address, &addr_len)) == -1)
{
if (errno == EWOULDBLOCK)
printf( "Remote Socket: %s [*] -> Failed to accept blocking connection (%s)\n", ServerName, strerror(errno) );
printf( "Remote Socket: %s [*] -> Failed to accept blocking connection (%s)\n", Name, strerror(errno) );
else
printf( "Remote Socket: %s [*] -> Failed to accept connection (%s)\n", ServerName, strerror(errno) );
printf( "Remote Socket: %s [*] -> Failed to accept connection (%s)\n", Name, strerror(errno) );
close( ClientHandle );
return -1;
}
@@ -162,7 +155,7 @@ int OpenTCPinClientSocket()
// Return client address
strcpy( ClientAddress, inet_ntoa(address.sin_addr) );
ClientState = ssOpen;
printf( "Remote Socket: %s -> Server accepted connection from client (%s)\n", ServerName, ClientAddress );
printf( "Remote Socket: %s -> Server accepted connection from client (%s)\n", Name, ClientAddress );
}
else if (ClientState == ssWaitingtoOpen)
{
@@ -171,7 +164,7 @@ int OpenTCPinClientSocket()
fcntl( ClientHandle, F_SETFL, (!O_NONBLOCK)&flags );
// Log event
printf( "Socket: %s -> Client now connected to server (%s)\n", ServerName, ServerAddress );
printf( "Socket: %s -> Client now connected to server (%s)\n", Name, ServerAddress );
// Trigger handler & set new state
ClientState = ssOpen;
@@ -181,13 +174,11 @@ int OpenTCPinClientSocket()
}
//---------------------------------------------------------------------------
int OpenTCPoutClientSocket( const char *pName, const char *pAddress, int pPortNo, bool KeepAlive )
int CSocketCore::OpenTCPoutClientSocket( const char *pAddress, int pPortNo, bool KeepAlive )
{
socklen_t addr_len;
struct sockaddr_in address;
ServerName = (char*)malloc( strlen(pName)+1 );
strcpy( ServerName, pName );
strcpy( ServerAddress, pAddress );
PortNo = pPortNo;
@@ -203,7 +194,7 @@ int OpenTCPoutClientSocket( const char *pName, const char *pAddress, int pPortNo
// Create File descriptor
if ((ClientHandle = socket( AF_INET, SOCK_STREAM, 0 )) < 0)
{
printf( "Client Socket: %s [*] -> Failed to create Client Socket (%s)\n", ServerName, strerror(errno) );
printf( "Client Socket: %s [*] -> Failed to create Client Socket (%s)\n", Name, strerror(errno) );
return -1;
};
@@ -218,7 +209,7 @@ int OpenTCPoutClientSocket( const char *pName, const char *pAddress, int pPortNo
(setsockopt( ClientHandle, SOL_TCP, TCP_KEEPCNT, &TCPcnt_opt, sizeof(TCPcnt_opt)) == -1) ||
(setsockopt( ClientHandle, SOL_TCP, TCP_KEEPINTVL, &TCPint_opt, sizeof(TCPint_opt)) == -1) ))
{
printf( "Client Socket: %s -> Could not set socket keepalive options (%s)\n", ServerName, strerror(errno) );
printf( "Client Socket: %s -> Could not set socket keepalive options (%s)\n", Name, strerror(errno) );
return -1;
}
@@ -242,7 +233,7 @@ int OpenTCPoutClientSocket( const char *pName, const char *pAddress, int pPortNo
}
else
{
printf( "Client Socket: %s -> Client failed to connect (%s)\n", ServerName, strerror(errno) );
printf( "Client Socket: %s -> Client failed to connect (%s)\n", Name, strerror(errno) );
// Set status
ClientState = ssFailed;
@@ -255,7 +246,7 @@ int OpenTCPoutClientSocket( const char *pName, const char *pAddress, int pPortNo
//---------------------------------------------------------------------------
// Delete socket
bool CloseTCPSocket( ESockConnectType SocketType )
bool CSocketCore::CloseTCPSocket( ESockConnectType SocketType )
{
bool Fail;
@@ -272,20 +263,20 @@ bool CloseTCPSocket( ESockConnectType SocketType )
switch (SocketType)
{
case ctServer:
printf( "Server Socket: %s -> Server %s\n", ServerName, ((Fail)? "failed" : "closed") );
printf( "Server Socket: %s -> Server %s\n", Name, ((Fail)? "failed" : "closed") );
break;
case ctRemoteClient:
printf( "Remote Client: %s -> Connection to client %s\n", ServerName, ((Fail)? "failed" : "closed") );
printf( "Remote Client: %s -> Connection to client %s\n", Name, ((Fail)? "failed" : "closed") );
break;
case ctClient:
printf( "Client Socket: %s -> Connection to server %s\n", ServerName, ((Fail)? "failed" : "closed") );
printf( "Client Socket: %s -> Connection to server %s\n", Name, ((Fail)? "failed" : "closed") );
break;
case ctNone:
default:
printf( "Socket : %s -> Cannot %s socket (invalid socket type)\n", ServerName, ((Fail)? "fail" : "close") );
printf( "Socket : %s -> Cannot %s socket (invalid socket type)\n", Name, ((Fail)? "fail" : "close") );
break;
};
@@ -293,10 +284,19 @@ bool CloseTCPSocket( ESockConnectType SocketType )
}
//---------------------------------------------------------------------------
bool ReadClientSocket()
int CSocketCore::Input( int InputID, char * Data, int Len )
{
return WriteToFD( ClientHandle, Data, Len );
}
//---------------------------------------------------------------------------
bool CSocketCore::Read( int FD )
{
int SockBytesRead = 0;
int SockBytesWaiting = 0;
// Check if anything to read
ioctl( ClientHandle, FIONREAD, &SockBytesWaiting );
ioctl( FD, FIONREAD, &SockBytesWaiting );
// Test for close event
if (!SockBytesWaiting)
@@ -309,69 +309,71 @@ bool ReadClientSocket()
// Check if socket ready (non-block open not in progress)
else if (ClientState == ssWaitingtoOpen)
{
printf( "Socket: %s -> Cannot read from socket in waiting\n", ServerName );
printf( "Socket: %s -> Cannot read from socket in waiting\n", Name );
SockBytesWaiting = 0;
}
// Handel incoming data
// Handle incoming data
else
{
// Read data into buffer
SockBytesRead = read( ClientHandle, &SockInBuffer[SockInLen], SockBytesWaiting ); // SockInBufLen-SockInLen
// Check of errors
if (SockBytesRead == -1)
{
printf( "Socket: %s -> Cannot read from socket (%s)\n", ServerName, strerror(errno) );
SockBytesRead = 0;
// Check of non-blocking write
// bool Blocked = ((errno == EAGAIN)? true : false );
// Read data from socket
if (!(SockBytesRead = Buffer->ReadFromFD( FD, SockBytesWaiting ))) {
return false;
}
else if (SockBytesRead > 0)
{
// Process Reply
SockInLen += SockBytesRead;
// Reset timer
SetStartTime( &SockInStart );
}
// Process Buffer
ProcessBuffer( false );
// Start timer
SetStartTime( &InStart );
}
return true;
}
//---------------------------------------------------------------------------
bool MaintainSocket( int PortHandle )
bool CSocketCore::ProcessBuffer( bool Force )
{
// Init vars
long Duration = 0;
int BytesWritten = 0;
int StartWrite = 0;
int Pos = 0;
int Len = 0;
char * Data = NULL;
if (SockInLen > 0)
// Check if buffered data
if (!Buffer->Len()) {
return false;
}
// Check if forced processed
if (Force)
{
// Check duration since last PortIn
Duration = TimePassed( SockInStart );
if (Duration > SockInTimeout)
// Show Packet
Len = Buffer->Peek( &Data );
ShowOutput( "Port In", OUT_NORMAL, Data, Len );
// Write buffer to Port
if (OutFunction)
OutFunction->Input( 0, Data, Len );
// Clear processed bytes from buffer
Buffer->Clear( Len );
}
else
{
// Search for end of packet marker
while (Buffer->FindChar( '\n', Pos ))
{
// Handle buffer as packet
SockInBuffer[ SockInLen ] = 0;
ShowOutput( "Sock In", OUT_NORMAL|OUT_HEX, SockInBuffer, SockInLen );
// Show Packet
Len = Buffer->Peek( &Data, 0, Pos+1 );
ShowOutput( "Port In", OUT_NORMAL, Data, Len );
// Write output to Port
BytesWritten = 0;
StartWrite = 0;
if (PortHandle != -1) {
while (StartWrite < SockInLen) {
BytesWritten = write( PortHandle, &SockInBuffer[StartWrite], SockInLen );
StartWrite += BytesWritten;
}
}
// Write buffer to Port
if (OutFunction)
OutFunction->Input( 0, Data, Len );
// Reset buffer and timer
SockInLen = 0;
SetInterval( &SockInStart, 0 );
// Clear processed bytes from buffer
Buffer->Clear( Len );
}
}
return true;
}
//---------------------------------------------------------------------------