forked from zeromq/zeromq.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incoming_msg.cc
63 lines (49 loc) · 1.83 KB
/
incoming_msg.cc
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
/* Copyright (c) 2017-2019 Rolf Timmermans */
#include "incoming_msg.h"
#include "util/error.h"
namespace zmq {
IncomingMsg::IncomingMsg() : ref(new Reference()) {}
IncomingMsg::~IncomingMsg() {
if (!moved && ref != nullptr) {
delete ref;
ref = nullptr;
}
}
Napi::Value IncomingMsg::IntoBuffer(const Napi::Env& env) {
if (moved) {
/* If ownership has been transferred, do not attempt to read the buffer
again in any case. This should not happen of course. */
ErrnoException(env, EINVAL).ThrowAsJavaScriptException();
return env.Undefined();
}
static auto constexpr zero_copy_threshold = 1 << 7;
auto data = reinterpret_cast<uint8_t*>(zmq_msg_data(*ref));
auto length = zmq_msg_size(*ref);
if (length > zero_copy_threshold) {
/* Reuse existing buffer for external storage. This avoids copying but
does include an overhead in having to call a finalizer when the
buffer is GC'ed. For very small messages it is faster to copy. */
moved = true;
/* Put appropriate GC pressure according to the size of the buffer. */
Napi::MemoryManagement::AdjustExternalMemory(env, length);
auto release = [](const Napi::Env& env, uint8_t*, Reference* ref) {
ptrdiff_t length = zmq_msg_size(*ref);
Napi::MemoryManagement::AdjustExternalMemory(env, -length);
delete ref;
};
return Napi::Buffer<uint8_t>::New(env, data, length, release, ref);
}
if (length > 0) {
return Napi::Buffer<uint8_t>::Copy(env, data, length);
}
return Napi::Buffer<uint8_t>::New(env, 0);
}
IncomingMsg::Reference::Reference() {
auto err = zmq_msg_init(&msg);
assert(err == 0);
}
IncomingMsg::Reference::~Reference() {
auto err = zmq_msg_close(&msg);
assert(err == 0);
}
}