X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChat.cxx;h=7206401389c40cdacd48c740b04b8bc089e5918a;hb=bc9b3f6ff1fcc5caa67c07ad99f971c0faacf91a;hp=fcced48eca9abb0963e8c23e13faf7dec6c008f3;hpb=5b734d8c527c291cccf7c366fd4b3a741409fb82;p=simgear.git diff --git a/simgear/io/sg_netChat.cxx b/simgear/io/sg_netChat.cxx index fcced48e..72064013 100644 --- a/simgear/io/sg_netChat.cxx +++ b/simgear/io/sg_netChat.cxx @@ -26,14 +26,16 @@ #include #include // for strdup +#include namespace simgear { void NetChat::setTerminator (const char* t) { - if (terminator) delete[] terminator; + if (terminator) free(terminator); terminator = strdup(t); + bytesToCollect = -1; } const char* @@ -42,6 +44,15 @@ NetChat::getTerminator (void) return terminator; } + +void +NetChat::setByteCount(int count) +{ + if (terminator) free(terminator); + terminator = NULL; + bytesToCollect = count; +} + // return the size of the largest prefix of needle at the end // of haystack @@ -89,13 +100,23 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) // necessary because we might read several data+terminator combos // with a single recv(). - while (in_buffer.getLength()) { - + while (in_buffer.getLength()) { // special case where we're not using a terminator - if (terminator == 0 || *terminator == 0) { - collectIncomingData (in_buffer.getData(),in_buffer.getLength()); - in_buffer.remove (); - return; + if (terminator == 0 || *terminator == 0) { + if ( bytesToCollect > 0) { + const int toRead = std::min(in_buffer.getLength(), bytesToCollect); + collectIncomingData(in_buffer.getData(), toRead); + in_buffer.remove(0, toRead); + bytesToCollect -= toRead; + if (bytesToCollect == 0) { // read all requested bytes + foundTerminator(); + } + } else { // read the whole lot + collectIncomingData (in_buffer.getData(),in_buffer.getLength()); + in_buffer.remove (); + } + + continue; } int terminator_len = strlen(terminator);