summaryrefslogtreecommitdiffhomepage
path: root/src/go/unit/nxt_go_lib.c
blob: 4455e7832d92b1c61dbfc4a48cb40f97e2ac2cc5 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

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

#include "nxt_go_run_ctx.h"
#include "nxt_go_log.h"
#include "nxt_go_port.h"

#include "_cgo_export.h"

#include <nxt_main.h>

int
nxt_go_response_write(nxt_go_request_t r, uintptr_t buf, size_t len)
{
    nxt_int_t         rc;
    nxt_go_run_ctx_t  *ctx;

    if (nxt_slow_path(r == 0)) {
        return 0;
    }

    nxt_go_debug("write: %d", (int) len);

    ctx = (nxt_go_run_ctx_t *) r;
    rc = nxt_go_ctx_write(ctx, (void *) buf, len);

    return rc == NXT_OK ? len : -1;
}


int
nxt_go_request_read(nxt_go_request_t r, uintptr_t dst, size_t dst_len)
{
    size_t            res;
    nxt_go_run_ctx_t  *ctx;

    if (nxt_slow_path(r == 0)) {
        return 0;
    }

    ctx = (nxt_go_run_ctx_t *) r;

    dst_len = nxt_min(dst_len, ctx->request.body.preread_size);

    res = nxt_go_ctx_read_raw(ctx, (void *) dst, dst_len);

    ctx->request.body.preread_size -= res;

    return res;
}


int
nxt_go_request_close(nxt_go_request_t r)
{
    return 0;
}


int
nxt_go_request_done(nxt_go_request_t r)
{
    nxt_int_t          res;
    nxt_go_run_ctx_t   *ctx;
    nxt_go_msg_t       *msg, *b;

    if (nxt_slow_path(r == 0)) {
        return 0;
    }

    ctx = (nxt_go_run_ctx_t *) r;

    res = nxt_go_ctx_flush(ctx, 1);

    nxt_go_ctx_release_msg(ctx, &ctx->msg);

    msg = ctx->msg.next;
    while (msg != NULL) {
        nxt_go_ctx_release_msg(ctx, msg);

        b = msg;
        msg = b->next;

        free(b);
    }

    free(ctx);

    return res;
}


void
nxt_go_ready(uint32_t stream)
{
    nxt_port_msg_t  port_msg;

    port_msg.stream = stream;
    port_msg.pid = getpid();
    port_msg.reply_port = 0;
    port_msg.type = _NXT_PORT_MSG_PROCESS_READY;
    port_msg.last = 1;
    port_msg.mmap = 0;
    port_msg.nf = 0;
    port_msg.mf = 0;
    port_msg.tracking = 0;

    nxt_go_main_send(&port_msg, sizeof(port_msg), NULL, 0);
}


nxt_go_request_t
nxt_go_process_port_msg(uintptr_t buf, size_t buf_len, uintptr_t oob, size_t oob_len)
{
    return nxt_go_port_on_read((void *) buf, buf_len, (void *) oob, oob_len);
}


const char *
nxt_go_version()
{
    return NXT_VERSION;
}