nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 607ff82970a2f289e0529d778c9f634178283569
parent a3d98fc2ed142ec92af337578acbc4940b08ea07
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 21 Nov 2019 20:51:23 -0500

nserve: necho.h -> nserve.h

* nserver/src/necho.c: Remove file.
* nserver/src/nserve.c: New file.
* nserver/src/necho.h: Rename to...
* nserver/src/nserve.h: ...this. Remove functions `echo` and
`echoserve`. Add function `nserve`.

Diffstat:
nserver/src/necho.c | 64----------------------------------------------------------------
nserver/src/necho.h | 15---------------
nserver/src/nserve.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
nserver/src/nserve.h | 14++++++++++++++
4 files changed, 73 insertions(+), 79 deletions(-)

diff --git a/nserver/src/necho.c b/nserver/src/necho.c @@ -1,64 +0,0 @@ -#include <necho.h> - -int slurpsock(RingBuffer *buf, int sock) -{ - ssize_t bytes; - - if (RingBuffer_available_data(buf) == 0) { - buf->start = buf->end = 0; - } - - bytes = recv(sock, RingBuffer_starts_at(buf), - RingBuffer_available_space(buf), 0); - check(bytes >= 0, "Failed to read from socket: %d", sock); - - RingBuffer_commit_write(buf, bytes); - - return bytes; - - error: - return -1; -} - -int barfsock(int sock, RingBuffer *buf, int len) -{ - int bytes = 0; - - bstring data = RingBuffer_get_all(buf); - check(data != NULL, "barfsock: Failed to get from the buffer."); - - bytes = send(sock, bdata(data), len, 0); - check(bytes >= 0, "barfsock: send failed"); - - return bytes; - error: - return -1; -} - -void echoserve(int sock) -{ - int rc = 0; - RingBuffer *buf = RingBuffer_create(1024 * 10); - ssize_t bytes; - - do { - bytes = slurpsock(buf, sock); - check(bytes >= 0, "echoserve: slurpsock failed"); - - if (bytes < 1) { - break; - } - - rc = barfsock(sock, buf, bytes); - check(rc != -1, "echoserve: echo failed"); - } while(1); - - rc = close(sock); - check(rc == 0, "echoserve: close failed"); - - exit(0); - error: - rc = close(sock); - check(rc == 0, "echoserve: close failed"); - exit(1); -} diff --git a/nserver/src/necho.h b/nserver/src/necho.h @@ -1,15 +0,0 @@ -#ifndef _necho_h -#define _necho_h - -#include <stdlib.h> -#include <unistd.h> -#include <sys/socket.h> - -#include <bstrlib.h> -#include <dbg.h> -#include <ringbuffer.h> - -int echo(int sock, char *buf, int len); -void echoserve(int sock); - -#endif diff --git a/nserver/src/nserve.c b/nserver/src/nserve.c @@ -0,0 +1,59 @@ +#include <nserve.h> + +int slurpsock(char *buf, size_t buf_sz, int sock) +{ + ssize_t bytes; + + bytes = recv(sock, buf, buf_sz, 0); + check(bytes >= 0, "Failed to read from socket: %d", sock); + + return bytes; + + error: + return -1; +} + +int barfsock(char *buf, size_t buf_sz, int sock) +{ + ssize_t bytes = 0; + + bytes = send(sock, buf, buf_sz, 0); + check(bytes >= 0, "barfsock: send failed"); + + return bytes; + error: + return -1; +} + +void nserve(int sock) +{ + size_t buf_sz = 200; + char *buf = calloc(sizeof(char), buf_sz); + check_mem(buf); + + // Read command from socket. + ssize_t bytes = slurpsock(buf, buf_sz, sock); + check(bytes >= 0, "nserve: slurpsock failed"); + + // Write response to socket. + int rc = barfsock(buf, bytes, sock); + check(rc != -1, "nserve: echo failed"); + + // Close socket. + rc = close(sock); + check(rc == 0, "nserve: close failed"); + + // Cleanup. + free(buf) + + exit(0); + error: + rc = close(sock); + check(rc == 0, "nserve: close failed"); + + // Cleanup if needed. + if (buf) + free(buf); + + exit(1); +} diff --git a/nserver/src/nserve.h b/nserver/src/nserve.h @@ -0,0 +1,14 @@ +#ifndef _nserve_h +#define _nserve_h + +#include <stdlib.h> +#include <unistd.h> +#include <sys/socket.h> + +#include <bstrlib.h> +#include <dbg.h> +#include <ringbuffer.h> + +void nserve(int sock); + +#endif