From 61f538c112b69d3cad10e91de7c3283c4e95a800 Mon Sep 17 00:00:00 2001 From: Vatiken Date: Wed, 21 Mar 2012 04:44:23 +0000 Subject: [PATCH] Small changes to formatting, and to input handling --- src/comm.c | 58 +++++++++++++++++++++++--------------------------- src/protocol.c | 14 ++++++------ src/protocol.h | 2 +- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/comm.c b/src/comm.c index 6690851..7172f0c 100644 --- a/src/comm.c +++ b/src/comm.c @@ -819,9 +819,9 @@ void game_loop(socket_t local_mother_desc) for (d = descriptor_list; d; d = next_d) { next_d = d->next; if (FD_ISSET(d->descriptor, &exc_set)) { - FD_CLR(d->descriptor, &input_set); - FD_CLR(d->descriptor, &output_set); - close_socket(d); + FD_CLR(d->descriptor, &input_set); + FD_CLR(d->descriptor, &output_set); + close_socket(d); } } @@ -831,9 +831,9 @@ void game_loop(socket_t local_mother_desc) if (FD_ISSET(d->descriptor, &input_set)) { if ( d->pProtocol != NULL ) /* KaVir's plugin */ - d->pProtocol->WriteOOB = 0; /* KaVir's plugin */ - if (process_input(d) < 0) - close_socket(d); + d->pProtocol->WriteOOB = 0; /* KaVir's plugin */ + if (process_input(d) < 0) + close_socket(d); } } @@ -899,8 +899,8 @@ void game_loop(socket_t local_mother_desc) /* Print prompts for other descriptors who had no other output */ for (d = descriptor_list; d; d = d->next) { if (!d->has_prompt) { - write_to_descriptor(d->descriptor, make_prompt(d)); - d->has_prompt = TRUE; + write_to_descriptor(d->descriptor, make_prompt(d)); + d->has_prompt = TRUE; } } @@ -1556,11 +1556,8 @@ static int process_output(struct descriptor_data *t) if (STATE(t) == CON_PLAYING && t->character && !IS_NPC(t->character) && !PRF_FLAGGED(t->character, PRF_COMPACT)) strcat(osb, "\r\n"); /* strcpy: OK (osb:MAX_SOCK_BUF-2 reserves space) */ - if (!t->pProtocol->WriteOOB) - { - /* add a prompt */ - strcat(i, make_prompt(t)); /* strcpy: OK (i:MAX_SOCK_BUF reserves space) */ - } + if (!t->pProtocol->WriteOOB) /* add a prompt */ + strcat(i, make_prompt(t)); /* strcpy: OK (i:MAX_SOCK_BUF reserves space) */ /* now, send the output. If this is an 'interruption', use the prepended * CRLF, otherwise send the straight output sans CRLF. */ @@ -1740,13 +1737,13 @@ static ssize_t perform_socket_read(socket_t desc, char *read_point, size_t space { ssize_t ret; -#if defined(CIRCLE_ACORN) - ret = recv(desc, read_point, space_left, MSG_DONTWAIT); -#elif defined(CIRCLE_WINDOWS) - ret = recv(desc, read_point, space_left, 0); -#else - ret = read(desc, read_point, space_left); -#endif + #if defined(CIRCLE_ACORN) + ret = recv(desc, read_point, space_left, MSG_DONTWAIT); + #elif defined(CIRCLE_WINDOWS) + ret = recv(desc, read_point, space_left, 0); + #else + ret = read(desc, read_point, space_left); + #endif /* Read was successful. */ if (ret > 0) @@ -1814,9 +1811,7 @@ static int process_input(struct descriptor_data *t) size_t space_left; char *ptr, *read_point, *write_point, *nl_pos = NULL; char tmp[MAX_INPUT_LENGTH]; - - static char read_buf[MAX_PROTOCOL_BUFFER]; /* KaVir's plugin */ - read_buf[0] = '\0'; /* KaVir's plugin */ + static char read_buf[MAX_PROTOCOL_BUFFER] = { '\0' }; /* KaVir's plugin */ /* first, find the point where we left off reading data */ buf_length = strlen(t->inbuf); @@ -1829,14 +1824,15 @@ static int process_input(struct descriptor_data *t) return (-1); } - bytes_read = perform_socket_read(t->descriptor, read_buf, space_left); - - if ( bytes_read >= 0 ) - { + /* Read # of "bytes_read" from socket, and if we have something, mark the sizeof data + * in the read_buf array as NULL */ + if ((bytes_read = perform_socket_read(t->descriptor, read_buf, space_left)) > 0) read_buf[bytes_read] = '\0'; - ProtocolInput( t, read_buf, bytes_read, t->inbuf ); - bytes_read = strlen(t->inbuf); - } + + /* Since we have recieved atleast 1 byte of data from the socket, lets run it through + * ProtocolInput() and rip out anything that is Out Of Band */ + if ( bytes_read > 0 ) + bytes_read = ProtocolInput( t, read_buf, bytes_read, t->inbuf ); if (bytes_read < 0) /* Error, disconnect them. */ return (-1); @@ -1849,7 +1845,7 @@ static int process_input(struct descriptor_data *t) /* search for a newline in the data we just read */ for (ptr = read_point; *ptr && !nl_pos; ptr++) if (ISNEWL(*ptr)) - nl_pos = ptr; + nl_pos = ptr; read_point += bytes_read; space_left -= bytes_read; diff --git a/src/protocol.c b/src/protocol.c index f70b864..fbd5a5e 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -11,6 +11,7 @@ ******************************************************************************/ #include +#include #include "protocol.h" /****************************************************************************** @@ -349,13 +350,13 @@ void ProtocolDestroy( protocol_t *apProtocol ) free(apProtocol); } -void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *apOut ) +ssize_t ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *apOut ) { static char CmdBuf[MAX_PROTOCOL_BUFFER+1]; static char IacBuf[MAX_PROTOCOL_BUFFER+1]; - int CmdIndex = 0; - int IacIndex = 0; - int Index; + ssize_t CmdIndex = 0; + ssize_t IacIndex = 0; + ssize_t Index; protocol_t *pProtocol = apDescriptor ? apDescriptor->pProtocol : NULL; @@ -365,7 +366,7 @@ void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *a if ( CmdIndex >= MAX_PROTOCOL_BUFFER || IacIndex >= MAX_PROTOCOL_BUFFER ) { ReportBug("ProtocolInput: Too much incoming data to store in the buffer.\n"); - return; + return (-1); } /* IAC IAC is treated as a single value of 255 */ @@ -419,7 +420,7 @@ void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *a { const char *pClientName = pProtocol->pVariables[eMSDP_CLIENT_ID]->pValueString; - InfoMessage(apDescriptor, "Receiving MXP Version From Client.\r\n"); + InfoMessage(apDescriptor, "Receiving MXP Version From Client.\r\n"); free(pProtocol->pVariables[eMSDP_CLIENT_VERSION]->pValueString); pProtocol->pVariables[eMSDP_CLIENT_VERSION]->pValueString = AllocString(pMXPTag); @@ -513,6 +514,7 @@ void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *a /* Copy the input buffer back to the player. */ strcat( apOut, CmdBuf ); + return (CmdIndex); } const char *ProtocolOutput( descriptor_t *apDescriptor, const char *apData, int *apLength ) diff --git a/src/protocol.h b/src/protocol.h index f9aa676..30466b7 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -250,7 +250,7 @@ void ProtocolNegotiate( descriptor_t *apDescriptor ); * whatever is left for the mud to parse normally. Call this after data has * been read into the input buffer, before it is used for anything else. */ -void ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *apOut ); +ssize_t ProtocolInput( descriptor_t *apDescriptor, char *apData, int aSize, char *apOut ); /* Function: ProtocolOutput *