Private
Public Access
1
0

pingpong prevent superclass from handling rest of ping frame

This commit is contained in:
2025-06-13 14:50:13 -07:00
parent e1ec237053
commit d706435103

View File

@@ -126,6 +126,7 @@
payload = [unmaskedPayload copy]; payload = [unmaskedPayload copy];
} }
NSLog(@"Sending pong response to ping with payload length: %lu", (unsigned long)[payload length]);
[self sendPongWithPayload:payload socket:sock]; [self sendPongWithPayload:payload socket:sock];
// Continue reading the next frame // Continue reading the next frame
@@ -133,23 +134,22 @@
return; return;
} }
// Intercept after masking key is read for ping/pong frames // Let parent handle TAG_PAYLOAD_PREFIX first to set nextOpCode
if (tag == TAG_MSG_MASKING_KEY && (nextOpCode == WS_OP_PING || nextOpCode == WS_OP_PONG)) { if (tag == TAG_PAYLOAD_PREFIX) {
// Store the masking key [super socket:sock didReadData:data withTag:tag];
maskingKey = [data copy];
// We need to determine the payload length and read it
// The parent class should have stored this somewhere, but we'll need to track it
// For now, let's assume short payload and read what was scheduled
return; return;
} }
// Intercept the payload length phase for ping/pong frames // Now intercept ping/pong handling after nextOpCode is set
if (tag == TAG_PAYLOAD_LENGTH && (nextOpCode == WS_OP_PING || nextOpCode == WS_OP_PONG)) { if (nextOpCode == WS_OP_PING || nextOpCode == WS_OP_PONG) {
if (tag == TAG_PAYLOAD_LENGTH) {
UInt8 frame = *(UInt8 *)[data bytes]; UInt8 frame = *(UInt8 *)[data bytes];
BOOL masked = (frame & 0x80) != 0; BOOL masked = (frame & 0x80) != 0;
NSUInteger length = frame & 0x7F; NSUInteger length = frame & 0x7F;
nextFrameMasked = masked; nextFrameMasked = masked;
NSLog(@"Processing ping/pong frame, masked: %d, length: %lu", masked, (unsigned long)length);
if (length <= 125) { if (length <= 125) {
if (nextFrameMasked) { if (nextFrameMasked) {
[asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY]; [asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY];
@@ -165,18 +165,17 @@
return; return;
} }
// Handle extended length for ping/pong if (tag == TAG_MSG_MASKING_KEY) {
if ((tag == TAG_PAYLOAD_LENGTH16 || tag == TAG_PAYLOAD_LENGTH64) && (nextOpCode == WS_OP_PING || nextOpCode == WS_OP_PONG)) { // Store the masking key
NSUInteger length = 0; maskingKey = [data copy];
if (tag == TAG_PAYLOAD_LENGTH16) { NSLog(@"Stored masking key for ping/pong frame");
UInt8 *pFrame = (UInt8 *)[data bytes];
length = ((NSUInteger)pFrame[0] << 8) | (NSUInteger)pFrame[1];
} else {
// For 64-bit length, this is complex - for now just close
[self didClose];
return; return;
} }
if (tag == TAG_PAYLOAD_LENGTH16) {
UInt8 *pFrame = (UInt8 *)[data bytes];
NSUInteger length = ((NSUInteger)pFrame[0] << 8) | (NSUInteger)pFrame[1];
if (nextFrameMasked) { if (nextFrameMasked) {
[asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY]; [asyncSocket readDataToLength:4 withTimeout:TIMEOUT_NONE tag:TAG_MSG_MASKING_KEY];
} }
@@ -184,9 +183,15 @@
return; return;
} }
if (tag == TAG_PAYLOAD_LENGTH64) {
// For 64-bit length, this is too complex for ping frames - just close
[self didClose];
return;
}
}
// For all other cases, call the parent implementation // For all other cases, call the parent implementation
[super socket:sock didReadData:data withTag:tag]; [super socket:sock didReadData:data withTag:tag];
} }