Skip to content

Commit

Permalink
Fixes issue robbiehanson#53 - allow stopping the server but keeping c…
Browse files Browse the repository at this point in the history
…urrent connections alive
  • Loading branch information
robbiehanson committed Jun 4, 2011
1 parent 9079b6f commit 9e4ebe0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
29 changes: 28 additions & 1 deletion HTTPServer.h
Expand Up @@ -162,8 +162,35 @@
- (NSDictionary *)TXTRecordDictionary; - (NSDictionary *)TXTRecordDictionary;
- (void)setTXTRecordDictionary:(NSDictionary *)dict; - (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)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; - (BOOL)isRunning;


- (void)addWebSocket:(WebSocket *)ws; - (void)addWebSocket:(WebSocket *)ws;
Expand Down
42 changes: 24 additions & 18 deletions HTTPServer.m
Expand Up @@ -448,7 +448,12 @@ - (BOOL)start:(NSError **)errPtr
return success; return success;
} }


- (BOOL)stop - (void)stop
{
[self stop:NO];
}

- (void)stop:(BOOL)keepExistingConnections
{ {
HTTPLogTrace(); HTTPLogTrace();


Expand All @@ -462,28 +467,29 @@ - (BOOL)stop
[asyncSocket disconnect]; [asyncSocket disconnect];
isRunning = NO; isRunning = NO;


// Now stop all HTTP connections the server owns if (!keepExistingConnections)
[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)
{ {
[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]; [pool drain];
}); });

return YES;
} }


- (BOOL)isRunning - (BOOL)isRunning
Expand Down

0 comments on commit 9e4ebe0

Please sign in to comment.