diff options
author | Michael Steinert <mike.steinert@gmail.com> | 2012-06-08 15:38:22 -0600 |
---|---|---|
committer | Alan Antonuk <alan.antonuk@gmail.com> | 2013-04-09 15:48:03 -0700 |
commit | 0f022fce389543404f86824ed6c5720d2502cd09 (patch) | |
tree | 6e39adb0a9268d32e6873a48723f3d84c1316e87 /tools | |
parent | 7240f48af34aea8412473f29e93f6443f79230db (diff) | |
download | rabbitmq-c-0f022fce389543404f86824ed6c5720d2502cd09.tar.gz |
Propose new socket API
The general idea is to have a non-instantiable socket base class.
Connection-specific sub-classes provide a constructor and methods for
modifying connection parameters. `amqp_socket_close()` is the
destructor.
Signed-off-by: Michael Steinert <mike.steinert@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/common.c | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/tools/common.c b/tools/common.c index 77bcb61..9738ba6 100644 --- a/tools/common.c +++ b/tools/common.c @@ -39,7 +39,10 @@ #endif #include "common.h" -#include <amqp-ssl.h> +#ifdef WITH_SSL +#include <amqp-ssl-socket.h> +#endif +#include <amqp-tcp-socket.h> #include <errno.h> #include <fcntl.h> #include <stdarg.h> @@ -220,8 +223,6 @@ struct poptOption connect_options[] = { static void init_connection_info(struct amqp_connection_info *ci) { - struct amqp_connection_info defaults; - ci->user = NULL; ci->password = NULL; ci->host = NULL; @@ -229,6 +230,8 @@ static void init_connection_info(struct amqp_connection_info *ci) ci->vhost = NULL; ci->user = NULL; + amqp_default_connection_info(ci); + if (amqp_url) die_amqp_error(amqp_parse_url(strdup(amqp_url), ci), "Parsing URL '%s'", amqp_url); @@ -312,30 +315,12 @@ static void init_connection_info(struct amqp_connection_info *ci) ci->vhost = amqp_vhost; } - -#if WITH_SSL - if (amqp_ssl) { - ci->ssl = true; - } -#endif - - amqp_default_connection_info(&defaults); - - if (!ci->user) - ci->user = defaults.user; - if (!ci->password) - ci->password = defaults.password; - if (!ci->host) - ci->host = defaults.host; - if (ci->port < 0) - ci->port = defaults.port; - if (!ci->vhost) - ci->vhost = defaults.vhost; } amqp_connection_state_t make_connection(void) { - int s; + int status; + amqp_socket_t *socket; struct amqp_connection_info ci; amqp_connection_state_t conn; @@ -343,16 +328,30 @@ amqp_connection_state_t make_connection(void) conn = amqp_new_connection(); if (ci.ssl) { #ifdef WITH_SSL - s = amqp_open_ssl_socket(conn, ci.host, ci.port, amqp_cacert, - amqp_key, amqp_cert); + socket = amqp_ssl_socket_new(); + if (!socket) { + die("creating SSL/TLS socket"); + } + if (amqp_cacert) { + amqp_ssl_socket_set_cacert(socket, amqp_cacert); + } + if (amqp_key && amqp_cert) { + amqp_ssl_socket_set_key(socket, amqp_key, amqp_cert); + } #else die("librabbitmq was not built with SSL/TLS support"); #endif } else { - s = amqp_open_socket(ci.host, ci.port); - amqp_set_sockfd(conn, s); + socket = amqp_tcp_socket_new(); + if (!socket) { + die("creating TCP socket (out of memory)"); + } + } + status = amqp_socket_open(socket, ci.host, ci.port); + if (status) { + die("opening socket to %s:%d", ci.host, ci.port); } - die_amqp_error(s, "opening socket to %s:%d", ci.host, ci.port); + amqp_set_socket(conn, socket); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), |