From aede6a1e93a5ced99668121e56a7b28a27c7df8a Mon Sep 17 00:00:00 2001 From: Thomas Lindner Date: Wed, 19 Oct 2022 21:26:20 +0200 Subject: [PATCH] move buffer code into own file --- src/CMakeLists.txt | 3 ++- src/buffer.c | 34 ++++++++++++++++++++++++++++ src/{network.c => connection.c} | 40 +++++++++++++-------------------- src/main.c | 2 +- 4 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/buffer.c rename src/{network.c => connection.c} (83%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 980ac9f..8fac055 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(${PROJECT_NAME} main.c - network.c + connection.c + buffer.c ) target_link_libraries(${PROJECT_NAME} libev::ev diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 0000000..38198a4 --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,34 @@ +#include +#include + +#if INTERFACE + +#include + +struct Buffer { + size_t size; + char data[4096]; +}; + +#define buffer_size(buffer) ((buffer)->size) +#define buffer_capacity(buffer) (sizeof(buffer)->data) +#define buffer_data(buffer) ((buffer)->data) + +#endif + +void buffer_consume(Buffer *buffer, size_t n) { + buffer->size -= n; + memmove(buffer->data, &buffer->data[n], buffer->size); +} + +void buffer_provide(Buffer *buffer, size_t n) { + buffer->size += n; +} + +size_t buffer_size_remaining(Buffer *buffer) { + return buffer_capacity(buffer) - buffer_size(buffer); +} + +char *buffer_data_remaining(Buffer *buffer) { + return &buffer->data[buffer_size(buffer)]; +} diff --git a/src/network.c b/src/connection.c similarity index 83% rename from src/network.c rename to src/connection.c index 2787ef5..c0b2ac6 100644 --- a/src/network.c +++ b/src/connection.c @@ -1,5 +1,5 @@ #include -#include +#include #include #if INTERFACE @@ -18,20 +18,6 @@ struct Connection { Buffer write_buf; }; -struct Buffer { - size_t size; - char data[4096]; -}; - -#endif - -void connection_init(EV_P_ Connection *connection, struct tls_config *config) { - (void)(EV_A_ 0); - ev_io_init(&connection->watcher, NULL, STDIN_FILENO, 0); - connection->ctx = tls_client(); - tls_configure(connection->ctx, config); -} - #define connection_set_failure_cb(connection, callback) \ do { \ (connection)->failure_cb = callback; \ @@ -45,6 +31,15 @@ void connection_init(EV_P_ Connection *connection, struct tls_config *config) { #define connection_read_buf(connection) &((connection)->read_buf) #define connection_write_buf(connection) &((connection)->write_buf) +#endif + +void connection_init(EV_P_ Connection *connection, struct tls_config *config) { + (void)(EV_A_ 0); + ev_io_init(&connection->watcher, NULL, STDIN_FILENO, 0); + connection->ctx = tls_client(); + tls_configure(connection->ctx, config); +} + void connection_read_some(EV_P_ Connection *connection) { assert(connection->success_cb && connection->failure_cb); ev_set_cb(&connection->watcher, connection_read_some_cb); @@ -57,9 +52,8 @@ static void connection_read_some_cb(EV_P_ ev_io *watcher, int revents) { (void)revents; ev_io_stop(EV_A_ watcher); - n = tls_read(connection->ctx, - &connection->read_buf.data[connection->read_buf.size], - sizeof(connection->read_buf.data) - connection->read_buf.size); + n = tls_read(connection->ctx, buffer_data_remaining(&connection->read_buf), + buffer_size_remaining(&connection->read_buf)); if (n == TLS_WANT_POLLIN) { ev_io_modify(watcher, EV_READ); ev_io_start(EV_A_ watcher); @@ -69,7 +63,7 @@ static void connection_read_some_cb(EV_P_ ev_io *watcher, int revents) { } else if (n == -1) { connection->failure_cb(EV_A_ connection); } else { - connection->read_buf.size += n; + buffer_provide(&connection->read_buf, n); connection->success_cb(EV_A_ connection); } } @@ -86,8 +80,8 @@ static void connection_write_some_cb(EV_P_ ev_io *watcher, int revents) { (void)revents; ev_io_stop(EV_A_ watcher); - n = tls_write(connection->ctx, connection->write_buf.data, - connection->write_buf.size); + n = tls_write(connection->ctx, buffer_data(&connection->write_buf), + buffer_size(&connection->write_buf)); if (n == TLS_WANT_POLLIN) { ev_io_modify(watcher, EV_READ); ev_io_start(EV_A_ watcher); @@ -97,9 +91,7 @@ static void connection_write_some_cb(EV_P_ ev_io *watcher, int revents) { } else if (n == -1) { connection->failure_cb(EV_A_ connection); } else { - connection->write_buf.size -= n; - memmove(connection->write_buf.data, &connection->write_buf.data[n], - connection->write_buf.size); + buffer_consume(&connection->write_buf, n); connection->success_cb(EV_A_ connection); } } diff --git a/src/main.c b/src/main.c index 94a5803..19fd759 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ int main(int argc, char **argv) { struct tls_config *config; Connection connection; - (void) argc, (void) argv; + (void)argc, (void)argv; tls_init(); config = tls_config_new(); connection_init(EV_DEFAULT_ & connection, config);