static void pni_connection_readable(pn_selectable_t *sel) { pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); pn_transport_t *transport = pni_transport(sel); ssize_t capacity = pn_transport_capacity(transport); if (capacity > 0) { ssize_t n = pn_recv(pn_reactor_io(reactor), pn_selectable_get_fd(sel), pn_transport_tail(transport), capacity); if (n <= 0) { if (n == 0 || !pn_wouldblock(pn_reactor_io(reactor))) { if (n < 0) { pn_condition_t *cond = pn_transport_condition(transport); pn_condition_set_name(cond, "proton:io"); pn_condition_set_description(cond, pn_error_text(pn_io_error(pn_reactor_io(reactor)))); } pn_transport_close_tail(transport); } } else { pn_transport_process(transport, (size_t)n); } } ssize_t newcap = pn_transport_capacity(transport); //occasionally transport events aren't generated when expected, so //the following hack ensures we always update the selector if (1 || newcap != capacity) { pni_connection_update(sel); pn_reactor_update(reactor, sel); } }
static void pni_connection_writable(pn_selectable_t *sel) { pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); pn_transport_t *transport = pni_transport(sel); ssize_t pending = pn_transport_pending(transport); if (pending > 0) { ssize_t n = pn_send(pn_reactor_io(reactor), pn_selectable_get_fd(sel), pn_transport_head(transport), pending); if (n < 0) { if (!pn_wouldblock(pn_reactor_io(reactor))) { pn_condition_t *cond = pn_transport_condition(transport); if (!pn_condition_is_set(cond)) { pn_condition_set_name(cond, "proton:io"); pn_condition_set_description(cond, pn_error_text(pn_io_error(pn_reactor_io(reactor)))); } pn_transport_close_head(transport); } } else { pn_transport_pop(transport, n); } } ssize_t newpending = pn_transport_pending(transport); if (newpending != pending) { pni_connection_update(sel); pn_reactor_update(reactor, sel); } }
static void pni_connection_finalize(pn_selectable_t *sel) { pn_reactor_t *reactor = (pn_reactor_t *) pni_selectable_get_context(sel); pn_transport_t *transport = pni_transport(sel); pn_record_t *record = pn_transport_attachments(transport); pn_record_set(record, PN_TRANCTX, NULL); pn_socket_t fd = pn_selectable_get_fd(sel); pn_close(pn_reactor_io(reactor), fd); }
pn_io_t* reactor::pn_io() const { return pn_reactor_io(pn_object()); }
void pni_handle_bound(pn_reactor_t *reactor, pn_event_t *event) { assert(reactor); assert(event); pn_connection_t *conn = pn_event_connection(event); pn_record_t *record = pn_connection_attachments(conn); pn_url_t *url = (pn_url_t *)pn_record_get(record, PNI_CONN_URL); const char *host = NULL; const char *port = "5672"; pn_string_t *str = NULL; if (url) { host = pn_url_get_host(url); const char *uport = pn_url_get_port(url); if (uport) { port = uport; } else { const char *scheme = pn_url_get_scheme(url); if (scheme && strcmp(scheme, "amqps") == 0) { port = "5671"; } } if (!pn_connection_get_user(conn)) { // user did not manually set auth info const char *user = pn_url_get_username(url); if (user) pn_connection_set_user(conn, user); const char *passwd = pn_url_get_password(url); if (passwd) pn_connection_set_password(conn, passwd); } } else { // for backward compatibility, see if the connection's hostname can be // used for the remote address. See JIRA PROTON-1133 const char *hostname = pn_connection_get_hostname(conn); if (hostname) { str = pn_string(hostname); char *h = pn_string_buffer(str); // see if a port has been included in the hostname. This is not // allowed by the spec, but the old reactor interface allowed it. char *colon = strrchr(h, ':'); if (colon) { *colon = '\0'; host = h; port = colon + 1; } } } // host will be NULL if this connection was created via the acceptor, which // creates its own transport/socket when created. if (!host) { return; } pn_transport_t *transport = pn_event_transport(event); pn_socket_t sock = pn_connect(pn_reactor_io(reactor), host, port); // invalid sockets are ignored by poll, so we need to do this manualy if (sock == PN_INVALID_SOCKET) { pn_condition_t *cond = pn_transport_condition(transport); pn_condition_set_name(cond, "proton:io"); pn_condition_set_description(cond, pn_error_text(pn_io_error(pn_reactor_io(reactor)))); pn_transport_close_tail(transport); pn_transport_close_head(transport); } pn_free(str); pn_reactor_selectable_transport(reactor, sock, transport); }