#include <simgear/io/sg_netChat.hxx>
#include <cstring> // for strdup
+#include <cstdlib>
namespace simgear {
void
NetChat::setTerminator (const char* t)
{
- if (terminator) delete[] terminator;
+ if (terminator) free(terminator);
terminator = strdup(t);
+ bytesToCollect = -1;
}
const char*
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
// 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);