From 2a0f3c8f7d913518afd60156c7d324340d8f6478 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 12 Nov 2013 17:39:53 +0100 Subject: [PATCH] frontend: Moved connection init/close to connection.c. --- src/frontend/connection.c | 44 +++++++++++++++++++++++++++++++ src/frontend/sock.c | 23 ++-------------- src/include/frontend/connection.h | 20 ++++++++++++++ 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 940f61b..f235dc7 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -26,6 +26,7 @@ */ #include "sysdb.h" +#include "core/error.h" #include "frontend/connection.h" #include "utils/strbuf.h" @@ -105,6 +106,49 @@ connection_read(sdb_conn_t *conn) * public API */ +int +sdb_connection_init(sdb_conn_t *conn) +{ + if (conn->buf) { + sdb_log(SDB_LOG_WARNING, "frontend: Attempted to re-initialize " + "a frontend connection"); + return -1; + } + + conn->buf = sdb_strbuf_create(/* size = */ 128); + if (! conn->buf) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer " + "for a new connection"); + sdb_connection_close(conn); + return -1; + } + + conn->cmd = conn->cmd_len = 0; + conn->fd = -1; + return 0; +} /* sdb_connection_init */ + +void +sdb_connection_close(sdb_conn_t *conn) +{ + size_t len; + + if (conn->buf) { + len = sdb_strbuf_len(conn->buf); + if (len) + sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command " + "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s"); + } + + sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", + conn->fd); + close(conn->fd); + conn->fd = -1; + + sdb_strbuf_destroy(conn->buf); + conn->buf = NULL; +} /* sdb_connection_fini */ + ssize_t sdb_connection_read(sdb_conn_t *conn) { diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 11fa936..1b80efe 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -291,12 +291,8 @@ connection_init(sdb_object_t *obj, va_list ap) sock_fd = va_arg(ap, int); - CONN(obj)->conn.buf = sdb_strbuf_create(/* size = */ 128); - if (! CONN(obj)->conn.buf) { - sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer " - "for a new remote connection"); + if (sdb_connection_init(&CONN(obj)->conn)) return -1; - } conn->client_addr_len = sizeof(conn->client_addr); conn->conn.fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr, @@ -337,23 +333,8 @@ connection_init(sdb_object_t *obj, va_list ap) static void connection_destroy(sdb_object_t *obj) { - connection_obj_t *conn; - size_t len; - assert(obj); - conn = CONN(obj); - - len = sdb_strbuf_len(conn->conn.buf); - if (len) - sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command " - "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s"); - - sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i", - conn->conn.fd); - close(conn->conn.fd); - conn->conn.fd = -1; - - sdb_strbuf_destroy(CONN(obj)->conn.buf); + sdb_connection_close(&CONN(obj)->conn); } /* connection_destroy */ static sdb_type_t connection_type = { diff --git a/src/include/frontend/connection.h b/src/include/frontend/connection.h index 7726e75..5748211 100644 --- a/src/include/frontend/connection.h +++ b/src/include/frontend/connection.h @@ -49,6 +49,26 @@ typedef struct { uint32_t cmd_len; } sdb_conn_t; +/* + * sdb_connection_init: + * Initialize an (already allocated) connection. This function allocates and + * initialized any attributes. It's an error to call init on an already + * initialized object. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_connection_init(sdb_conn_t *conn); + +/* + * sdb_connection_close: + * Close a open connection and deallocate any memory used by its attributes. + */ +void +sdb_connection_close(sdb_conn_t *conn); + /* * sdb_connection_read: * Read from an open connection until reading would block. -- 2.30.2