// Read a line from socket
// ----------------------------------------------------------------------------
-// TODO ReadLine() should use buffers private to wxProtocol for efficiency!
-
-// static
+/* static */
wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result)
{
- result.Empty();
- char ch, chLast = '\0';
- while ( !socket->Read(&ch, sizeof(ch)).Error() )
+ static const int LINE_BUF = 4095;
+
+ result.clear();
+
+ wxCharBuffer buf(LINE_BUF);
+ while ( sock->WaitForRead() )
{
- switch ( ch )
+ // peek at the socket to see if there is a CRLF
+ sock->Peek(buf.data(), LINE_BUF);
+
+ size_t nRead = sock->LastCount();
+ if ( !nRead && sock->Error() )
+ return wxPROTO_NETERR;
+
+ // look for "\r\n" paying attention to a special case: "\r\n" could
+ // have been split by buffer boundary, so check also for \r at the end
+ // of the last chunk and \n at the beginning of this one
+ buf.data()[nRead] = '\0';
+ const char *eol = strchr(buf, '\n');
+
+ // if we found '\n', is there a '\r' as well?
+ if ( eol )
{
- case '\r':
- // remember it, if the following is '\n', we're done
- chLast = '\r';
- break;
-
- case '\n':
- // only ends line if the previous character was '\r'
- if ( chLast == '\r' )
+ if ( eol == buf.data() )
+ {
+ // check for case of "\r\n" being split
+ if ( result.empty() || result.Last() != _T('\r') )
{
- // EOL found
- return wxPROTO_NOERR;
+ // ignore the stray '\n'
+ eol = NULL;
}
- //else: fall through
+ //else: ok, got real EOL
- default:
- // normal char
- if ( chLast )
+ // read just this '\n' and restart
+ nRead = 1;
+ }
+ else // '\n' in the middle of the buffer
+ {
+ // in any case, read everything up to and including '\n'
+ nRead = eol - buf + 1;
+
+ if ( eol[-1] != '\r' )
{
- result += wxString::FromAscii( chLast );
- chLast = '\0';
+ // as above, simply ignore stray '\n'
+ eol = NULL;
}
+ }
+ }
+
+ sock->Read(buf.data(), nRead);
+ if ( sock->LastCount() != nRead )
+ return wxPROTO_NETERR;
+
+ buf.data()[nRead] = '\0';
+ result += wxString::FromAscii(buf);
+
+ if ( eol )
+ {
+ // remove trailing "\r\n"
+ result.RemoveLast(2);
- result += wxString::FromAscii( ch );
+ return wxPROTO_NOERR;
}
}