summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f529f86)
raw | patch | inline | side by side (parent: f529f86)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 12 Nov 2013 16:39:53 +0000 (17:39 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 12 Nov 2013 16:39:53 +0000 (17:39 +0100) |
src/frontend/connection.c | patch | blob | history | |
src/frontend/sock.c | patch | blob | history | |
src/include/frontend/connection.h | patch | blob | history |
index 940f61b493c0c0f284eb36fec4ae4d26ae07822e..f235dc7a84e09d2697010276c089dfc6422270a6 100644 (file)
*/
#include "sysdb.h"
+#include "core/error.h"
#include "frontend/connection.h"
#include "utils/strbuf.h"
* 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 11fa936686da75e169445842840cad502615d676..1b80efed7522f764ccf34334cae5a88f8df1c227 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
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,
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)
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.