-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPacket.cpp
More file actions
86 lines (67 loc) · 2.74 KB
/
Packet.cpp
File metadata and controls
86 lines (67 loc) · 2.74 KB
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
#include "Packet.hpp"
#include <stdexcept>
namespace nbs {
Packet Packet::FromJsValue(const Napi::Value& jsPacket, const Napi::Env& env) {
if (!jsPacket.IsObject()) {
throw std::runtime_error("expected packet object");
}
auto jsObject = jsPacket.As<Napi::Object>();
// Check keys are valid
if (!jsObject.Has("timestamp")) {
throw std::runtime_error("expected object with `timestamp` key");
}
if (!jsObject.Has("type")) {
throw std::runtime_error("expected object with `type` key");
}
if (!jsObject.Has("payload")) {
throw std::runtime_error("expected object with `payload` key");
}
uint32_t subtype = 0;
if (jsObject.Has("subtype")) {
if (jsObject.Get("subtype").IsNumber()) {
subtype = jsObject.Get("subtype").As<Napi::Number>().Uint32Value();
}
else if (!jsObject.Get("subtype").IsUndefined()) {
throw std::runtime_error("expected `subtype` to be a number");
}
}
uint64_t timestamp = 0;
try {
timestamp = timestamp::FromJsValue(jsObject.Get("timestamp"), env);
}
catch (const std::exception& ex) {
throw std::runtime_error(std::string("error in `timestamp`: ") + ex.what());
}
uint64_t type = 0;
try {
type = hash::FromJsValue(jsObject.Get("type"), env);
}
catch (const std::exception& ex) {
throw std::runtime_error(std::string("error in `type`: ") + ex.what());
}
if (!jsObject.Get("payload").IsBuffer()) {
throw std::runtime_error("expected `payload` to be buffer object");
}
auto payloadBuffer = jsObject.Get("payload").As<Napi::Buffer<uint8_t>>();
Packet packet;
packet.timestamp = timestamp;
packet.type = type;
packet.subtype = subtype;
packet.length = payloadBuffer.ByteLength();
packet.payload = payloadBuffer.Data(); // Pointer to external JS managed memory
return packet;
}
Napi::Value Packet::ToJsValue(const Packet& packet, const Napi::Env& env) {
auto jsPacket = Napi::Object::New(env);
jsPacket.Set("timestamp", timestamp::ToJsValue(packet.timestamp, env));
jsPacket.Set("type", hash::ToJsValue(packet.type, env));
jsPacket.Set("subtype", Napi::Number::New(env, packet.subtype));
if (packet.payload == nullptr) {
jsPacket.Set("payload", env.Undefined());
}
else {
jsPacket.Set("payload", Napi::Buffer<uint8_t>::Copy(env, packet.payload, packet.length));
}
return jsPacket;
}
} // namespace nbs