move buffer code into own file
This commit is contained in:
parent
aec240b777
commit
aede6a1e93
|
@ -1,6 +1,7 @@
|
|||
add_executable(${PROJECT_NAME}
|
||||
main.c
|
||||
network.c
|
||||
connection.c
|
||||
buffer.c
|
||||
)
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
libev::ev
|
||||
|
|
34
src/buffer.c
Normal file
34
src/buffer.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <buffer.h>
|
||||
#include <string.h>
|
||||
|
||||
#if INTERFACE
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
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)];
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
#include <assert.h>
|
||||
#include <network.h>
|
||||
#include <connection.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue