From: Holger Schemel Date: Wed, 22 Aug 2018 09:27:00 +0000 (+0200) Subject: added functions to network code for sending files X-Git-Tag: 4.1.1.0~62 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=457283149872e45280a6a7f0ee1dcb604ed43da2;p=rocksndiamonds.git added functions to network code for sending files --- diff --git a/src/netserv.c b/src/netserv.c index 9ebfc4e0..0a3661e1 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -312,6 +312,67 @@ void putNetworkBufferString(struct NetworkBuffer *nb, char *s) nb->size = nb->pos; } +int getNetworkBufferFile(struct NetworkBuffer *nb, char *filename) +{ + FILE *file; + int num_bytes = getNetworkBuffer32BitInteger(nb); + int i; + + if (!(file = fopen(filename, MODE_WRITE))) + { + Error(ERR_WARN, "cannot write file '%s' from network buffer", filename); + + return 0; + } + + for (i = 0; i < num_bytes; i++) + { + int b = getNetworkBuffer8BitInteger(nb); + + putFile8Bit(file, b); + } + + fclose(file); + + return num_bytes; +} + +int putNetworkBufferFile(struct NetworkBuffer *nb, char *filename) +{ + File *file; + int filesize_pos = nb->pos; + int num_bytes = 0; + + /* will be replaced with file size */ + putNetworkBuffer32BitInteger(nb, 0); + + if (!(file = openFile(filename, MODE_READ))) + { + Error(ERR_WARN, "cannot read file '%s' to network buffer", filename); + + return 0; + } + + while (1) + { + int b = getFile8Bit(file); + + if (checkEndOfFile(file)) + break; + + putNetworkBuffer8BitInteger(nb, b); + + num_bytes++; + } + + closeFile(file); + + /* set file size */ + putNetwork32BitInteger(&nb->buffer[filesize_pos], num_bytes); + + return num_bytes; +} + void dumpNetworkBuffer(struct NetworkBuffer *nb) { int i; diff --git a/src/netserv.h b/src/netserv.h index df016684..d69deca7 100644 --- a/src/netserv.h +++ b/src/netserv.h @@ -72,6 +72,8 @@ int getNetworkBuffer32BitInteger(struct NetworkBuffer *); void putNetworkBuffer32BitInteger(struct NetworkBuffer *, int); char *getNetworkBufferString(struct NetworkBuffer *); void putNetworkBufferString(struct NetworkBuffer *, char *); +int getNetworkBufferFile(struct NetworkBuffer *, char *); +int putNetworkBufferFile(struct NetworkBuffer *, char *); void dumpNetworkBuffer(struct NetworkBuffer *);