From 3780f207f924f14734cb839fd015bd883fe52ff1 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 30 Jan 2025 21:02:42 -0800 Subject: restructure project --- src/ramen/main.c | 102 ++++++++++++++++++++------------------------------- src/ramen/protocol.c | 49 +++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 63 deletions(-) create mode 100644 src/ramen/protocol.c (limited to 'src/ramen') diff --git a/src/ramen/main.c b/src/ramen/main.c index 799a5b2..86a4d2c 100644 --- a/src/ramen/main.c +++ b/src/ramen/main.c @@ -12,39 +12,32 @@ #include #include +#include "../include/stdobj.h" #include "../include/better_string.h" #include "../include/hash_table.h" #include "../include/helpers.h" #include "../include/list.h" #include "../include/opcodes.h" #include "../include/protocol.h" -#include "../include/tsv.h" +#include "../include/bsv.h" #include "../include/opcodes.h" int PORT = DEFAULT_PORT; -int nfds = 1; -struct pollfd fds[MAX_CONNECTIONS * 2]; -ht_t *CHAN; -ht_t *USERS; +server_t *STATE; void handle_sigint(int sig) { - for (int i = 0; i < nfds; i++) { - if (fds[i].fd >= 0) { - close(fds[i].fd); - } - } exit(0); } list_t *tokenize_buf(char *buf) { - tsv_t *tsv = init_tsv(buf); - string_t *s = tsv_next(tsv); + bsv_t *bsv = init_bsv(buf, '\t'); + string_t *s = bsv_next(bsv); list_t *a = init_list(); while (s) { list_push_back(a, s); - s = tsv_next(tsv); + s = bsv_next(bsv); } return a; @@ -59,9 +52,9 @@ void set_non_blocking(int sock) { int main(int argc, char **argv) { char buffer[MAX_BUFSIZE]; char res_buffer[MAX_BUFSIZE]; - struct pollfd *local_fds1 = fds; - struct pollfd *local_fds2 = fds + MAX_CONNECTIONS; - struct pollfd *local_fds = local_fds1; + STATE = init_server(1000); + struct pollfd *local_fds1 = STATE->fds; + struct pollfd *local_fds2 = STATE->fds + MAX_CONNECTIONS; /* We start by initializing our net-related structs */ signal(SIGINT, handle_sigint); @@ -99,9 +92,9 @@ int main(int argc, char **argv) { connects, we know from the listening socket. */ - alloc_zero(fds, sizeof(fds)); - fds[0].fd = listen_sd; - fds[0].events = POLLIN; + alloc_zero(STATE->fds, 2 * MAX_BUFSIZE * sizeof(struct pollfd)); + STATE->fds[0].fd = listen_sd; + STATE->fds[0].events = POLLIN; int timeout = 6000; int sock_poll; @@ -114,19 +107,19 @@ int main(int argc, char **argv) { if an fd loses connection, we want to remove all -1's from the fds array */ - sock_poll = poll(local_fds, nfds, timeout); + sock_poll = poll(STATE->fds, STATE->nfds, timeout); die_lz(sock_poll, "poll()"); - for (int i = 0; i < nfds; i++) { - if (local_fds[i].revents == 0) + for (int i = 0; i < STATE->nfds; i++) { + if (STATE->fds[i].revents == 0) continue; - if (local_fds[i].revents != POLLIN) { + if (STATE->fds[i].revents != POLLIN) { end_server = true; break; } - if (local_fds[i].fd == listen_sd) { + if (STATE->fds[i].fd == listen_sd) { printf("socket is readable\n"); int new_sd; @@ -139,15 +132,18 @@ int main(int argc, char **argv) { break; } - local_fds[nfds].fd = new_sd; - local_fds[nfds].events = POLLIN; - nfds++; + STATE->fds[STATE->nfds].fd = new_sd; + STATE->fds[STATE->nfds].events = POLLIN; + STATE->nfds++; + /* TODO: Create new user */ + + /* TODO: add new user to maps */ } while (new_sd >= 0); } else { bool close_conn = false; int fd_recv; int fd_send; - fd_recv = recv(local_fds[i].fd, buffer, sizeof(buffer), 0); + fd_recv = recv(STATE->fds[i].fd, buffer, sizeof(buffer), 0); if (fd_recv < 0 && errno != EWOULDBLOCK) { perror("recv() failed"); @@ -157,29 +153,9 @@ int main(int argc, char **argv) { close_conn = true; } else { list_t *tokens = tokenize_buf(buffer); - string_t *opcode = list_pop_front(tokens); - int op = encode_server_opcode(opcode->buf); - - switch (op) { - case CO_NCK: - break; - case CO_JN: - break; - case CO_NOP: - break; - case CO_PST: - break; - case CO_DM: - break; - case CO_QT: - break; - case CO_LVE: - break; - default: - break; - } - - fd_send = send(local_fds[i].fd, buffer, fd_recv, 0); + server_execop(STATE, tokens); + /* TODO: correct reciept */ + fd_send = send(STATE->fds[i].fd, buffer, fd_recv, 0); if (fd_send < 0) { perror("send()"); close_conn = true; @@ -187,33 +163,33 @@ int main(int argc, char **argv) { } if (close_conn) { - close(local_fds[i].fd); - local_fds[i].fd = 0; + close(STATE->fds[i].fd); + STATE->fds[i].fd = 0; compress_array = true; } } } if (compress_array) { - int cur_nfds = nfds; - nfds = 0; + int cur_nfds = STATE->nfds; + STATE->nfds = 0; for (int i = 0; i < cur_nfds; i++) { - if (local_fds[i].fd != 0) { - local_fds2[nfds] = local_fds[i]; - nfds++; + if (STATE->fds[i].fd != 0) { + local_fds2[STATE->nfds] = STATE->fds[i]; + STATE->nfds++; } } local_fds1 = local_fds2; - local_fds2 = local_fds; - local_fds = local_fds1; + local_fds2 = STATE->fds; + STATE->fds = local_fds1; alloc_zero(local_fds2, MAX_CONNECTIONS); } } while (!end_server); - for (int i = 0; i < nfds; i++) { - if (fds[i].fd >= 0) { - close(fds[i].fd); + for (int i = 0; i < STATE->nfds; i++) { + if (STATE->fds[i].fd >= 0) { + close(STATE->fds[i].fd); } } diff --git a/src/ramen/protocol.c b/src/ramen/protocol.c new file mode 100644 index 0000000..eea5da2 --- /dev/null +++ b/src/ramen/protocol.c @@ -0,0 +1,49 @@ +#include "../include/protocol.h" +#include "../include/better_string.h" +#include "../include/helpers.h" + +#include +#include +#include + +extern int GUESTS; + +string_t *date_str() { + char dateStr[11]; + time_t t = time(NULL); + struct tm *tm_info = localtime(&t); + + strftime(dateStr, sizeof(dateStr), "%d-%m-%Y", tm_info); + return init_string(dateStr); +} + +bool same_day(struct tm *date1, struct tm *date2) { + return (date1->tm_year == date2->tm_year && date1->tm_mon == date2->tm_mon && + date1->tm_mday == date2->tm_mday); +} + +user_t *init_user(int fd) { + char buf[20]; + user_t *user = safe_calloc(1, sizeof(user_t)); + string_t *nick = init_string("guest"); + GUESTS++; + snprintf(buf, 20, "%d", GUESTS); + string_concat_const(nick, buf); + user->is_guest = true; + user->autojoin = NULL; + user->dms = NULL; + return user; +} + +server_t *init_server(size_t size) { + server_t *s = safe_calloc(1, sizeof(server_t)); + s->accmap = init_ht(size); + s->chanmap = init_ht(size); + s->fdmap = init_ht(size); + s->friendmap = init_ht(size); + s->opmap = init_ht(size); + s->usermap = init_ht(size); + s->fds = NULL; + s->nfds = 4096; + return s; +} -- cgit