summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_router.c
blob: caef8503f27dfcb3b97a96a337c02d12d3200d3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Valentin V. Bartenev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <nxt_runtime.h>
#include <nxt_master_process.h>


static nxt_int_t nxt_router_listen_socket(nxt_task_t *task, nxt_runtime_t *rt);


nxt_int_t
nxt_router_start(nxt_task_t *task, nxt_runtime_t *rt)
{
    if (nxt_router_listen_socket(task, rt) != NXT_OK) {
        return NXT_ERROR;
    }

    return NXT_OK;
}


static nxt_int_t
nxt_router_listen_socket(nxt_task_t *task, nxt_runtime_t *rt)
{
    nxt_sockaddr_t       *sa;
    nxt_listen_socket_t  *ls;

    sa = nxt_sockaddr_alloc(rt->mem_pool, sizeof(struct sockaddr_in),
                            NXT_INET_ADDR_STR_LEN);
    if (sa == NULL) {
        return NXT_ERROR;
    }

    sa->type = SOCK_STREAM;
    sa->u.sockaddr_in.sin_family = AF_INET;
    sa->u.sockaddr_in.sin_port = htons(8000);

    nxt_sockaddr_text(sa);

    ls = nxt_runtime_listen_socket_add(rt, sa);
    if (ls == NULL) {
        return NXT_ERROR;
    }

    ls->read_after_accept = 1;

    ls->flags = NXT_NONBLOCK;

#if 0
    /* STUB */
    wq = nxt_mem_zalloc(cf->mem_pool, sizeof(nxt_work_queue_t));
    if (wq == NULL) {
        return NXT_ERROR;
    }
    nxt_work_queue_name(wq, "listen");
    /**/

    ls->work_queue = wq;
#endif
    ls->handler = nxt_stream_connection_init;

    /*
     * Connection memory pool chunk size is tunned to
     * allocate the most data in one mem_pool chunk.
     */
    ls->mem_pool_size = nxt_listen_socket_pool_min_size(ls)
                        + sizeof(nxt_event_conn_proxy_t)
                        + sizeof(nxt_event_conn_t)
                        + 4 * sizeof(nxt_buf_t);

    if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
        return NXT_ERROR;
    }

    if (nxt_event_conn_listen(task, ls) != NXT_OK) {
        return NXT_ERROR;
    }

    return NXT_OK;
}