diff options
| author | Nakidai <nakidai@disroot.org> | 2024-09-28 16:09:14 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2024-09-28 16:09:14 +0300 |
| commit | d3046284b23ca81be438c94e8f83b7e13e929d81 (patch) | |
| tree | 541a2de8cf543eee4abd99e6916e3169672f8cea | |
| parent | ffa61b7b6930fe4a08bb53ace07c2ee40a9cef43 (diff) | |
| download | cptc-d3046284b23ca81be438c94e8f83b7e13e929d81.tar.gz cptc-d3046284b23ca81be438c94e8f83b7e13e929d81.zip | |
Make it multithreaded v1.0.0
And also add `int n` to the CPTC_requestHandler so it threads will not conflict with each other with filenames
| -rw-r--r-- | cptc.c | 32 | ||||
| -rw-r--r-- | cptc.h | 2 | ||||
| -rw-r--r-- | requestHandler.c | 10 |
3 files changed, 33 insertions, 11 deletions
diff --git a/cptc.c b/cptc.c index 0396b71..1dafddc 100644 --- a/cptc.c +++ b/cptc.c @@ -1,11 +1,14 @@ #include "cptc.h" +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> + +#include <pthread.h> #include <unistd.h> #include <curl/curl.h> @@ -20,11 +23,30 @@ } 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) { struct sockaddr_in addr, peer; socklen_t peer_size; int fd, peerfd; + int n = 0; curl_easy_init(); @@ -56,10 +78,10 @@ void CPTC(const char *ip, in_port_t port) } printf("Connection from %s:%u!\n", inet_ntoa(peer.sin_addr), peer.sin_port); - /* - * TODO: Make it multithreaded - */ - CPTC_requestHandler(peerfd); - close(peerfd); + pthread_t thread; + struct RequestHandlerArgs arg = {peerfd, n++, false}; + pthread_create(&thread, NULL, (void *(*)(void *))requestHandlerThread, &arg); + pthread_detach(thread); + while (!arg.accept); } } diff --git a/cptc.h b/cptc.h index 2eaa14f..9b57cd0 100644 --- a/cptc.h +++ b/cptc.h @@ -14,7 +14,7 @@ extern const char *CPTC_token; extern const char *CPTC_root; void CPTC(const char *address, in_port_t port); -void CPTC_requestHandler(int fd); +void CPTC_requestHandler(int fd, int n); char *CPTC_downloadAvatar(long long uid, const char *download_path); #endif /* __CPTC_H__ */ diff --git a/requestHandler.c b/requestHandler.c index 68442f0..6e4a691 100644 --- a/requestHandler.c +++ b/requestHandler.c @@ -23,15 +23,15 @@ static bool isnumber(const char *s) return true; } -static char *generate_filename(char *buf, size_t size, const char *ext) +static char *generate_filename(char *buf, size_t size, const char *ext, int n) { struct timespec tp; clock_gettime(CLOCK_REALTIME, &tp); - snprintf(buf, size, "/tmp/%lld.%s", tp.tv_sec * 1000000000LL + tp.tv_nsec, ext); + snprintf(buf, size, "/tmp/%d-%lld.%s", n, tp.tv_sec * 1000000000LL + tp.tv_nsec, ext); return buf; } -void CPTC_requestHandler(int fd) +void CPTC_requestHandler(int fd, int n) { enum CPTC_Method method; int ch; @@ -83,7 +83,7 @@ void CPTC_requestHandler(int fd) char *length = malloc(sizeof(*length) * 32); *strchr(path, '.') = '\0'; - generate_filename(filenamebuf, sizeof(filenamebuf), "png"); + generate_filename(filenamebuf, sizeof(filenamebuf), "png", n); char *in = CPTC_downloadAvatar(atoll(path + 1), filenamebuf); if (!in) { @@ -92,7 +92,7 @@ void CPTC_requestHandler(int fd) send(fd, response, strlen(response), 0); return; } - generate_filename(filenamebuf, sizeof(filenamebuf), "gif"); + generate_filename(filenamebuf, sizeof(filenamebuf), "gif", n); CPetPet(in, filenamebuf, 2); printf("From %s to %s\n", in, filenamebuf); |