Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/GCDWebServer/Core/GCDWebServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,23 @@ - (void)_willEnterForeground:(NSNotification *)notification {
}
}

- (void)_reconnectInForeground:(NSNotification *)notification {
GWS_DCHECK([NSThread isMainThread]);
GWS_LOG_DEBUG(@"Will enter foreground (not suspending in background)");

// When not suspending in the background we keep serving, but iOS may still
// tear down our listening sockets once the process is actually suspended.
// If we were running, rebuild them from scratch when returning to the
// foreground so the server keeps accepting connections. -_start: reuses the
// previously assigned port, so client URLs stay valid. See
// swisspol/GCDWebServer#292. Existing (already-accepted) connections are
// unaffected — only the listening sockets are rebuilt.
if (_source4) {
[self _stop];
[self _start:NULL]; // TODO: There's probably nothing we can do on failure
}
}

#endif

- (BOOL)startWithOptions:(NSDictionary<NSString *, id> *)options error:(NSError **)error {
Expand All @@ -861,6 +878,8 @@ - (BOOL)startWithOptions:(NSDictionary<NSString *, id> *)options error:(NSError
if (_suspendInBackground) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_reconnectInForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

#endif
Expand Down Expand Up @@ -890,6 +909,8 @@ - (void)stop {
if (_suspendInBackground) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

#endif
Expand Down