我在Objective C庫(對於iPhone)中出現非常奇怪的行爲。我想要做的是將數據包送入(離開配件端口),將它們追加到NSMutableData實例變量中,然後檢查是否有任何完整的數據包。如果我這樣做,[containsPacket]方法將返回數據包的長度,否則它將返回-1。如果包含數據包,則使用該數據創建一個新的RASPPacket對象,並用其餘數據更新我的receivedData對象(減去剛創建RASPPacket的數據)。我循環瀏覽,直到長度爲< 0(表示receivedData中沒有更多完整的數據包)。目標C對象行爲奇怪(使用舊引用)
當我給它一個完整的數據包時,它的工作方式應該如此(containsPacket返回正確的長度,並且receivedData成爲一個空的NSMutableData)。奇怪的是,我第二次調用containsPacket時,它仍然返回相同的數字(13),即使receivedData爲空。我在裏面放了一些NSLogs,看看發生了什麼事情(因爲我從計算機斷開連接,我無法逐步使用調試器),其輸出如下所示。
- (void)receive:(NSData *)packet {
[receivedData appendData:packet];
NSMutableString *dataString = [[NSMutableString alloc] init];
unsigned char *ptr = (unsigned char *)[receivedData bytes];
for(int i = 0; i < [receivedData length]; i++) {
[dataString appendFormat:@"%02X ", ptr[i]];
}
NSLog(@"Received data: %@", dataString);
int length = [RASPPacket containsPacket:receivedData];
NSLog(@"length is %i", length);
while(length > 0) {
RASPPacket *receivedPacket = [[RASPPacket alloc] initWithIncomingPacket:[NSData dataWithBytes:[receivedData bytes] length:length]];
[receivedData setData:[NSMutableData dataWithBytes:([receivedData bytes]+length) length:([receivedData length] - length)]];
dataString = [[NSMutableString alloc] init];
ptr = (unsigned char *)[receivedData bytes];
for(int i = 0; i < [receivedData length]; i++) {
[dataString appendFormat:@"%02X ", ptr[i]];
}
NSLog(@"new received data: %@", dataString);
if([receivedPacket isValidPacket]) {
[raspObject receive:receivedPacket];
NSLog(@"Complete packet is valid");
}
[receivedPacket release];
length = [RASPPacket containsPacket:receivedData];
NSLog(@"length is %i", length);
}
}
而且containsPacket方法:
+ (int)containsPacket:(NSData *)thePacket {
//Return -1 by default (if it doesn't contain a complete packet)
int returnValue = -1;
unsigned char *data = (unsigned char *)[thePacket bytes];
NSMutableString *dataString = [[NSMutableString alloc] init];
unsigned char *ptr = (unsigned char *)[thePacket bytes];
for(int i = 0; i < [thePacket length]; i++) {
[dataString appendFormat:@"%02X ", ptr[i]];
}
NSLog(@"containsPacket data: %@, len %i", dataString, [thePacket length]);
NSLog(@"beginning return value %i", returnValue);
//DLE counts the number of consecutive DLE bytes (0x10). An odd number means
//we found the DLE we want, an even means it's just in a sequence in the message data
int dleCount = 0;
NSLog(@"a return value %i", returnValue);
//Start i at 2 to skip 0x10 0x01
for(int i = 2; (i < ([thePacket length] - 1)) && returnValue < 0; i++) {
NSLog(@"b return value %i, i: %i, len: %i", returnValue, i, [thePacket length] - 1);
//Check if we found 0x10 and 0x03 and there are at least three bytes extra (0x03 and checksum)
if(data[i] == 0x10 && data[i+1] == 0x03 && [thePacket length] - 3 > i) {
int j = i - 1;
dleCount = 1;
while(data[j] == 0x10 && j >= 0) {
dleCount++;
j--;
}
if(dleCount % 2) {
//Add 1 to convert to indices to length, 1 to go from 0x10 to 0x03, and 2 for checksum
returnValue = i + 2 + 1 + 1;
NSLog(@"c return value %i", returnValue);
}
}
}
NSLog(@"ending return value %i", returnValue);
return returnValue;
}
最後,的NSLog輸出:
TestRaspLayer[922:6c03] Received data: 10 01 00 01 01 10 02 00 00 10 03 A1 07
TestRaspLayer[922:6c03] containsPacket data: 10 01 00 01 01 10 02 00 00 10 03 A1 07 , len 13
TestRaspLayer[922:6c03] beginning return value -1
TestRaspLayer[922:6c03] a return value -1
TestRaspLayer[922:6c03] b return value -1, i: 2, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 3, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 4, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 5, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 6, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 7, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 8, len: 12
TestRaspLayer[922:6c03] b return value -1, i: 9, len: 12
TestRaspLayer[922:6c03] c return value 13
TestRaspLayer[922:6c03] ending return value 13
TestRaspLayer[922:6c03] length is 13
TestRaspLayer[922:6c03] new received data:
TestRaspLayer[922:6c03] RASP receive, interface: 1, command 1, looking for interface: 9, command 1
TestRaspLayer[922:6c03] Complete packet is valid
TestRaspLayer[922:6c03] containsPacket data: , len 0
TestRaspLayer[922:6c03] beginning return value -1
TestRaspLayer[922:6c03] a return value -1
TestRaspLayer[922:6c03] b return value -1, i: 2, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 3, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 4, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 5, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 6, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 7, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 8, len: -1
TestRaspLayer[922:6c03] b return value -1, i: 9, len: -1
TestRaspLayer[922:6c03] c return value 13
TestRaspLayer[922:6c03] ending return value 13
TestRaspLayer[922:6c03] length is 13
第一次它是通過有效數據去,第二時間它不是」 t,並且不應該繼續通過for循環(因爲i> [thePacket length] -1)。