unit.cpp (1536:8d1511734750) unit.cpp (1543:42f27153db91)
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
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 nxt_nodejs_ctx_t {
21struct port_data_t {
22 nxt_unit_ctx_t *ctx;
22 nxt_unit_port_id_t port_id;
23 uv_poll_t poll;
24};
25
26
27struct req_data_t {
28 napi_ref sock_ref;
29 napi_ref resp_ref;

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

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

--- 585 unchanged lines hidden ---
440}
441
442
443void
444Unit::quit_cb(nxt_unit_ctx_t *ctx)
445{
446 Unit *obj;
447

--- 585 unchanged lines hidden ---