Skip to content

Commit

Permalink
http: Properly stop server on stop server.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Jul 4, 2016
1 parent 429346b commit 287d196
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 7 additions & 3 deletions UI/RemoteISOScreen.cpp
Expand Up @@ -79,7 +79,7 @@ static void RegisterServer(int port) {
static void ExecuteServer() {
setCurrentThreadName("HTTPServer");

net::Init();
net::AutoInit netInit;
auto http = new http::Server(new threading::SameThreadExecutor());

std::map<std::string, std::string> paths;
Expand Down Expand Up @@ -156,7 +156,11 @@ static void ExecuteServer() {
}

if (!http->Listen(g_Config.iRemoteISOPort)) {
http->Listen(0);
if (!http->Listen(0)) {
ERROR_LOG(COMMON, "Unable to listen on any port");
UpdateStatus(ServerStatus::STOPPED);
return;
}
}
UpdateStatus(ServerStatus::RUNNING);

Expand All @@ -173,7 +177,7 @@ static void ExecuteServer() {
}
}

net::Shutdown();
http->Stop();

UpdateStatus(ServerStatus::STOPPED);
}
Expand Down
12 changes: 11 additions & 1 deletion ext/native/net/http_server.cpp
Expand Up @@ -162,6 +162,10 @@ bool Server::Listen(int port) {
}

bool Server::RunSlice(double timeout) {
if (listener_ < 0 || port_ == 0) {
return false;
}

if (timeout <= 0.0) {
timeout = 86400.0;
}
Expand All @@ -182,7 +186,9 @@ bool Server::RunSlice(double timeout) {
}

bool Server::Run(int port) {
Listen(port);
if (!Listen(port)) {
return false;
}

while (true) {
RunSlice(0.0);
Expand All @@ -192,6 +198,10 @@ bool Server::Run(int port) {
return true;
}

void Server::Stop() {
closesocket(listener_);
}

void Server::HandleConnection(int conn_fd) {
Request request(conn_fd);
if (!request.IsOK()) {
Expand Down
1 change: 1 addition & 0 deletions ext/native/net/http_server.h
Expand Up @@ -74,6 +74,7 @@ class Server {
// for a new connection to handle.
bool RunSlice(double timeout);
bool Listen(int port);
void Stop();

void RegisterHandler(const char *url_path, UrlHandlerFunc handler);
void SetFallbackHandler(UrlHandlerFunc handler);
Expand Down

0 comments on commit 287d196

Please sign in to comment.