Permalink
Browse files

Stopping server nicely

  • Loading branch information...
1 parent c7f4ea6 commit 0bfcb3642fddb5621d9a8d5c007b53409cdfaea1 @tadzik committed Jun 6, 2012
Showing with 20 additions and 7 deletions.
  1. +3 −2 app.cpp
  2. +8 −0 csgi.cpp
  3. +9 −5 csgi.hpp
View
@@ -52,10 +52,11 @@ int main()
std::cerr << e.what() << std::endl;
return 1;
}
- for (;;) {
+ for (int i = 0; i < 10; i++) {
sleep(1);
std::cerr << "counter is now " << app->increment() << std::endl;
}
- std::cerr << "Server running on port 8080" << std::endl;
+ std::cerr << "App cleaning up" << std::endl;
+ delete app;
return 0;
}
View
@@ -1,5 +1,7 @@
#include "csgi.hpp"
+bool can_read(int fd);
+
void *run_thread(void *arg)
{
CSGI::Server *srv = (CSGI::Server *)arg;
@@ -75,7 +77,13 @@ void CSGI::Server::serve()
int newfd;
for (;;) {
+ if (stop_) {
+ //std::cerr << "serve() stopping" << std::endl;
+ return;
+ }
SSL *ssl = SSL_new(ssl_ctx_);
+ // so we check stop_
+ if (!can_read(sockfd_)) continue;
newfd = accept(sockfd_, 0, 0);
SSL_set_fd(ssl, newfd);
int err = SSL_accept(ssl);
View
@@ -111,15 +111,18 @@ class Server {
}
worker_ = NULL;
+ stop_ = 0;
}
~Server()
{
- std::cerr << "Cleaning up\n" << std::endl;
- //FIXME
- //if (pid_ != -1) {
- // kill(pid_, SIGTERM);
- //}
+ //std::cerr << "Cleaning up" << std::endl;
+ if (worker_ != NULL) {
+ stop_ = 1;
+ pthread_join(*worker_, NULL);
+ free(worker_);
+ //std::cerr << "Child returned" << std::endl;
+ }
close(sockfd_);
freeaddrinfo(res_);
SSL_CTX_free(ssl_ctx_);
@@ -141,6 +144,7 @@ class Server {
int sockfd_;
int port_;
int backlog_;
+ int stop_;
pthread_t *worker_;
SSL_METHOD *ssl_method_;

0 comments on commit 0bfcb36

Please sign in to comment.