Revert "Rewrote -[SocketWrapper receive] so that we do not get any more partial/broke...
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 20 May 2009 03:47:21 +0000 (23:47 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 20 May 2009 03:47:21 +0000 (23:47 -0400)
This reverts commit d4d5c4a90a8e7a421576aaf906f95894a6903074.

CHANGES
Source/SocketWrapper.m

diff --git a/CHANGES b/CHANGES
index 9f4a9c70e0af9bf9599aa0bab8d75997aca7b30b..e4ba9b348882d4e01c15f2f165fb1bcdeac7ff64 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -15,7 +15,6 @@ a subsequent time
 - Fix: #157  After using the run command, the stack will now be properly updated
 - New: A preference has been added to disable automatic stepping into the
 first line of execution
-- Fix: MacGDBp should no longer throw incomplete packet error messages
 
 
 1.2.1
index f93d2f13d886f7c4d248f0aae6581fe405e6dcaa..bb91fcfde3c01eef6ba8753fcd451478a2df73ea 100644 (file)
  */
 - (NSString*)receive
 {
-       // Read the first part of the response, the length of the packet.
+       // create a buffer
+       char buffer[1024];
+       
+       // do our initial recv() call to get (hopefully) all the data and the lengh of the packet
+       int recvd = recv(sock, &buffer, sizeof(buffer), 0);
+       
+       // take the received data and put it into an NSData
+       NSMutableString* str = [NSMutableString string];
+       
+       if (recvd == -1)
+               return nil;
+       
+       // strip the length from the packet, and clear the null byte then add it to the NSData
        char packetLength[8];
-       memset(&packetLength, 0x0, 8);
-       char c;
+       memset(packetLength, '\0', sizeof(packetLength));
        int i = 0;
-       while (recv(sock, &c, 1, 0) == 1 && c != 0x0)
-               packetLength[i++] = c;
-       int length = atoi(packetLength);
+       while (buffer[i] != '\0')
+       {
+               packetLength[i] = buffer[i];
+               i++;
+       }
        
-       // Our final output.
-       NSMutableString* string = [[NSMutableString alloc] initWithCapacity:length];
+       // we also want the null byte, so move us up 1
+       i++;
        
-       // Create a buffer that we will move data from the network into.
-       char buffer[1024];
+       // the total length of the full transmission
+       int length = atoi(packetLength);
        
-       // The total amount of data we have currently read.
-       int received = 0;
+       // move the packet part of the received data into it's own char[]
+       char packet[sizeof(buffer)];
+       memset(packet, '\0', sizeof(packet));
+       memmove(packet, &buffer[i], recvd - i);
        
-       // Loop until we have the entire packet.
-       while (received < length)
+       // convert bytes to NSString
+       [str appendString:[NSString stringWithCString:packet length:recvd - i]];
+       
+       // check if we have a partial packet
+       if (length + i > sizeof(buffer))
        {
-               int size = recv(sock, &buffer, sizeof(buffer), 0);
-               if (size < 1)
+               while (recvd < length + i)
                {
-                       [self error:@"Socket closed or could not be read"];
-                       return nil;
+                       int latest = recv(sock, &buffer, sizeof(buffer), 0);
+                       if (latest < 1)
+                       {
+                               [self error:@"Socket closed or could not be read"];
+                               return nil;
+                       }
+                       [str appendString:[NSString stringWithCString:buffer length:latest]];
+                       recvd += latest;
                }
-               NSString* temp = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];
-               [string appendString:temp];
-               received += [temp length];
        }
        
-       return [string autorelease];
+       NSString* tmp = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // strip whitespace
+       tmp = [tmp substringToIndex:[tmp length] - 1]; // don't want the null byte
+       
+       NSAssert([tmp UTF8String][[tmp length] - 1] == '>', @"-[SocketWrapper receive] buffer is incomplete");
+       
+       return tmp;
 }
 
 /**