src/irc_net_connect.c
branchnew-transport
changeset 156 6534a4ac957b
parent 154 f4472119de3b
child 177 a74b55104fb9
--- a/src/irc_net_connect.c	Tue Apr 28 20:27:45 2009 +0300
+++ b/src/irc_net_connect.c	Tue Apr 28 22:08:59 2009 +0300
@@ -1,5 +1,6 @@
 
 #include "irc_net_internal.h"
+#include "sock_tcp.h"
 #include "log.h"
 
 #include <time.h>
@@ -25,18 +26,18 @@
 }
 
 /**
- * We have succesfully established a connection to our server with the given sock, so create the irc_conn and bind it
- * to us.
+ * We have succesfully established a connection to our server with the given transport, so create the irc_conn and bind
+ * it to us.
  *
- * If this fails, this will clean up any partial state, including sock.
+ * If this fails, this will clean up any partial state, including \a transport.
  */
-static err_t irc_net_connected (struct irc_net *net, struct sock_stream *sock, struct error_info *err)
+static err_t irc_net_connected (struct irc_net *net, transport_t *transport, struct error_info *err)
 {
     // mark state
     net->connecting = false;
 
     // create the irc connection state
-    if (irc_conn_create(&net->conn, sock, &irc_net_conn_callbacks, net, err))
+    if (irc_conn_create(&net->conn, transport, &irc_net_conn_callbacks, net, err))
         goto error;
 
     // add our command handlers
@@ -55,8 +56,8 @@
 
 error:
     if (!net->conn) {
-        // cleanup sock ourselves
-        sock_stream_release(sock);
+        // cleanup transport ourselves
+        transport_destroy(transport);
 
     } else {
         // cleanup the partial stuff
@@ -72,26 +73,36 @@
  * Our sock_*_connect_async callback. If the connect ended up failing, then try and reconnect later. Otherwise, do
  * irc_net_connected().
  */
-static void irc_net_connect_cb (struct sock_stream *sock, struct error_info *conn_err, void *arg)
+static void irc_net_on_connect (transport_t *transport, void *arg)
 {
     struct irc_net *net = arg;
-    struct error_info err;
+    error_t err;
     
-    if (conn_err) {
-        // attempt reconnect later
-        log_err_info(conn_err, "connect failed");
-        
-        if (irc_net_connect(net, false, &err))
-            log_err_info(&err, "unable to reconnect");
+    // yay
+    if (irc_net_connected(net, transport, &err))
+        log_err_info(&err, "irc_net_connected");
+}
 
-    } else {
-        // yay
-        if (irc_net_connected(net, sock, &err))
-            log_err_info(&err, "irc_net_connected");
+static void irc_net_on_connect_error (transport_t *transport, const error_t *conn_err, void *arg)
+{
+    struct irc_net *net = arg;
+    error_t err;
 
-    }
+    // clean up
+    transport_destroy(transport);
+
+    // attempt reconnect later
+    log_err_info(conn_err, "connect failed");
+    
+    if (irc_net_connect(net, false, &err))
+        log_err_info(&err, "unable to reconnect");
 }
 
+static const struct transport_callbacks irc_net_transport_callbacks = {
+    .on_connect = irc_net_on_connect,
+    .on_error   = irc_net_on_connect_error,
+};
+
 /**
  * The low-level connect() implementation, connects based on irc_net::info, calling irc_net_connected/irc_net_reconnect
  * later if this succeeds.
@@ -99,20 +110,24 @@
 static err_t irc_net_do_connect (struct irc_net *net, struct error_info *err)
 {
     struct irc_net_info *info = &net->info;
-    struct sock_stream *sock = NULL;
+    struct transport_info transport_info = { &irc_net_transport_callbacks, net, TRANSPORT_READ | TRANSPORT_WRITE };
+    transport_t *transport = NULL;
 
     // sanity check
     assert(!net->connecting && !net->connected);
 
     // connect based on what's known
-    if (info->raw_sock) {
-        log_debug("connected using raw socket: %p", info->raw_sock);
+    if (info->transport) {
+        log_debug("connected using raw transport: %p", info->transport);
 
-        // direct sock_stream connection
-        sock = info->raw_sock;
+        // direct transport connection
+        transport = info->transport;
 
-        // then create the conn right away
-        if (irc_net_connected(net, sock, err))
+        // invalidate it from info since it will get destroyed later
+        info->transport = NULL;
+
+        // then create the transport right away
+        if (irc_net_connected(net, transport, err))
             goto error;
 
     } else if (info->ssl_cred) {
@@ -123,20 +138,23 @@
         log_debug("connecting to [%s]:%s using SSL", info->hostname, info->service);
 
         // connect
-        if (sock_ssl_connect_async(&sock, info->hostname, info->service, info->ssl_cred, &irc_net_connect_cb, net, err))
+        if (sock_ssl_connect(&transport_info, &transport, info->hostname, info->service, info->ssl_cred, err))
             goto error;
 
         net->connecting = true;
 
-    } else {
+    } else if (info->hostname || info->service) {
         log_debug("connecting to [%s]:%s", info->hostname, info->service);
             
         // begin async connect
-        if (sock_tcp_connect_async(&sock, info->hostname, info->service, &irc_net_connect_cb, net, err))
+        if (sock_tcp_connect(&transport_info, &transport, info->hostname, info->service, err))
             goto error;
         
         net->connecting = true;
 
+    } else {
+        RETURN_SET_ERROR_STR(err, ERR_MISC, "no connection info specified");
+
     }
 
     return SUCCESS;
@@ -161,9 +179,6 @@
         log_err_info(&err, "unable to reconnect");
 }
 
-// XXX: to get the ev_base
-#include "sock_internal.h"
-
 /**
  * Schedule a reconnection attempt in IRC_NET_RECONNECT_INTERVAL.
  */
@@ -204,6 +219,10 @@
     return ERROR_CODE(err);
 }
 
+// XXX: to get the ev_base
+#include "sock_internal.h"
+
+
 err_t irc_net_connect_init (struct irc_net *net, struct error_info *err)
 {
     // look up the ev_base