X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=inline;f=src%2Fclient%2Fprotocol.c;h=079a4ff0d03814a3e285bdd246ebd73123d8c7f6;hb=857e7c75bb043a518ea0e724e635f68702ba33d0;hp=dae51cbcf3908e9f3340d96dcfa7800957be0407;hpb=ee724c0360fac7840d032812b5ab677aa0d15074;p=tpg%2Fopendispense2.git diff --git a/src/client/protocol.c b/src/client/protocol.c index dae51cb..079a4ff 100644 --- a/src/client/protocol.c +++ b/src/client/protocol.c @@ -9,9 +9,11 @@ * This file is licenced under the 3-clause BSD Licence. See the file * COPYING for full details. */ +//#define DEBUG_TRACE_SERVER 2 #include #include #include +#include #include // gethostbyname #include #include @@ -701,7 +703,7 @@ int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const c rv = RV_BAD_ITEM; break; case 403: // Not in coke - fprintf(stderr, "You are not in coke (sucker)\n"); + fprintf(stderr, "Permissions error: %s\n", buf+4); rv = RV_PERMISSIONS; break; case 404: // Unknown user @@ -742,7 +744,7 @@ int Dispense_SetBalance(int Socket, const char *Username, int Balance, const cha switch(responseCode) { case 200: return 0; // OK - case 403: // Not in coke + case 403: // Not an administrator fprintf(stderr, "You are not an admin\n"); return RV_PERMISSIONS; case 404: // Unknown user @@ -1176,15 +1178,15 @@ int Dispense_SetItem(int Socket, const char *Type, int ID, int NewPrice, const c // === // Helpers // === +/// Read from the input socket until a newline is seen char *ReadLine(int Socket) { static char buf[BUFSIZ]; - static int bufPos = 0; static int bufValid = 0; - int len; + int len = 0; char *newline = NULL; int retLen = 0; - char *ret = malloc(10); + char *ret = malloc(32); #if DEBUG_TRACE_SERVER printf("ReadLine: "); @@ -1193,41 +1195,55 @@ char *ReadLine(int Socket) ret[0] = '\0'; + // While a newline hasn't been seen while( !newline ) { + assert(bufValid < BUFSIZ); + // If there is data left over from a previous call, use the data from that for the first pass if( bufValid ) { len = bufValid; + bufValid = 0; } else { - len = recv(Socket, buf+bufPos, BUFSIZ-1-bufPos, 0); + // Otherwise read some data + len = recv(Socket, buf, BUFSIZ, 0); if( len <= 0 ) { free(ret); return strdup("599 Client Connection Error\n"); } } - buf[bufPos+len] = '\0'; + assert(len < BUFSIZ); + buf[len] = '\0'; - newline = strchr( buf+bufPos, '\n' ); + // Search for newline in buffer + newline = strchr( buf, '\n' ); if( newline ) { *newline = '\0'; } - retLen += strlen(buf+bufPos); + // Increment return length by amount of data up to newline (or end of read) + retLen += strlen(buf); ret = realloc(ret, retLen + 1); - strcat( ret, buf+bufPos ); - - if( newline ) { - int newLen = newline - (buf+bufPos) + 1; - bufValid = len - newLen; - len = newLen; - } - if( len + bufPos == BUFSIZ - 1 ) bufPos = 0; - else bufPos += len; + assert(ret); // evil NULL check + strcat( ret, buf ); // append buffer data } #if DEBUG_TRACE_SERVER printf("%i '%s'\n", retLen, ret); #endif + + // If the newline wasn't the last character in the buffer. (I.e. there's extra data for the next call) + assert(newline - buf + 1 <= len); + if( newline - buf + 1 < len ) { + int extra_bytes = len - (newline - buf + 1); + // Copy `extra_bytes` from end of buffer down to start and set `bufValid` to `extra_bytes`? + memmove(&buf[0], newline + 1, extra_bytes); + bufValid = extra_bytes; + + #if DEBUG_TRACE_SERVER > 1 + printf("- Caching %i bytes '%.*s'\n", bufValid, bufValid, buf); + #endif + } return ret; }