0

前回の投稿について質問があります

しかし、最初はいくつかのコードを提供する必要があると思います。

作成したオブジェクトを処理する関数です

- (void)handleObject:(NSMutableData *)object {

NSLog(@"[object length] = %d", [object length]);
Byte *byteArray;
byteArray = (Byte *)[object bytes];

switch (byteArray[5]) {

    case 0: NSLog(@"Login OK"); 
        break;

    case 1: NSLog(@"Exit. Server disconnect");
        break;

    case 2: NSLog(@"Ping erhalten");
        break;

    case 4: NSLog(@"Points received");
        break;

    case 6: NSLog(@"transfer of POLYLINE vector objects");
        break;

    case 8: NSLog(@"transfer of POLYGON vector objects");
        break;

    case 9: NSLog(@"transfer of Render Rule");
        break;

    case 10: {

        NSLog(@"end of response for RequestVectorObjects");                                 
        isLoading = FALSE;
    }
        break;

    case 11: NSLog(@"Background Colorcode: %d", (byteArray[6] & 0xFF));
        break;    

    default: NSLog(@"From server: default value");
        break;
}

}

そして、ここで私はストリームからデータを受け取ります

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

switch(eventCode) {

    case NSStreamEventHasBytesAvailable:
    {
        isLoading = YES; 

        while (isLoading) {               

            uint8_t bufSize[4];            

            int packetLength = 0; 

            [(NSInputStream *)stream read:bufSize maxLength:4];
            packetLength = [self bytesToInt:bufSize withOffset:0];
            NSLog(@"packetLength: %d", packetLength);

            [tempStore appendBytes:bufSize length:4];

            if (packetLength == 25600) {

                loggedIn = YES;
                uint8_t buf[4];
                [(NSInputStream *)stream read:buf maxLength:4];
                [tempStore appendBytes:buf length:4];
                [self handleObject:tempStore];
            }
            else {

                uint8_t buf[packetLength];
                [(NSInputStream *)stream read:buf maxLength:packetLength];
                [tempStore appendBytes:buf length:packetLength];
                [self handleObject:tempStore];                    
            }
            [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
            [tempStore setLength:0];
        }           
        NSLog(@"Finished loading objects");                 

    } break;      

}

シミュレーターで、またはiPhoneでアプリケーションをデバッグする場合はすべて問題なく動作しますが、デバイスで実行しようとすると、packetLengthの値が間違っているため、常にBAD_EXCESSが発生します。そのため、バイトを読みすぎてしまいます。バッファ。

さて、私の質問です。デバッグモードではすべての値が正しいのに、実行モードでは完全に混乱するのはどうしてですか。その問題を回避するために私ができること、または私が知っておくべきことはありますか?

あなたの助けは大歓迎です

これが私のBytesToInt関数ですが、受け取った値が正しければ、正常に機能すると思います。

- (int)bytesToInt:(Byte *)b withOffset:(int)offset {

int ret = 0;
for (int i = 0; i < 4; i++) {

    ret |= (b[offset + i] & 0xFF) << (3-i)*8;
}    
return ret;

}

4

2 に答える 2

2

maxLength値に達するまで、受信したデータをループすることで、最終的にそれを実行しました。修正するのはそれほど難しいことではありませんでしたが、私はあまり賢く行動していませんでした...

さて、これが私がそれをした方法です:

isLoading = YES; 

        while (isLoading) {               

            uint8_t bufSize[4];            

            int packetLength , maxLength, len;

            maxLength = 4;
            len = [(NSInputStream *)stream read:bufSize maxLength:maxLength];
            NSLog(@"len = %d", len);
            packetLength = [self bytesToInt:bufSize withOffset:0];
            NSLog(@"packetLength: %d", packetLength);

            [tempStore appendBytes:bufSize length:maxLength];

            if (packetLength == 25600) {

                loggedIn = YES;
                maxLength = 4;
                uint8_t buf[maxLength];                    
                len = [(NSInputStream *)stream read:buf maxLength:maxLength];
                NSLog(@"len = %d", len);
                [tempStore appendBytes:buf length:maxLength];
            }
            else {
                maxLength = packetLength;
                BOOL allDataReceived = NO;

                while (!allDataReceived) {

                    uint8_t buf[maxLength];                    
                    len = [(NSInputStream *)stream read:buf maxLength:maxLength];                                                                    
                    NSLog(@"len = %d", len);                       

                    if (len < maxLength) {

                        maxLength -= len;
                        [tempStore appendBytes:buf length:len];
                    }
                    else {

                        allDataReceived = YES;
                        [tempStore appendBytes:buf length:len];
                    }                            
                }                    
            }
            [self handleObject:tempStore];
            [tempStore resetBytesInRange:NSMakeRange(0, [tempStore length])];
            [tempStore setLength:0];
        }
于 2012-03-16T09:39:16.960 に答える
1

の戻り値を確認してください[NSInputStream read]maxLengthを指定しても、実際にその数のバイトが実際に読み取られるという意味ではありません。ストリームが2バイトしか読み取らない場合は、バッファですでに読み取られているバイトの後ろを指すポインタを使用して2回目の読み取りを発行し、残りのバイトのmaxLengthを発行する必要があると思います。バッファ。

于 2012-03-15T15:59:25.133 に答える