So I am looking for a more general solution - and, hopefully an explanation or hint on how to do this 'properly'
My code looks like:
Code: Select all
bool MyFrame::GetNextLineFromServer( wxSocketClient *pSock, wxString &wsLine )
{
wxString wsT;
int i, j;
int iLen;
wxCharBuffer buf2( 256 );
char buf3[256];
wsLine = wxEmptyString;
while ( true )
{
if( !pSock->WaitForRead( 2 ) )
return false; // must be timeout
pSock->Read(buf2.data(), sizeof(buf2) ); <<<<<<<<<<<<<<<<< hangs here waiting for Read to
iLen = pSock->LastReadCount(); return with last 3 bytes
memset( buf3, '\0', iLen + 1 );
memcpy( buf3, buf2.data(), iLen );
wsT = wxEmptyString;
for ( j = 0; j < iLen; j++ )
{
wsT.append( buf3[j] );
if ( buf3[j] == '\n' )
{
wsLine += wsT;
// put back any unused bytes
// for the next call
if( j < (iLen - 1))
{
pSock->Unread(&(buf3[j+1]), iLen - j - 1 );
}
return true;
}
}
if ( wsT.IsEmpty() )
return false;
wsLine += wsT;
}
return true;
}
All is well until the timing and data lengths result in the last buffer read to contain enough data for the next-to-last-line plus the 3 char long terminator line, which then ends up being pushed back to the read buffer in the socket.
BUT, at this time, the server has nothing more to send, so the last read for the terminating line hangs until the socket times out , at which point it does return the expected data, but of course, the server has hung up and won't accept any further request.
My expectation was and still is that the next time Read is called, it would return immediately, because the buffer contains the pushed back line - but it does not!
And only in this specific case. The push back code gets call dozens of times and as long as there is more data coming down the pipe from the server, all works as expected. Except for that last call.
Has anyone else run into this issue and/or how can I avoid it or work around it in a reasonably general way?
TIA
Arnold