]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_netChat.cxx
Fix Linux compilation of netChat - explicit include of malloc.h required.
[simgear.git] / simgear / io / sg_netChat.cxx
index fcced48eca9abb0963e8c23e13faf7dec6c008f3..ee3c992a16f81dc37e3af3252b3b24f4b8f4756b 100644 (file)
 #include <simgear/io/sg_netChat.hxx>
 
 #include <cstring> // for strdup
+#include <malloc.h>
 
 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,12 +100,22 @@ 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 ();
+    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 ();
+        }
+      
       return;
     }