~aritra1911/mc_server

2e2e3b2dbe922b3cf90f11831e4240feaf6369af — Aritra Sarkar 2 years ago dc9a328 master pending
Confirm sender command instead of echoing
3 files changed, 46 insertions(+), 21 deletions(-)

M common.h
M events.c
M response.c
M common.h => common.h +17 -13
@@ 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_ */

M events.c => events.c +25 -8
@@ 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);

M response.c => response.c +4 -0
@@ 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