static void maybe_connect_some() { uv_connect_t* req; uv_tcp_t* tcp; uv_pipe_t* pipe; int r; while (max_connect_socket < TARGET_CONNECTIONS && max_connect_socket < write_sockets + MAX_SIMULTANEOUS_CONNECTS) { if (type == TCP) { tcp = &tcp_write_handles[max_connect_socket++]; r = uv_tcp_init(tcp); ASSERT(r == 0); req = (uv_connect_t*) req_alloc(); r = uv_tcp_connect(req, tcp, connect_addr, connect_cb); ASSERT(r == 0); } else { pipe = &pipe_write_handles[max_connect_socket++]; r = uv_pipe_init(pipe); ASSERT(r == 0); req = (uv_connect_t*) req_alloc(); r = uv_pipe_connect(req, pipe, TEST_PIPENAME, connect_cb); ASSERT(r == 0); } } }
static struct zdoor_result * zdoor_cb(struct zdoor_cookie *cookie, char *argp, size_t argp_sz) { struct door *d; struct req *r; ErlNifEnv *env = enif_alloc_env(); /* we kept the struct door in the biscuit */ d = (struct door *)cookie->zdc_biscuit; /* this request */ r = req_alloc(); /* take the rlist lock first, then the req lock */ enif_rwlock_rwlock(d->rlock); enif_mutex_lock(r->lock); req_insert(d, r); enif_rwlock_rwunlock(d->rlock); /* make the request into a binary term to put it into enif_send() */ ErlNifBinary bin; enif_alloc_binary(argp_sz, &bin); memcpy(bin.data, argp, argp_sz); ERL_NIF_TERM binTerm = enif_make_binary(env, &bin); /* send a message back to the session owner */ enif_send(NULL, &d->owner, env, enif_make_tuple3(env, enif_make_atom(env, "zdoor"), enif_make_resource(env, r), binTerm)); /* now wait until the request has been replied to */ enif_cond_wait(r->cond, r->lock); /* convert the reply into a zdoor_result */ /* we have to use naked malloc() since libzdoor will use free() */ struct zdoor_result *res = malloc(sizeof(struct zdoor_result)); res->zdr_size = r->replen; res->zdr_data = r->rep; r->rep = NULL; r->replen = 0; /* yes, we have to unlock and re-lock to avoid lock inversion here */ enif_mutex_unlock(r->lock); /* remove and free the struct req */ enif_rwlock_rwlock(d->rlock); enif_mutex_lock(r->lock); req_remove(d, r); enif_rwlock_rwunlock(d->rlock); req_free(r); enif_free_env(env); return res; }
static void start_stats_collection() { uv_req_t* req = req_alloc(); int r; /* Show-stats timer */ stats_left = STATS_COUNT; r = uv_timer_init(&timer_handle, close_cb, NULL); ASSERT(r == 0); r = uv_timer_start(&timer_handle, show_stats, STATS_INTERVAL, STATS_INTERVAL); ASSERT(r == 0); }
static void do_write(uv_stream_t* stream) { uv_write_t* req; uv_buf_t buf; int r; buf.base = (char*) &write_buffer; buf.len = sizeof write_buffer; req = (uv_write_t*) req_alloc(); r = uv_write(req, stream, &buf, 1, write_cb); ASSERT(r == 0); }
static void do_connect(uv_handle_t* handle, struct sockaddr* addr) { uv_req_t* req; int r; r = uv_tcp_init(handle, close_cb, NULL); ASSERT(r == 0); req = req_alloc(); uv_req_init(req, handle, connect_cb); r = uv_connect(req, addr); ASSERT(r == 0); }
// shutdown and close the client void client_finish(client_t *self) { assert(self); // sanity check if (self->closed) return; // shutdown UV stream, then close this client uv_shutdown_t *rq = (uv_shutdown_t *)req_alloc(NULL); rq->data = self; if (uv_shutdown(rq, (uv_stream_t *)&self->handle, client_after_shutdown)) { // on error still call callback with error status client_after_shutdown(rq, -1); } }
static void start_stats_collection() { uv_req_t* req = req_alloc(); int r; /* Show-stats timer */ stats_left = STATS_COUNT; r = uv_timer_init(&timer_handle); ASSERT(r == 0); r = uv_timer_start(&timer_handle, show_stats, STATS_INTERVAL, STATS_INTERVAL); ASSERT(r == 0); uv_update_time(); start_time = uv_now(); }
static void do_write(uv_handle_t* handle) { uv_req_t* req; uv_buf_t buf; int r; buf.base = (char*) &write_buffer; buf.len = sizeof write_buffer; while (handle->write_queue_size == 0) { req = req_alloc(); uv_req_init(req, handle, write_cb); r = uv_write(req, &buf, 1); ASSERT(r == 0); } }
// raw write to client stream void client_write(client_t *self, void *data, size_t len) { assert(self); if (self->closed) return; DEBUGF("WRITE %p %*s", self, len, data); uv_write_t *rq = (uv_write_t *)req_alloc(NULL); rq->data = self; uv_buf_t buf = uv_buf_init(data, len); if (uv_write(rq, (uv_stream_t *)&self->handle, &buf, 1, client_after_write)) { // on error still call callback with error status client_after_write(rq, -1); } }
static void maybe_connect_some() { uv_req_t* req; uv_tcp_t* tcp; int r; while (max_connect_socket < TARGET_CONNECTIONS && max_connect_socket < write_sockets + MAX_SIMULTANEOUS_CONNECTS) { tcp = &write_handles[max_connect_socket++]; r = uv_tcp_init(tcp); ASSERT(r == 0); req = req_alloc(); uv_req_init(req, (uv_handle_t*)tcp, connect_cb); r = uv_tcp_connect(req, connect_addr); ASSERT(r == 0); } }