aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-09-25 21:33:11 -0500
committericeyrazor <iceyrazor@mailfence.com>2026-09-25 21:33:11 -0500
commit817a3b2422c7966fc0ae47ba7ebfbd086792471c (patch)
tree622bfd9e4fcc45feab7cf4aea36762492f1c9940 /src/main.c
init
Diffstat (limited to 'src/main.c')
-rwxr-xr-xsrc/main.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100755
index 0000000..8b28d63
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,56 @@
+#include <launch-socket.c>
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+// dont use stdio in these
+sig_atomic_t running = 1;
+void sig_int_handler(int num)
+{
+ running = 0;
+}
+
+void on_message(struct Server* server, char message[], ssize_t amountRecv, int socketFD)
+{
+ if (amountRecv > 0) {
+ printf("%s\n", message);
+
+ struct AcceptedSocket* sel_socket = server->accepted_socket;
+ while (sel_socket != NULL) {
+ if (sel_socket->socketFD != socketFD) {
+ send(sel_socket->socketFD, message, 30000, 0);
+ }
+ sel_socket = sel_socket->next;
+ }
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ if (argc < 2) {
+ fprintf(stderr, "arg1 must be set for port\n");
+ exit(-1);
+ }
+ int port = atoi(argv[1]);
+ if (port == 0) {
+ fprintf(stderr, "port must not be 0\n");
+ exit(-1);
+ }
+
+ struct Server server = server_constructor(AF_INET, SOCK_STREAM, 0, INADDR_ANY, port, 10, launch_socket);
+ server.on_message = on_message;
+ pthread_t server_thread = server.launch(&server);
+
+ struct sigaction act = { 0 };
+ act.sa_handler = &sig_int_handler;
+ sigaction(SIGINT, &act, NULL);
+
+ while (running) { }
+
+ server_close_socket(&server);
+ pthread_join(server_thread, NULL);
+
+ return 0;
+}