void copy_body(amqp_connection_state_t conn, int fd) { size_t body_remaining; amqp_frame_t frame; int res = amqp_simple_wait_frame(conn, &frame); die_amqp_error(res, "waiting for header frame"); if (frame.frame_type != AMQP_FRAME_HEADER) { die("expected header, got frame type 0x%X", frame.frame_type); } body_remaining = frame.payload.properties.body_size; while (body_remaining) { res = amqp_simple_wait_frame(conn, &frame); die_amqp_error(res, "waiting for body frame"); if (frame.frame_type != AMQP_FRAME_BODY) { die("expected body, got frame type 0x%X", frame.frame_type); } write_all(fd, frame.payload.body_fragment); body_remaining -= frame.payload.body_fragment.len; } }
amqp_connection_state_t make_connection(void) { int s; struct amqp_connection_info ci; amqp_connection_state_t conn; init_connection_info(&ci); s = amqp_open_socket(ci.host, ci.port); die_amqp_error(s, "opening socket to %s:%d", ci.host, ci.port); conn = amqp_new_connection(); amqp_set_sockfd(conn, s); die_rpc(amqp_login(conn, ci.vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, ci.user, ci.password), "logging in to AMQP server"); if (!amqp_channel_open(conn, 1)) { die_rpc(amqp_get_rpc_reply(conn), "opening channel"); } return conn; }
static void do_publish(amqp_connection_state_t conn, char *exchange, char *routing_key, amqp_basic_properties_t *props, amqp_bytes_t body) { int res = amqp_basic_publish(conn, 1, cstring_bytes(exchange), cstring_bytes(routing_key), 0, 0, props, body); die_amqp_error(res, "basic.publish"); }
void close_connection(amqp_connection_state_t conn) { int res; die_rpc(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), "closing channel"); die_rpc(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "closing connection"); res = amqp_destroy_connection(conn); die_amqp_error(res, "closing connection"); }
static void init_connection_info(struct amqp_connection_info *ci) { ci->user = NULL; ci->password = NULL; ci->host = NULL; ci->port = -1; 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); if (amqp_server) { char *colon; if (ci->host) die("both --server and --url options specify" " server host"); /* parse the server string into a hostname and a port */ colon = strchr(amqp_server, ':'); if (colon) { char *port_end; size_t host_len; /* Deprecate specifying the port number with the --server option, because it is not ipv6 friendly. --url now allows connection options to be specificied concisely. */ fprintf(stderr, "Specifying the port number with" " --server is deprecated\n"); host_len = colon - amqp_server; ci->host = malloc(host_len + 1); memcpy(ci->host, amqp_server, host_len); ci->host[host_len] = 0; if (ci->port >= 0) die("both --server and --url options specify" " server port"); if (amqp_port >= 0) die("both --server and --port options specify" " server port"); ci->port = strtol(colon+1, &port_end, 10); if (ci->port < 0 || ci->port > 65535 || port_end == colon+1 || *port_end != 0) die("bad server port number in '%s'", amqp_server); } #if WITH_SSL if (amqp_ssl && !ci->ssl) { die("the --ssl option specifies an SSL connection" " but the --server option does not"); } #endif } if (amqp_port >= 0) { if (ci->port >= 0) die("both --port and --url options specify" " server port"); ci->port = amqp_port; } if (amqp_username) { if (ci->user) die("both --username and --url options specify" " AMQP username"); ci->user = amqp_username; } if (amqp_password) { if (ci->password) die("both --password and --url options specify" " AMQP password"); ci->password = amqp_password; } if (amqp_vhost) { if (ci->vhost) die("both --vhost and --url options specify" " AMQP vhost"); ci->vhost = amqp_vhost; } if (amqp_heartbeat < 0) { die("--heartbeat must be a positive value"); } }