Major update:

- Convert PortCore functions into Class
Bug fixes:
  - Correctly handle socket close event (return false on read)
  - Only write to port/socket if valid file handle
This commit is contained in:
Charl Wentzel
2016-05-17 07:44:08 +02:00
parent 264169e525
commit d7facce4de
3 changed files with 163 additions and 112 deletions

View File

@@ -297,44 +297,43 @@ bool ReadClientSocket()
// Check if anything to read
ioctl( ClientHandle, FIONREAD, &SockBytesWaiting );
if (SockBytesWaiting)
{
// Handle Incoming Data
// Check if socket ready (non-block open not in progress)
if (ClientState == ssWaitingtoOpen)
{
printf( "Socket: %s -> Cannot read from socket in waiting\n", ServerName );
SockBytesWaiting = 0;
}
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 );
}
else if (SockBytesRead > 0)
{
// Process Reply
SockInLen += SockBytesRead;
// Reset timer
SetStartTime( &SockInStart );
}
}
}
else // if (!SockBytesWaiting)
// Test for close event
if (!SockBytesWaiting)
{
// EOF from server (close connection)
CloseTCPSocket( ctRemoteClient );
ClientHandle = -1;
return false;
}
// 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 );
SockBytesWaiting = 0;
}
// Handel 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 );
}
else if (SockBytesRead > 0)
{
// Process Reply
SockInLen += SockBytesRead;
// Reset timer
SetStartTime( &SockInStart );
}
}
return true;
}
@@ -360,9 +359,11 @@ bool MaintainSocket( int PortHandle )
// Write output to Port
BytesWritten = 0;
StartWrite = 0;
while (StartWrite < SockInLen) {
BytesWritten = write( PortHandle, &SockInBuffer[StartWrite], SockInLen );
StartWrite += BytesWritten;
if (PortHandle != -1) {
while (StartWrite < SockInLen) {
BytesWritten = write( PortHandle, &SockInBuffer[StartWrite], SockInLen );
StartWrite += BytesWritten;
}
}
// Reset buffer and timer