Skip to content

Commit 85cf17f

Browse files
Fix weird apple foo creating a second (unusable) framer
1 parent fa72472 commit 85cf17f

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

Monal/Classes/MLStream.m

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -470,59 +470,48 @@ +(void) connectWithSNIDomain:(NSString*) SNIDomain connectHost:(NSString*) host
470470
return YES;
471471
});
472472

473-
/*
474473
//some weird apple stuff creates the framer twice: once directly when starting the tcp handshake
475-
//and again later after the tcp connection was established successfully --> ignore the first one
474+
//and once a few milliseconds later, presumably after the tcp connection was established successfully
475+
//--> ignore all but the first one
476476
if(framerId < 1)
477477
{
478-
nw_framer_set_input_handler(framer, ^size_t(nw_framer_t framer) {
479-
nw_framer_parse_input(framer, 1, BUFFER_SIZE, nil, ^size_t(uint8_t* buffer, size_t buffer_length, bool is_complete) {
480-
MLAssert(NO, @"Unexpected incoming bytes in first framer!", (@{
481-
@"logtag": nilWrapper(logtag),
482-
@"framer": framer,
483-
@"buffer": [NSData dataWithBytes:buffer length:buffer_length],
484-
@"buffer_length": @(buffer_length),
485-
@"is_complete": bool2str(is_complete),
486-
}));
487-
return buffer_length;
478+
DDLogVerbose(@"Framer is the first one, using it...");
479+
//we have to simulate nw_connection_state_ready because the connection state will not reflect that while our framer is active
480+
//--> use framer start as "connection active" signal
481+
//first framer start is allowed to directly send data which will be used as tcp early data
482+
if(!wasOpenOnce)
483+
{
484+
wasOpenOnce = YES;
485+
@synchronized(shared_state) {
486+
shared_state.open = YES;
487+
}
488+
//make sure to not do this inside the framer thread to not cause any deadlocks
489+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
490+
[input generateEvent:NSStreamEventOpenCompleted];
491+
[output generateEvent:NSStreamEventOpenCompleted];
488492
});
489-
return 0; //why that?
493+
}
494+
495+
nw_framer_set_input_handler(framer, ^size_t(nw_framer_t framer) {
496+
DDLogDebug(@"Got new input for framer: %@", framer);
497+
[input schedule_read];
498+
return 0; //why that??
490499
});
491500
nw_framer_set_output_handler(framer, ^(nw_framer_t framer, nw_framer_message_t message, size_t message_length, bool is_complete) {
492-
MLAssert(NO, @"Unexpected outgoing bytes in first framer!", (@{
501+
MLAssert(NO, @"Unexpected outgoing bytes in framer!", (@{
493502
@"logtag": nilWrapper(logtag),
494503
@"framer": framer,
495504
@"message": message,
496505
@"message_length": @(message_length),
497506
@"is_complete": bool2str(is_complete),
498507
}));
499508
});
500-
return nw_framer_start_result_will_mark_ready;
501-
}
502-
*/
503-
504-
//we have to simulate nw_connection_state_ready because the connection state will not reflect that while our framer is active
505-
//--> use framer start as "connection active" signal
506-
//first framer start is allowed to directly send data which will be used as tcp early data
507-
if(!wasOpenOnce)
508-
{
509-
wasOpenOnce = YES;
510-
@synchronized(shared_state) {
511-
shared_state.open = YES;
512-
}
513-
//make sure to not do this inside the framer thread to not cause any deadlocks
514-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
515-
[input generateEvent:NSStreamEventOpenCompleted];
516-
[output generateEvent:NSStreamEventOpenCompleted];
517-
});
509+
510+
shared_state.framer = framer;
518511
}
512+
else
513+
DDLogVerbose(@"Ignoring subsequent framer...");
519514

520-
nw_framer_set_input_handler(framer, ^size_t(nw_framer_t framer) {
521-
[input schedule_read];
522-
return 0; //why that??
523-
});
524-
525-
shared_state.framer = framer;
526515
return nw_framer_start_result_will_mark_ready;
527516
});
528517
DDLogInfo(@"%@: Not doing direct TLS: appending framer to protocol stack...", logtag);

0 commit comments

Comments
 (0)