Permalink
Browse files

Fixes issue #53 - allow stopping the server but keeping current conne…

…ctions alive
  • Loading branch information...
1 parent 9079b6f commit 9e4ebe074b181c76d55d81d592fcfa1f0a33aeaf @robbiehanson robbiehanson committed Jun 4, 2011
Showing with 52 additions and 19 deletions.
  1. +28 −1 HTTPServer.h
  2. +24 −18 HTTPServer.m
View
@@ -162,8 +162,35 @@
- (NSDictionary *)TXTRecordDictionary;
- (void)setTXTRecordDictionary:(NSDictionary *)dict;
+/**
+ * Attempts to starts the server on the configured port, interface, etc.
+ *
+ * If an error occurs, this method returns NO and sets the errPtr (if given).
+ * Otherwise returns YES on success.
+ *
+ * Some examples of errors that might occur:
+ * - You specified the server listen on a port which is already in use by another application.
+ * - You specified the server listen on a port number below 1024, which requires root priviledges.
+ *
+ * Code Example:
+ *
+ * NSError *err = nil;
+ * if (![httpServer start:&err])
+ * {
+ * NSLog(@"Error starting http server: %@", err);
+ * }
+**/
- (BOOL)start:(NSError **)errPtr;
-- (BOOL)stop;
+
+/**
+ * Stops the server, preventing it from accepting any new connections.
+ * You may specify whether or not you want to close the existing client connections.
+ *
+ * The default stop method (with no arguments) will close any existing connections. (It invokes [self stop:NO])
+**/
+- (void)stop;
+- (void)stop:(BOOL)keepExistingConnections;
+
- (BOOL)isRunning;
- (void)addWebSocket:(WebSocket *)ws;
View
@@ -448,7 +448,12 @@ - (BOOL)start:(NSError **)errPtr
return success;
}
-- (BOOL)stop
+- (void)stop
+{
+ [self stop:NO];
+}
+
+- (void)stop:(BOOL)keepExistingConnections
{
HTTPLogTrace();
@@ -462,28 +467,29 @@ - (BOOL)stop
[asyncSocket disconnect];
isRunning = NO;
- // Now stop all HTTP connections the server owns
- [connectionsLock lock];
- for (HTTPConnection *connection in connections)
- {
- [connection stop];
- }
- [connections removeAllObjects];
- [connectionsLock unlock];
-
- // Now stop all WebSocket connections the server owns
- [webSocketsLock lock];
- for (WebSocket *webSocket in webSockets)
+ if (!keepExistingConnections)
{
- [webSocket stop];
+ // Stop all HTTP connections the server owns
+ [connectionsLock lock];
+ for (HTTPConnection *connection in connections)
+ {
+ [connection stop];
+ }
+ [connections removeAllObjects];
+ [connectionsLock unlock];
+
+ // Stop all WebSocket connections the server owns
+ [webSocketsLock lock];
+ for (WebSocket *webSocket in webSockets)
+ {
+ [webSocket stop];
+ }
+ [webSockets removeAllObjects];
+ [webSocketsLock unlock];
}
- [webSockets removeAllObjects];
- [webSocketsLock unlock];
[pool drain];
});
-
- return YES;
}
- (BOOL)isRunning

0 comments on commit 9e4ebe0

Please sign in to comment.