From 0f022fce389543404f86824ed6c5720d2502cd09 Mon Sep 17 00:00:00 2001 From: Michael Steinert Date: Fri, 8 Jun 2012 15:38:22 -0600 Subject: 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 --- tools/common.c | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'tools') 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 +#ifdef WITH_SSL +#include +#endif +#include #include #include #include @@ -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), -- cgit v1.2.1