// AMQP // ==== #include "amqp_common.h" Term amqp_connect_run(Env e, Term* f, IoWork* w) { uint64_t hn = 0; uint64_t un = 0; uint64_t pn = 0; uint64_t vn = 0; char* host = io_cstr(e, f[0], &hn); char* user = io_cstr(e, f[2], &un); char* pass = io_cstr(e, f[3], &pn); char* vhost = io_cstr(e, f[4], &vn); u32 port = (u32)f[1]; amqp_connection_state_t conn = amqp_new_connection(); amqp_socket_t* sock = amqp_tcp_socket_new(conn); u32 err = 0; if (conn == NULL || sock == NULL) { err = ENOMEM; } else if (amqp_socket_open(sock, host, port)) { err = (u32)errno; } else { amqp_rpc_reply_t reply = amqp_login( conn, vhost, 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, user, pass); err = lapine_rpc_error(reply); } free(host); free(user); free(pass); free(vhost); if (err != 0) { if (conn != NULL) { amqp_destroy_connection(conn); } return io_fail(e, err, "lapine: amqp connect failed"); } LapineConnection* lap = lapine_conn_new(conn); return io_done(e, (Term)(intptr_t)lap); } static void __attribute__((constructor)) amqp_connect_use(void) { io_eff(CID_AMQP_CONNECT, amqp_connect_run, 0); }