From d0a7bb377f23b50c411ac202b3fcf28f744e5361 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 2 Aug 2007 20:59:12 -0700 Subject: [PATCH] Improving the efficiency of [SocketWrapper receive] * Source/SocketWrapper.m: ([SocketWrapper receive]): Cutting down the size of packetLength[], use memmove() instead of strcpy() for moving part of buffer[] to packet[], and autorelease the string we send to the delegate. Also, no longer rely on sizeof() in checking for partial packets because we now have i. --- Source/SocketWrapper.m | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 7630022..16bfaec 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -133,24 +133,28 @@ NSString *SocketWrapperDataSentNotification = @"dataSent"; NSMutableData *data = [NSMutableData data]; // strip the length from the packet, and clear the null byte then add it to the NSData - char packetLength[32]; + char packetLength[8]; int i = 0; - do + while (buffer[i] != '\0') { packetLength[i] = buffer[i]; i++; - } while (buffer[i] != '\0'); + } + + // we also want the null byte, so move us up 1 + i++; + // the length of the packet // packet is formatted in lenpacket int length = atoi(packetLength); // take our bytes and convert them to NSData char packet[sizeof(buffer)]; - strcpy(packet, &buffer[i + 1]); + memmove(packet, &buffer[i], recvd - i); [data appendBytes: packet length: recvd]; // check if we have a partial packet - if (length + sizeof(length) > sizeof(buffer)) + if (length + i > sizeof(buffer)) { while (recvd < length) { @@ -165,11 +169,7 @@ NSString *SocketWrapperDataSentNotification = @"dataSent"; } // convert the NSData into a NSString - NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; - - [_delegate dataReceived: string]; - - //return string; + [_delegate dataReceived: [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]]; } /** -- 2.22.5