diff --git a/kordophone/Bridge/MBIMPingPongWebSocket.m b/kordophone/Bridge/MBIMPingPongWebSocket.m index 02004c3..244fe65 100644 --- a/kordophone/Bridge/MBIMPingPongWebSocket.m +++ b/kordophone/Bridge/MBIMPingPongWebSocket.m @@ -43,7 +43,7 @@ @implementation MBIMPingPongWebSocket { - NSData *pendingPingPayload; + NSUInteger currentPayloadLength; } #pragma mark - Ping/Pong Frame Construction @@ -90,7 +90,7 @@ return [frame copy]; } -- (void)sendPongWithPayload:(NSData *)payload socket:(GCDAsyncSocket *)asyncSocket { +- (void)sendPongWithPayload:(NSData *)payload { NSData *pongFrame = [self createPongFrameWithPayload:payload]; // Send the pong frame directly through the socket @@ -101,12 +101,12 @@ #pragma mark - Override AsyncSocket Delegate -- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { +- (void)socket:(GCDAsyncSocket *)asyncSocket didReadData:(NSData *)data withTag:(long)tag { // xxx: sheesh: really need to get off of CococaHTTPServer. NSData *maskingKey = [self valueForKey:@"maskingKey"]; BOOL nextFrameMasked = [[self valueForKey:@"nextFrameMasked"] boolValue]; NSUInteger nextOpCode = [[self valueForKey:@"nextOpCode"] unsignedIntValue]; - + NSLog(@"tag: %ld, nextOpCode: %lu", tag, (unsigned long)nextOpCode); // Handle our custom ping payload tag @@ -127,7 +127,7 @@ } NSLog(@"Sending pong response to ping with payload length: %lu", (unsigned long)[payload length]); - [self sendPongWithPayload:payload socket:sock]; + [self sendPongWithPayload:payload]; // Continue reading the next frame [asyncSocket readDataToLength:1 withTimeout:TIMEOUT_NONE tag:TAG_PAYLOAD_PREFIX]; @@ -136,7 +136,7 @@ // Let parent handle TAG_PAYLOAD_PREFIX first to set nextOpCode if (tag == TAG_PAYLOAD_PREFIX) { - [super socket:sock didReadData:data withTag:tag]; + [super socket:asyncSocket didReadData:data withTag:tag]; return; } @@ -147,14 +147,21 @@ BOOL masked = (frame & 0x80) != 0; NSUInteger length = frame & 0x7F; nextFrameMasked = masked; + currentPayloadLength = length; NSLog(@"Processing ping/pong frame, masked: %d, length: %lu", masked, (unsigned long)length); if (length <= 125) { if (nextFrameMasked) { [asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY]; + } else if (length > 0) { + [asyncSocket readDataToLength:length withTimeout:TIMEOUT_NONE tag:TAG_PING_PAYLOAD]; + } else { + // Empty payload, no masking - handle immediately + NSLog(@"Handling empty unmasked ping payload"); + [self sendPongWithPayload:[NSData data]]; + [asyncSocket readDataToLength:1 withTimeout:TIMEOUT_NONE tag:TAG_PAYLOAD_PREFIX]; } - [asyncSocket readDataToLength:length withTimeout:TIMEOUT_NONE tag:TAG_PING_PAYLOAD]; } else if (length == 126) { [asyncSocket readDataToLength:2 withTimeout:TIMEOUT_NONE tag:TAG_PAYLOAD_LENGTH16]; @@ -169,6 +176,16 @@ // Store the masking key maskingKey = [data copy]; NSLog(@"Stored masking key for ping/pong frame"); + + // Now read the payload (or handle empty payload) + if (currentPayloadLength > 0) { + [asyncSocket readDataToLength:currentPayloadLength withTimeout:TIMEOUT_NONE tag:TAG_PING_PAYLOAD]; + } else { + // Empty payload - send pong immediately + NSLog(@"Sending pong for empty masked ping payload"); + [self sendPongWithPayload:[NSData data]]; + [asyncSocket readDataToLength:1 withTimeout:TIMEOUT_NONE tag:TAG_PAYLOAD_PREFIX]; + } return; } @@ -191,10 +208,9 @@ } // For all other cases, call the parent implementation - [super socket:sock didReadData:data withTag:tag]; + [super socket:asyncSocket didReadData:data withTag:tag]; } - #pragma mark - Helper Methods // Expose the isValidWebSocketFrame method since it's private in the parent @@ -207,22 +223,4 @@ return YES; } -// Override didReceiveMessage to add logging -- (void)didReceiveMessage:(NSString *)msg { - NSLog(@"Received message: %@", msg); - [super didReceiveMessage:msg]; -} - -// Override didOpen to add logging -- (void)didOpen { - NSLog(@"WebSocket opened with ping/pong support"); - [super didOpen]; -} - -- (void)didClose { - NSLog(@"WebSocket closed"); - [super didClose]; -} - @end -