summaryrefslogtreecommitdiffhomepage
path: root/go/nxt_cgo_lib.c
blob: 330697c181ea7d0d70355a161f37f3dfce3a2294 (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
86
87
88
89
90
91
92
93
94

/*
 * Copyright (C) Max Romanov
 * Copyright (C) NGINX, Inc.
 */

#include "_cgo_export.h"

#include <nxt_unit.h>
#include <nxt_unit_request.h>


static ssize_t nxt_cgo_port_send(nxt_unit_ctx_t *, nxt_unit_port_t *port,
    const void *buf, size_t buf_size, const void *oob, size_t oob_size);
static ssize_t nxt_cgo_port_recv(nxt_unit_ctx_t *, nxt_unit_port_t *port,
    void *buf, size_t buf_size, void *oob, size_t oob_size);

int
nxt_cgo_run(uintptr_t handler)
{
    int              rc;
    nxt_unit_ctx_t   *ctx;
    nxt_unit_init_t  init;

    memset(&init, 0, sizeof(init));

    init.callbacks.request_handler = nxt_go_request_handler;
    init.callbacks.add_port        = nxt_go_add_port;
    init.callbacks.remove_port     = nxt_go_remove_port;
    init.callbacks.port_send       = nxt_cgo_port_send;
    init.callbacks.port_recv       = nxt_cgo_port_recv;
    init.callbacks.shm_ack_handler = nxt_go_shm_ack_handler;

    init.data = (void *) handler;

    ctx = nxt_unit_init(&init);
    if (ctx == NULL) {
        return NXT_UNIT_ERROR;
    }

    rc = nxt_unit_run_ctx(ctx);

    nxt_unit_done(ctx);

    return rc;
}


static ssize_t
nxt_cgo_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
    const void *buf, size_t buf_size, const void *oob, size_t oob_size)
{
    return nxt_go_port_send(port->id.pid, port->id.id,
                            (void *) buf, buf_size, (void *) oob, oob_size);
}


static ssize_t
nxt_cgo_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port,
    void *buf, size_t buf_size, void *oob, size_t oob_size)
{
    return nxt_go_port_recv(port->id.pid, port->id.id,
                            buf, buf_size, oob, oob_size);
}


ssize_t
nxt_cgo_response_write(nxt_unit_request_info_t *req, uintptr_t start,
    uint32_t len)
{
    return nxt_unit_response_write_nb(req, (void *) start, len, 0);
}


ssize_t
nxt_cgo_request_read(nxt_unit_request_info_t *req, uintptr_t dst,
    uint32_t dst_len)
{
    return nxt_unit_request_read(req, (void *) dst, dst_len);
}


void
nxt_cgo_warn(const char *msg, uint32_t msg_len)
{
    nxt_unit_warn(NULL, "%.*s", (int) msg_len, (char *) msg);
}


void
nxt_cgo_alert(const char *msg, uint32_t msg_len)
{
    nxt_unit_alert(NULL, "%.*s", (int) msg_len, (char *) msg);
}