int nn_cinproc_create (struct nn_ep *ep) { struct nn_cinproc *self; self = nn_alloc (sizeof (struct nn_cinproc), "cinproc"); alloc_assert (self); nn_ep_tran_setup (ep, &nn_cinproc_ops, self); nn_ins_item_init (&self->item, ep); nn_fsm_init_root (&self->fsm, nn_cinproc_handler, nn_cinproc_shutdown, nn_ep_getctx (ep)); self->state = NN_CINPROC_STATE_IDLE; nn_list_init (&self->sinprocs); nn_ep_stat_increment (ep, NN_STAT_INPROGRESS_CONNECTIONS, 1); /* Start the state machine. */ nn_fsm_start (&self->fsm); /* Register the inproc endpoint into a global repository. */ nn_ins_connect (&self->item, nn_cinproc_connect); return 0; }
int nn_binproc_create (struct nn_ep *ep) { int rc; struct nn_binproc *self; self = nn_alloc (sizeof (struct nn_binproc), "binproc"); alloc_assert (self); nn_ins_item_init (&self->item, ep); nn_fsm_init_root (&self->fsm, nn_binproc_handler, nn_binproc_shutdown, nn_ep_getctx (ep)); self->state = NN_BINPROC_STATE_IDLE; nn_list_init (&self->sinprocs); /* Start the state machine. */ nn_fsm_start (&self->fsm); /* Register the inproc endpoint into a global repository. */ rc = nn_ins_bind (&self->item, nn_binproc_connect); if (rc < 0) { nn_list_term (&self->sinprocs); /* TODO: Now, this is ugly! We are getting the state machine into the idle state manually. How should it be done correctly? */ self->fsm.state = 1; nn_fsm_term (&self->fsm); nn_ins_item_term (&self->item); nn_free (self); return rc; } nn_ep_tran_setup (ep, &nn_binproc_ops, self); return 0; }
int nn_cipc_create (struct nn_ep *ep) { struct nn_cipc *self; int reconnect_ivl; int reconnect_ivl_max; size_t sz; /* Allocate the new endpoint object. */ self = nn_alloc (sizeof (struct nn_cipc), "cipc"); alloc_assert (self); /* Initialise the structure. */ self->ep = ep; nn_ep_tran_setup (ep, &nn_cipc_ep_ops, self); nn_fsm_init_root (&self->fsm, nn_cipc_handler, nn_cipc_shutdown, nn_ep_getctx (ep)); self->state = NN_CIPC_STATE_IDLE; nn_usock_init (&self->usock, NN_CIPC_SRC_USOCK, &self->fsm); sz = sizeof (reconnect_ivl); nn_ep_getopt (ep, NN_SOL_SOCKET, NN_RECONNECT_IVL, &reconnect_ivl, &sz); nn_assert (sz == sizeof (reconnect_ivl)); sz = sizeof (reconnect_ivl_max); nn_ep_getopt (ep, NN_SOL_SOCKET, NN_RECONNECT_IVL_MAX, &reconnect_ivl_max, &sz); nn_assert (sz == sizeof (reconnect_ivl_max)); if (reconnect_ivl_max == 0) reconnect_ivl_max = reconnect_ivl; nn_backoff_init (&self->retry, NN_CIPC_SRC_RECONNECT_TIMER, reconnect_ivl, reconnect_ivl_max, &self->fsm); nn_sipc_init (&self->sipc, NN_CIPC_SRC_SIPC, ep, &self->fsm); /* Start the state machine. */ nn_fsm_start (&self->fsm); return 0; }
struct nn_ctx *nn_epbase_getctx (struct nn_epbase *self) { return nn_ep_getctx (self->ep); }
int nn_cws_create (struct nn_ep *ep) { int rc; const char *addr; size_t addrlen; const char *semicolon; const char *hostname; size_t hostlen; const char *colon; const char *slash; const char *resource; size_t resourcelen; struct sockaddr_storage ss; size_t sslen; int ipv4only; size_t ipv4onlylen; struct nn_cws *self; int reconnect_ivl; int reconnect_ivl_max; int msg_type; size_t sz; /* Allocate the new endpoint object. */ self = nn_alloc (sizeof (struct nn_cws), "cws"); alloc_assert (self); self->ep = ep; /* Initalise the endpoint. */ nn_ep_tran_setup (ep, &nn_cws_ep_ops, self); /* Check whether IPv6 is to be used. */ ipv4onlylen = sizeof (ipv4only); nn_ep_getopt (ep, NN_SOL_SOCKET, NN_IPV4ONLY, &ipv4only, &ipv4onlylen); nn_assert (ipv4onlylen == sizeof (ipv4only)); /* Start parsing the address. */ addr = nn_ep_getaddr (ep); addrlen = strlen (addr); semicolon = strchr (addr, ';'); hostname = semicolon ? semicolon + 1 : addr; colon = strrchr (addr, ':'); slash = colon ? strchr (colon, '/') : strchr (addr, '/'); resource = slash ? slash : addr + addrlen; self->remote_hostname_len = colon ? colon - hostname : resource - hostname; /* Host contains both hostname and port. */ hostlen = resource - hostname; /* Parse the port; assume port 80 if not explicitly declared. */ if (colon != NULL) { rc = nn_port_resolve (colon + 1, resource - colon - 1); if (rc < 0) { return -EINVAL; } self->remote_port = rc; } else { self->remote_port = 80; } /* Check whether the host portion of the address is either a literal or a valid hostname. */ if (nn_dns_check_hostname (hostname, self->remote_hostname_len) < 0 && nn_literal_resolve (hostname, self->remote_hostname_len, ipv4only, &ss, &sslen) < 0) { return -EINVAL; } /* If local address is specified, check whether it is valid. */ if (semicolon) { rc = nn_iface_resolve (addr, semicolon - addr, ipv4only, &ss, &sslen); if (rc < 0) { return -ENODEV; } } /* At this point, the address is valid, so begin allocating resources. */ nn_chunkref_init (&self->remote_host, hostlen + 1); memcpy (nn_chunkref_data (&self->remote_host), hostname, hostlen); ((uint8_t *) nn_chunkref_data (&self->remote_host)) [hostlen] = '\0'; if (semicolon) { nn_chunkref_init (&self->nic, semicolon - addr); memcpy (nn_chunkref_data (&self->nic), addr, semicolon - addr); } else { nn_chunkref_init (&self->nic, 1); memcpy (nn_chunkref_data (&self->nic), "*", 1); } /* The requested resource is used in opening handshake. */ resourcelen = strlen (resource); if (resourcelen) { nn_chunkref_init (&self->resource, resourcelen + 1); strncpy (nn_chunkref_data (&self->resource), resource, resourcelen + 1); } else { /* No resource specified, so allocate base path. */ nn_chunkref_init (&self->resource, 2); strncpy (nn_chunkref_data (&self->resource), "/", 2); } /* Initialise the structure. */ nn_fsm_init_root (&self->fsm, nn_cws_handler, nn_cws_shutdown, nn_ep_getctx (ep)); self->state = NN_CWS_STATE_IDLE; nn_usock_init (&self->usock, NN_CWS_SRC_USOCK, &self->fsm); sz = sizeof (msg_type); nn_ep_getopt (ep, NN_WS, NN_WS_MSG_TYPE, &msg_type, &sz); nn_assert (sz == sizeof (msg_type)); self->msg_type = (uint8_t) msg_type; sz = sizeof (reconnect_ivl); nn_ep_getopt (ep, NN_SOL_SOCKET, NN_RECONNECT_IVL, &reconnect_ivl, &sz); nn_assert (sz == sizeof (reconnect_ivl)); sz = sizeof (reconnect_ivl_max); nn_ep_getopt (ep, NN_SOL_SOCKET, NN_RECONNECT_IVL_MAX, &reconnect_ivl_max, &sz); nn_assert (sz == sizeof (reconnect_ivl_max)); if (reconnect_ivl_max == 0) reconnect_ivl_max = reconnect_ivl; nn_backoff_init (&self->retry, NN_CWS_SRC_RECONNECT_TIMER, reconnect_ivl, reconnect_ivl_max, &self->fsm); nn_sws_init (&self->sws, NN_CWS_SRC_SWS, ep, &self->fsm); nn_dns_init (&self->dns, NN_CWS_SRC_DNS, &self->fsm); /* Start the state machine. */ nn_fsm_start (&self->fsm); return 0; }