From 2e2e3b2dbe922b3cf90f11831e4240feaf6369af Mon Sep 17 00:00:00 2001 From: Aritra Sarkar Date: Sun, 3 Jul 2022 01:02:04 +0530 Subject: [PATCH] Confirm sender command instead of echoing --- common.h | 30 +++++++++++++++++------------- events.c | 33 +++++++++++++++++++++++++-------- response.c | 4 ++++ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/common.h b/common.h index ff811ef..bfe0671 100644 --- a/common.h +++ b/common.h @@ -25,21 +25,25 @@ # define PORT "4200" +/* TODO: Can we get rid of SND_JOIN and RES_JOIN ?? */ + typedef enum { - SND_NORMAL=0, /* A normal message */ - SND_JOIN, /* Join */ - SND_SETNICK, /* Change nick */ - SND_ACTION, /* Perform an action */ - SND_QUIT /* Quit */ + SND_NORMAL=0, /* A normal message */ + SND_JOIN, /* Join */ + SND_SETNICK, /* Change nick */ + SND_ACTION, /* Perform an action */ + SND_QUIT, /* Quit */ + SND_KEY /* Sending session key to server */ } SendCode; -typedef enum _ResponseCode { - RES_NORMAL=0, /* Normal group messages */ - RES_JOIN, /* User joins */ - RES_SETNICK, /* Nick changes */ - RES_ACTION, /* Actions */ - RES_QUIT, /* User quits or disconnects */ - RES_ERROR /* User did something wrong */ +typedef enum { + RES_NORMAL=0, /* Normal group messages */ + RES_JOIN, /* User joins */ + RES_SETNICK, /* Nick changes */ + RES_ACTION, /* Actions */ + RES_QUIT, /* User quits or disconnects */ + RES_ERROR, /* User did something wrong */ + RES_CONFIRM /* Server announces to all */ } ResponseCode; typedef enum { @@ -52,4 +56,4 @@ typedef enum { IN_SEND_CODE /* Client sent an unknown send code */ } ResponseError; -#endif +#endif /* _COMMON_H_ */ diff --git a/events.c b/events.c index e56de0b..0d8aea7 100644 --- a/events.c +++ b/events.c @@ -452,6 +452,8 @@ recv_and_send(int thread_id, void *arg) free(arg); + /* WARNING: This is a mess */ + if ( (num_bytes = recv(fd, buf, BUFLEN, 0)) == -1 ) { get_err_str(errno, err_str); fprintf(stderr, " ERR : [THREAD #%02i] %s:%i => `recv()`: %s\n", @@ -494,6 +496,27 @@ recv_and_send(int thread_id, void *arg) num_bytes = 2; } + /* Early response / confirmation back to sender */ + if ( *buf == RES_ERROR ) { + if ( send(fd, buf, (size_t) num_bytes, 0) == -1 ) { + get_err_str(errno, err_str); + fprintf(stderr, " ERR : [THREAD #%02i] %s:%i =>" + " `send()` to fd = %i failed: %s\n", + thread_id, __FILE__, __LINE__ - 4, fd, err_str); + } + } else if ( *sessionp->clients[i].nick ) { + char temp = *buf; + *buf = RES_CONFIRM; + if ( send(fd, buf, 1, 0) == -1 ) { + get_err_str(errno, err_str); + fprintf(stderr, " ERR : [THREAD #%02i] %s:%i =>" + " `send()`ing confirmation to fd = %i" + " failed: %s\n", + thread_id, __FILE__, __LINE__ - 5, fd, err_str); + } + *buf = temp; + } + if ( buf[0] == RES_QUIT ) { disconnect: if ( remove_client(i, sessionp) == -1 ) { @@ -529,16 +552,10 @@ disconnect: if ( announce ) { printf("DBUG : [THREAD #%02i] Announcing...\n", thread_id); - if ( buf[0] == RES_ERROR ) { - if ( send(fd, buf, (size_t) num_bytes, 0) == -1 ) { - get_err_str(errno, err_str); - fprintf(stderr, " ERR : [THREAD #%02i] %s:%i =>" - " `send()` to fd = %i failed: %s\n", - thread_id, __FILE__, __LINE__ - 4, fd, err_str); - } - } else { + if ( buf[0] != RES_ERROR ) { pthread_rwlock_rdlock(&sessionp->lock); for (i = 1 /* avoid listener */; i < sessionp->fd_count; i++) { + if ( sessionp->pfds[i].fd == fd ) continue; if ( send(sessionp->pfds[i].fd, buf, (size_t) num_bytes, 0) == -1 ) { get_err_str(errno, err_str); diff --git a/response.c b/response.c index 4851457..2cd3e4c 100644 --- a/response.c +++ b/response.c @@ -137,6 +137,10 @@ respond(client_t *client, const client_t *clients_list, size_t list_size, case SND_QUIT: return append_nick(RES_QUIT, client->nick, buf, len, err_code); + + case SND_KEY: + fprintf(stderr, " ERR : `SND_KEY` : Feature Unimplemented!\n"); + return -1; } /* Uncomment this if the above switch is not handling all possible -- 2.45.2