about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-06-18 16:39:41 +0300
committerNakidai <nakidai@disroot.org>2025-06-18 16:50:57 +0300
commitf41dd07dfc4ad4e5b77ebc24986cb3422f44ea5e (patch)
treea2770addaa6082b01c136bc603d0319338e1d8ed
parent820203c352d2af7318e67640282a69ed41598f93 (diff)
downloadcptc-f41dd07dfc4ad4e5b77ebc24986cb3422f44ea5e.tar.gz
cptc-f41dd07dfc4ad4e5b77ebc24986cb3422f44ea5e.zip
Remove pthreads
I decided that they're not needed there. Instead you can run multiple
instances to make them run in parallel. For this flag -w/--id is added
-rw-r--r--cptc.c32
-rw-r--r--cptc.h3
-rw-r--r--main.c15
3 files changed, 21 insertions, 29 deletions
diff --git a/cptc.c b/cptc.c
index 1dafddc..32cdfcc 100644
--- a/cptc.c
+++ b/cptc.c
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include "cptc.h"
 
 #include <stdbool.h>
@@ -8,7 +9,6 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include <pthread.h>
 #include <unistd.h>
 
 #include <curl/curl.h>
@@ -23,30 +23,11 @@
     } while (0)             \
 
 
-struct RequestHandlerArgs
-{
-    int fd;
-    int n;
-    bool accept;
-};
-
-static void *requestHandlerThread(struct RequestHandlerArgs *arg)
-{
-    int fd = arg->fd;
-    int n = arg->n;
-    arg->accept = true;
-    CPTC_requestHandler(fd, n);
-    close(fd);
-    pthread_exit(NULL);
-}
-
-
-void CPTC(const char *ip, in_port_t port)
+void CPTC(const char *ip, in_port_t port, unsigned id)
 {
     struct sockaddr_in addr, peer;
     socklen_t peer_size;
     int fd, peerfd;
-    int n = 0;
 
     curl_easy_init();
 
@@ -54,6 +35,8 @@ void CPTC(const char *ip, in_port_t port)
         error("socket()", 1);
     if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int))) < 0)
         error("setsockopt()", 1);
+    if ((setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &(int){1}, sizeof(int))) < 0)
+        error("setsockopt()", 1);
 
     addr = (struct sockaddr_in)
     {
@@ -78,10 +61,7 @@ void CPTC(const char *ip, in_port_t port)
         }
         printf("Connection from %s:%u!\n", inet_ntoa(peer.sin_addr), peer.sin_port);
 
-        pthread_t thread;
-        struct RequestHandlerArgs arg = {peerfd, n++, false};
-        pthread_create(&thread, NULL, (void *(*)(void *))requestHandlerThread, &arg);
-        pthread_detach(thread);
-        while (!arg.accept);
+        CPTC_requestHandler(peerfd, id);
+        close(peerfd);
     }
 }
diff --git a/cptc.h b/cptc.h
index 463e2c9..55b78b6 100644
--- a/cptc.h
+++ b/cptc.h
@@ -30,8 +30,9 @@ extern const char *CPTC_root;
  * Starts CPTC server
  * @param address Adress to bind
  * @param port Port to bind
+ * @param id Worker ID
  */
-void CPTC(const char *address, in_port_t port);
+void CPTC(const char *address, in_port_t port, unsigned id);
 
 /**
  * Handles request
diff --git a/main.c b/main.c
index 4785b93..bec406b 100644
--- a/main.c
+++ b/main.c
@@ -20,6 +20,7 @@ static struct option long_options[] =
     {"help", 0, NULL, 'h'},
     {"host", 1, NULL, 'i'},
     {"port", 1, NULL, 'p'},
+    {"id",   1, NULL, 'w'},
     {0}
 };
 
@@ -35,6 +36,7 @@ noreturn void usage(char *name, bool full)
             "  -h, --help        show this help message and quit\n"
             "  -i, --host IP     set bind address to IP )default: 127.0.0.1)\n"
             "  -p, --port PORT   set bind port to PORT (default: 8080)\n"
+            "  -w, --id   ID     set worker id to ID (default: 0)\n"
             "Environment variables:\n"
             "  CPTC_TOKEN=TOKEN  Discord bot's TOKEN\n"
         :
@@ -56,9 +58,10 @@ int main(int argc, char **argv)
 {
     char *address = "127.0.0.1";
     int port = 8080;
+    unsigned id = 0;
     int ch;
 
-    while ((ch = getopt_long(argc, argv, "hi:p:", long_options, NULL)) != EOF)
+    while ((ch = getopt_long(argc, argv, "hi:p:w:", long_options, NULL)) != EOF)
     {
         switch (ch)
         {
@@ -73,6 +76,14 @@ int main(int argc, char **argv)
             }
             port = atoi(optarg);
             break;
+        case 'w':
+            if (!isnumber(optarg))
+            {
+                fprintf(stderr, "%s: id should be an unsigned integer\n", argv[0]);
+                usage(argv[0], false);
+            }
+            id = atoi(optarg);
+            break;
         case 'h':
             usage(argv[0], true);
             /* NOTREACHED */
@@ -88,5 +99,5 @@ int main(int argc, char **argv)
         usage(argv[0], false);
     }
 
-    CPTC(address, port);
+    CPTC(address, port, id);
 }