Code

frontend: Moved connection init/close to connection.c.
authorSebastian Harl <sh@tokkee.org>
Tue, 12 Nov 2013 16:39:53 +0000 (17:39 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 12 Nov 2013 16:39:53 +0000 (17:39 +0100)
src/frontend/connection.c
src/frontend/sock.c
src/include/frontend/connection.h

index 940f61b493c0c0f284eb36fec4ae4d26ae07822e..f235dc7a84e09d2697010276c089dfc6422270a6 100644 (file)
@@ -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)
 {
index 11fa936686da75e169445842840cad502615d676..1b80efed7522f764ccf34334cae5a88f8df1c227 100644 (file)
@@ -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 = {
index 7726e75fa92c7d6df025bb5139a92b8d155b9231..5748211f4c1d5c78f367eb1fc9672c825f757c2c 100644 (file)
@@ -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.