Deleted Added
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include "unit.h"
7
8#include <unistd.h>

--- 4 unchanged lines hidden (view full) ---

13#include <nxt_unit_websocket.h>
14
15
16static void delete_port_data(uv_handle_t* handle);
17
18napi_ref Unit::constructor_;
19
20
21struct port_data_t {
22 nxt_unit_ctx_t *ctx;
23 nxt_unit_port_id_t port_id;
24 uv_poll_t poll;
25};
26
27
28struct req_data_t {
29 napi_ref sock_ref;
30 napi_ref resp_ref;

--- 325 unchanged lines hidden (view full) ---

356
357
358int
359Unit::add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port)
360{
361 int err;
362 Unit *obj;
363 uv_loop_t *loop;
364 port_data_t *data;
365 napi_status status;
366
367 if (port->in_fd != -1) {
368 obj = reinterpret_cast<Unit *>(ctx->unit->data);
369
370 if (fcntl(port->in_fd, F_SETFL, O_NONBLOCK) == -1) {
371 nxt_unit_warn(ctx, "fcntl(%d, O_NONBLOCK) failed: %s (%d)",
372 port->in_fd, strerror(errno), errno);
373 return -1;
374 }
375
376 status = napi_get_uv_event_loop(obj->env(), &loop);
377 if (status != napi_ok) {
378 nxt_unit_warn(ctx, "Failed to get uv.loop");
379 return NXT_UNIT_ERROR;
380 }
381
382 data = new port_data_t;
383
384 err = uv_poll_init(loop, &data->poll, port->in_fd);
385 if (err < 0) {
386 nxt_unit_warn(ctx, "Failed to init uv.poll");
387 return NXT_UNIT_ERROR;
388 }
389
390 err = uv_poll_start(&data->poll, UV_READABLE, nxt_uv_read_callback);
391 if (err < 0) {
392 nxt_unit_warn(ctx, "Failed to start uv.poll");
393 return NXT_UNIT_ERROR;
394 }
395
396 port->data = data;
397
398 data->ctx = ctx;
399 data->port_id = port->id;
400 data->poll.data = ctx;
401 }
402
403 return NXT_UNIT_OK;
404}
405
406
407inline bool
408operator == (const nxt_unit_port_id_t &p1, const nxt_unit_port_id_t &p2)
409{
410 return p1.pid == p2.pid && p1.id == p2.id;
411}
412
413
414void
415Unit::remove_port(nxt_unit_t *unit, nxt_unit_port_t *port)
416{
417 port_data_t *data;
418
419 if (port->data != NULL) {
420 data = (port_data_t *) port->data;
421
422 if (data->port_id == port->id) {
423 uv_poll_stop(&data->poll);
424
425 data->poll.data = data;
426 uv_close((uv_handle_t *) &data->poll, delete_port_data);
427 }
428 }
429}
430
431
432static void
433delete_port_data(uv_handle_t* handle)
434{
435 port_data_t *data;
436
437 data = (port_data_t *) handle->data;
438
439 delete data;
440}
441
442
443void
444Unit::quit_cb(nxt_unit_ctx_t *ctx)
445{
446 Unit *obj;
447

--- 585 unchanged lines hidden ---