Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Threads

  • Loading branch information...
commit 15a1214c6e39c1c355c295f2f6a0a53ec7af6e6c 1 parent 357329b
@tadzik authored
Showing with 33 additions and 13 deletions.
  1. +1 −1  Makefile
  2. +15 −2 app.cpp
  3. +9 −4 csgi.cpp
  4. +8 −6 csgi.hpp
View
2  Makefile
@@ -1,6 +1,6 @@
CXX?=clang++
CFLAGS=-Wall -Wextra -pedantic -ggdb
-LDFLAGS=-lssl -lcrypto
+LDFLAGS=-lssl -lcrypto -lpthread
COMPILE= $(CXX) $(CFLAGS)
all: app skunk_test
View
17 app.cpp
@@ -2,7 +2,12 @@
#include <iostream>
class MyApp : public CSGI::Application {
+ int counter_;
public:
+ MyApp() : counter_(0) { }
+
+ int increment() { return ++counter_; }
+
virtual CSGI::Response operator()(CSGI::Env& env)
{
CSGI::Response resp;
@@ -21,6 +26,10 @@ class MyApp : public CSGI::Application {
}
resp.content = "Hello, world!";
+ resp.content.append(" Counter = ");
+ std::stringstream counter;
+ counter << counter_;
+ resp.content.append(counter.str());
std::stringstream len;
len << resp.content.length();
@@ -35,14 +44,18 @@ class MyApp : public CSGI::Application {
int main()
{
- CSGI::Application *app = new MyApp;
+ MyApp *app = new MyApp;
CSGI::Server srv(app, 8080);
try {
- srv.run(false);
+ srv.run(true);
} catch (CSGI::Exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
+ for (;;) {
+ sleep(1);
+ std::cerr << "counter is now " << app->increment() << std::endl;
+ }
std::cerr << "Server running on port 8080" << std::endl;
return 0;
}
View
13 csgi.cpp
@@ -1,5 +1,12 @@
#include "csgi.hpp"
+void *run_thread(void *arg)
+{
+ CSGI::Server *srv = (CSGI::Server *)arg;
+ srv->serve();
+ return NULL;
+}
+
void CSGI::Server::run(bool async)
{
int i;
@@ -53,7 +60,8 @@ void CSGI::Server::run(bool async)
throw CSGI::Exception(err);
}
if (async) {
- if ((pid_ = fork()) == 0) serve();
+ worker_ = (pthread_t *)malloc(sizeof(pthread_t));
+ pthread_create(worker_, NULL, run_thread, (void*)this);
} else {
serve();
}
@@ -98,9 +106,6 @@ void CSGI::Server::serve()
}
bool can_read(int fd) {
- if (fd < 0) {
- std::cerr << "lol what" << std::endl;
- }
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
View
14 csgi.hpp
@@ -19,6 +19,7 @@
#include <openssl/err.h>
#include <iostream> // FIXME REMOVEME
+#include <pthread.h>
/**
* @file csgi.hpp
@@ -109,15 +110,16 @@ class Server {
throw CSGI::Exception("Error checking private key");
}
- pid_ = -1;
+ worker_ = NULL;
}
~Server()
{
std::cerr << "Cleaning up\n" << std::endl;
- if (pid_ != -1) {
- kill(pid_, SIGTERM);
- }
+ //FIXME
+ //if (pid_ != -1) {
+ // kill(pid_, SIGTERM);
+ //}
close(sockfd_);
freeaddrinfo(res_);
SSL_CTX_free(ssl_ctx_);
@@ -130,8 +132,8 @@ class Server {
* jeśli nie, wywołanie jest blokujące
*/
void run(bool async);
-private: // udokumentowane w csgi.cpp
void serve();
+private: // udokumentowane w csgi.cpp
Env parse_request(SSL*);
void send_response(Response&, SSL*);
Application *app_;
@@ -139,8 +141,8 @@ class Server {
int sockfd_;
int port_;
int backlog_;
- int pid_;
+ pthread_t *worker_;
SSL_METHOD *ssl_method_;
SSL_CTX *ssl_ctx_;
};
Please sign in to comment.
Something went wrong with that request. Please try again.