Skip to content

Commit dd2508d

Browse files
committed
Minor optimizations and improved logging
1 parent 74f670a commit dd2508d

File tree

1 file changed

+55
-41
lines changed

1 file changed

+55
-41
lines changed

GCD/GCDAsyncSocket.m

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
12741274
NSString *reason = @"Error enabling non-blocking IO on socket (fcntl)";
12751275
err = [self errnoErrorWithReason:reason];
12761276

1277+
LogVerbose(@"close(socketFD)");
12771278
close(socketFD);
12781279
return SOCKET_NULL;
12791280
}
@@ -1285,6 +1286,7 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
12851286
NSString *reason = @"Error enabling address reuse (setsockopt)";
12861287
err = [self errnoErrorWithReason:reason];
12871288

1289+
LogVerbose(@"close(socketFD)");
12881290
close(socketFD);
12891291
return SOCKET_NULL;
12901292
}
@@ -1297,6 +1299,7 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
12971299
NSString *reason = @"Error in bind() function";
12981300
err = [self errnoErrorWithReason:reason];
12991301

1302+
LogVerbose(@"close(socketFD)");
13001303
close(socketFD);
13011304
return SOCKET_NULL;
13021305
}
@@ -1309,6 +1312,7 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
13091312
NSString *reason = @"Error in listen() function";
13101313
err = [self errnoErrorWithReason:reason];
13111314

1315+
LogVerbose(@"close(socketFD)");
13121316
close(socketFD);
13131317
return SOCKET_NULL;
13141318
}
@@ -1425,6 +1429,7 @@ - (BOOL)acceptOnInterface:(NSString *)inInterface port:(uint16_t)port error:(NSE
14251429
{
14261430
if (socket4FD != SOCKET_NULL)
14271431
{
1432+
LogVerbose(@"close(socket4FD)");
14281433
close(socket4FD);
14291434
}
14301435

@@ -2362,7 +2367,6 @@ - (void)didNotConnect:(int)aConnectIndex error:(NSError *)error
23622367
return;
23632368
}
23642369

2365-
[self endConnectTimeout];
23662370
[self closeWithError:error];
23672371
}
23682372

@@ -2495,62 +2499,72 @@ - (void)closeWithError:(NSError *)error
24952499
// So we have to unpause the source if needed.
24962500
// This allows the cancel handler to be run, which in turn releases the source and closes the socket.
24972501

2498-
if (accept4Source)
2499-
{
2500-
LogVerbose(@"dispatch_source_cancel(accept4Source)");
2501-
dispatch_source_cancel(accept4Source);
2502-
2503-
// We never suspend accept4Source
2504-
2505-
accept4Source = NULL;
2506-
}
2507-
2508-
if (accept6Source)
2502+
if (!accept4Source && !accept6Source && !readSource && !writeSource)
25092503
{
2510-
LogVerbose(@"dispatch_source_cancel(accept6Source)");
2511-
dispatch_source_cancel(accept6Source);
2512-
2513-
// We never suspend accept6Source
2514-
2515-
accept6Source = NULL;
2516-
}
2517-
if (!readSource && !writeSource) {
25182504
LogVerbose(@"manually closing close");
25192505

2520-
if (socket4FD) {
2506+
if (socket4FD != SOCKET_NULL)
2507+
{
2508+
LogVerbose(@"close(socket4FD)");
25212509
close(socket4FD);
2510+
socket4FD = SOCKET_NULL;
25222511
}
25232512

2524-
if (socket6FD) {
2513+
if (socket6FD != SOCKET_NULL)
2514+
{
2515+
LogVerbose(@"close(socket6FD)");
25252516
close(socket6FD);
2517+
socket6FD = SOCKET_NULL;
25262518
}
25272519
}
2528-
2529-
if (readSource)
2520+
else
25302521
{
2531-
LogVerbose(@"dispatch_source_cancel(readSource)");
2532-
dispatch_source_cancel(readSource);
2533-
2534-
[self resumeReadSource];
2522+
if (accept4Source)
2523+
{
2524+
LogVerbose(@"dispatch_source_cancel(accept4Source)");
2525+
dispatch_source_cancel(accept4Source);
2526+
2527+
// We never suspend accept4Source
2528+
2529+
accept4Source = NULL;
2530+
}
25352531

2536-
readSource = NULL;
2537-
}
2532+
if (accept6Source)
2533+
{
2534+
LogVerbose(@"dispatch_source_cancel(accept6Source)");
2535+
dispatch_source_cancel(accept6Source);
2536+
2537+
// We never suspend accept6Source
2538+
2539+
accept6Source = NULL;
2540+
}
25382541

2539-
if (writeSource)
2540-
{
2541-
LogVerbose(@"dispatch_source_cancel(writeSource)");
2542-
dispatch_source_cancel(writeSource);
2542+
if (readSource)
2543+
{
2544+
LogVerbose(@"dispatch_source_cancel(readSource)");
2545+
dispatch_source_cancel(readSource);
2546+
2547+
[self resumeReadSource];
2548+
2549+
readSource = NULL;
2550+
}
25432551

2544-
[self resumeWriteSource];
2552+
if (writeSource)
2553+
{
2554+
LogVerbose(@"dispatch_source_cancel(writeSource)");
2555+
dispatch_source_cancel(writeSource);
2556+
2557+
[self resumeWriteSource];
2558+
2559+
writeSource = NULL;
2560+
}
25452561

2546-
writeSource = NULL;
2562+
// The sockets will be closed by the cancel handlers of the corresponding source
2563+
2564+
socket4FD = SOCKET_NULL;
2565+
socket6FD = SOCKET_NULL;
25472566
}
25482567

2549-
// The sockets will be closed by the cancel handlers of the corresponding source
2550-
2551-
socket4FD = SOCKET_NULL;
2552-
socket6FD = SOCKET_NULL;
2553-
25542568
// If the client has passed the connect/accept method, then the connection has at least begun.
25552569
// Notify delegate that it is now ending.
25562570
BOOL shouldCallDelegate = (flags & kSocketStarted);

0 commit comments

Comments
 (0)