#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <signal.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
return server_sock;
}
+
+AcceptorThread::AcceptorThread(int server_sock)
+ : server_sock(server_sock)
+{
+}
+
+void AcceptorThread::run()
+{
+ should_stop = false;
+ pthread_create(&worker_thread, NULL, &AcceptorThread::do_work_thunk, this);
+}
-void *acceptor_thread_run(void *arg)
+void AcceptorThread::stop()
+{
+ should_stop = true;
+ pthread_kill(worker_thread, SIGHUP);
+ if (pthread_join(worker_thread, NULL) == -1) {
+ perror("pthread_join");
+ exit(1);
+ }
+}
+
+void *AcceptorThread::do_work_thunk(void *arg)
+{
+ AcceptorThread *acceptor_thread = reinterpret_cast<AcceptorThread *>(arg);
+ acceptor_thread->do_work();
+ return NULL;
+}
+
+void AcceptorThread::do_work()
{
- int server_sock = int(intptr_t(arg));
while (!hupped) {
// Since we are non-blocking, we need to wait for the right state first.
// Wait up to 50 ms, then check hupped.
// Pick a server, round-robin, and hand over the socket to it.
servers->add_client(sock);
}
- return NULL;
}