src/lib/tcp_client.c
branchnew-lib-errors
changeset 219 cefec18b8268
parent 177 a74b55104fb9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/tcp_client.c	Thu May 28 01:17:36 2009 +0300
@@ -0,0 +1,278 @@
+#include "tcp_internal.h"
+#include "log.h"
+
+/*
+ * Our transport methods
+ */
+static void tcp_client__deinit (transport_t *transport)
+{
+    struct tcp_client *client = transport_check(transport, &tcp_client_type);
+    
+    // proxy
+    tcp_client_deinit(client);
+}
+
+/*
+ * Our transport_type
+ */
+const struct transport_type tcp_client_type = {
+    .base_type = {
+        .parent             = &tcp_transport_type.base_type,
+    },
+    .methods                = {
+        .read               = transport_fd__read,
+        .write              = transport_fd__write,
+        .events             = transport_fd__events,
+        .deinit             = tcp_client__deinit,
+    },
+};
+
+/*
+ * Forward-declare 
+ */
+static void tcp_client_on_connect (struct transport_fd *fd, short what, void *arg);
+
+/*
+ * Function implementations
+ */
+void tcp_client_init (struct tcp_client *client)
+{
+    tcp_transport_init(&client->base_trans, -1);
+    
+    resolve_result_init(&client->rr);
+}
+
+/*
+ * Start connecting to the given address in a non-blocking fashion. Returns any errors that immediately crop up,
+ * otherwise eventually calls tcp_client_connect_done().
+ */
+static err_t tcp_client_connect_addr (struct tcp_client *client, struct addrinfo *addr, error_t *err)
+{
+    struct transport_fd *_fd = &client->base_trans.base_fd;
+    int ret;
+    evutil_socket_t sock;
+    err_t tmp;
+
+    // first, create the socket
+    if ((sock = tcp_sock_create(addr, err)) < 0)
+        return ERROR_CODE(err);
+
+    // set it as our sock
+    if ((ERROR_CODE(err) = transport_fd_set(_fd, sock)))
+        goto error;
+
+    // then, set it up as nonblocking
+    if ((ERROR_CODE(err) = transport_fd_nonblock(_fd, true)))
+        goto error;
+
+    // then, initiate the connect
+    if ((ret = connect(sock, addr->ai_addr, addr->ai_addrlen)) < 0 && errno != EINPROGRESS) 
+        JUMP_SET_ERROR_ERRNO(err, ERR_CONNECT);
+    
+    if (ret < 0) {
+        // ok, connect started, setup our completion callback
+        if ((ERROR_CODE(err) = transport_fd_setup(_fd, tcp_client_on_connect, client)))
+            goto error;
+    
+        // enable for write
+        if ((ERROR_CODE(err) = transport_fd_enable(_fd, TRANSPORT_WRITE)))
+            goto error;
+
+    } else {
+        // oops... blocking connect - fail to avoid confusion
+        // XXX: come up with a better error name to use
+        // XXX: support non-async connects as well
+        JUMP_SET_ERROR_EXTRA(err, ERR_CONNECT, EINPROGRESS);
+    }
+    
+    // ok
+    return SUCCESS;
+
+error:
+    // close the stuff we did open
+    if ((tmp = transport_fd_close(_fd)))
+        log_warn("error closing socket after connect error: %s", error_name(tmp));
+
+    return ERROR_CODE(err);
+}
+
+
+/*
+ * Attempt to connect to the next addrinfo, or the next one, if that fails, etc.
+ *
+ * This does not call transport_connected().
+ */
+static err_t tcp_client_connect_continue (struct tcp_client *client, error_t *err)
+{
+    struct addrinfo *addr;
+
+    // try and connect to each one until we find one that works
+    while ((addr = resolve_result_next(&client->rr))) {
+        // attempt to start connect
+        if (tcp_client_connect_addr(client, addr, err) == SUCCESS)
+            break;
+
+        // log a warning on the failed connect
+        log_warn_error(err, "%s", resolve_addr_text(addr));
+    }
+    
+
+    if (addr)
+        // we succesfully did a tcp_client_connect_addr on valid address
+        return SUCCESS;
+
+    else
+        // all of the connect_async_addr's failed, return the last error
+        return ERROR_CODE(err);
+}
+
+/*
+ * Cleanup our resolver state and any connect callbacks after a connect
+ */
+static void tcp_client_connect_cleanup (struct tcp_client *client)
+{
+    // drop the resolver stuff
+    resolve_result_deinit(&client->rr);
+    
+    // remove our event handler
+    transport_fd_clear(&client->base_trans.base_fd);
+}
+
+/*
+ * Our async connect operation has completed, clean up, set up state for event-based operation with user callbacks, and
+ * invoke transport_connected().
+ *
+ * The given \a err should be NULL for successful completion, or the error for failures.
+ */
+static void tcp_client_connect_done (struct tcp_client *client, error_t *conn_err)
+{
+    error_t err;
+
+    // cleanup
+    tcp_client_connect_cleanup(client);
+
+    if (conn_err)
+        JUMP_SET_ERROR_INFO(&err, conn_err);
+
+    // let the transport handle the rest
+    if (tcp_transport_connected(&client->base_trans, &err))
+        goto error;
+    
+    // ok
+    return;
+
+error:    
+    // pass the error on to transport
+    transport_connected(&client->base_trans.base_fd.base, &err, false);
+}
+
+/*
+ * Our async connect callback
+ */
+static void tcp_client_on_connect (struct transport_fd *fd, short what, void *arg)
+{
+    struct tcp_client *client = arg;
+    error_t err;
+    err_t tmp;
+
+    // XXX: timeouts
+    (void) what;
+    
+    // read socket error code
+    if (tcp_sock_error(client->base_trans.base_fd.fd, &err))
+        goto error;
+
+    // did the connect fail?
+    if (ERROR_EXTRA(&err))
+        JUMP_SET_ERROR(&err, ERR_CONNECT);
+    
+    // done, success
+    return tcp_client_connect_done(client, NULL);
+
+error:
+    // close the socket
+    if ((tmp = transport_fd_close(fd)))
+        log_warn("error closing socket after connect error: %s", error_name(tmp));
+
+    // log a warning
+    log_warn_error(&err, "connect to %s failed", "???");
+
+    // try the next one or fail completely
+    if (tcp_client_connect_continue(client, &err))
+        tcp_client_connect_done(client, &err);
+}
+
+err_t tcp_client_connect_async (struct tcp_client *client, const char *hostname, const char *service, error_t *err)
+{
+    // do the resolving
+    if (resolve_addr(&client->rr, hostname, service, SOCK_STREAM, 0, err))
+        return ERROR_CODE(err);
+
+    // start connecting with the first result
+    if (tcp_client_connect_continue(client, err))
+        goto error;
+
+    // ok
+    return SUCCESS;
+
+error:
+    // cleanup
+    resolve_result_deinit(&client->rr);
+    
+    return ERROR_CODE(err);
+}
+
+void tcp_client_deinit (struct tcp_client *client)
+{
+    // cleanup our stuff
+    resolve_result_deinit(&client->rr);
+    
+    // deinit lower transport
+    tcp_transport_deinit(&client->base_trans);
+}
+
+/*
+ * Deinit and free, not using the transport interface
+ */
+static void tcp_client_destroy (struct tcp_client *client)
+{
+    tcp_client_deinit(client);
+
+    free(client);
+}
+
+/*
+ * Public interface
+ */
+err_t tcp_connect (const struct transport_info *info, transport_t **transport_ptr, 
+        const char *host, const char *service, error_t *err)
+{
+    struct tcp_client *client;
+ 
+    // alloc
+    if ((client = calloc(1, sizeof(*client))) == NULL)
+        return ERR_CALLOC;
+
+    // init transport
+    transport_init(&client->base_trans.base_fd.base, &tcp_client_type, info);
+    
+    // init our state
+    tcp_client_init(client);
+ 
+    // begin connect
+    if (tcp_client_connect_async(client, host, service, err))
+        goto error;
+
+    // good
+    *transport_ptr = &client->base_trans.base_fd.base;
+
+    return 0;
+
+error:
+    // cleanup
+    tcp_client_destroy(client);
+        
+    // return error code
+    return ERROR_CODE(err);
+}
+