Repository navigation
Expand file tree
/
Copy pathplugin_entry.cpp
More file actions
145 lines (126 loc) · 4.74 KB
/
Copy pathplugin_entry.cpp
File metadata and controls
145 lines (126 loc) · 4.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0
/// @file plugins/links/portmap/plugin_entry.cpp
/// @brief Hand-rolled `gn_plugin_*` surface for the `gn.link.portmap`
/// extension. The extension shape does not match any of the
/// GN_*_PLUGIN macros (it is not a handler, link, or
/// strategy), so the entry is written directly.
#include <cstdint>
#include <new>
#include <sdk/abi.h>
#include <sdk/extensions/portmap.h>
#include <sdk/host_api.h>
#include <sdk/plugin.h>
#include <sdk/types.h>
#include "portmap.hpp"
namespace {
struct PortmapPluginInstance {
const host_api_t* api = nullptr;
void* host_ctx = nullptr;
gn::link::portmap::PortMap* impl = nullptr;
gn_portmap_api_t vtable{};
bool extension_registered = false;
};
int portmap_request_thunk(void* ctx,
gn_portmap_protocol_t protocol,
std::uint16_t int_port,
std::uint16_t external_port_hint,
std::uint32_t lifetime_s_hint,
gn_portmap_mapping_t* out_mapping) {
auto* p = static_cast<PortmapPluginInstance*>(ctx);
if (!p || !p->impl) return -1;
return p->impl->request(protocol, int_port, external_port_hint,
lifetime_s_hint, out_mapping);
}
int portmap_release_thunk(void* ctx,
gn_portmap_protocol_t protocol,
std::uint16_t int_port) {
auto* p = static_cast<PortmapPluginInstance*>(ctx);
if (!p || !p->impl) return -1;
return p->impl->release(protocol, int_port);
}
std::uint32_t portmap_supported_thunk(void* ctx) {
auto* p = static_cast<PortmapPluginInstance*>(ctx);
if (!p || !p->impl) return 0;
return p->impl->supported_protocols();
}
const char* const kProvides[] = {
GN_EXT_PORTMAP,
nullptr,
};
const gn_plugin_descriptor_t kDescriptor = {
/* name */ "goodnet_link_portmap",
/* version */ "0.1.0",
/* hot_reload_safe */ 0,
/* ext_requires */ nullptr,
/* ext_provides */ &kProvides[0],
/* kind */ GN_PLUGIN_KIND_UNKNOWN,
/* _reserved */ {nullptr, nullptr, nullptr, nullptr},
};
} // namespace
extern "C" {
GN_PLUGIN_EXPORT void GN_PLUGIN_SDK_VERSION_NAME(std::uint32_t* major,
std::uint32_t* minor,
std::uint32_t* patch) {
if (major) *major = GN_SDK_VERSION_MAJOR;
if (minor) *minor = GN_SDK_VERSION_MINOR;
if (patch) *patch = GN_SDK_VERSION_PATCH;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_INIT_NAME(const host_api_t* api,
void** out_self) {
if (!api || !out_self) return GN_ERR_NULL_ARG;
auto* p = new (std::nothrow) PortmapPluginInstance{};
if (!p) return GN_ERR_OUT_OF_MEMORY;
p->api = api;
p->host_ctx = api->host_ctx;
p->impl = new (std::nothrow) gn::link::portmap::PortMap();
if (!p->impl) {
delete p;
return GN_ERR_OUT_OF_MEMORY;
}
p->impl->set_host_api(api);
p->vtable.api_size = sizeof(gn_portmap_api_t);
p->vtable.request = &portmap_request_thunk;
p->vtable.release = &portmap_release_thunk;
p->vtable.supported_protocols = &portmap_supported_thunk;
p->vtable.ctx = p;
*out_self = p;
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_REGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<PortmapPluginInstance*>(self);
if (!p->api || !p->api->register_extension) {
return GN_ERR_NOT_IMPLEMENTED;
}
const gn_result_t rc = p->api->register_extension(
p->host_ctx,
GN_EXT_PORTMAP,
GN_EXT_PORTMAP_VERSION,
&p->vtable);
if (rc != GN_OK) return rc;
p->extension_registered = true;
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_UNREGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<PortmapPluginInstance*>(self);
if (p->extension_registered && p->api && p->api->unregister_extension) {
(void)p->api->unregister_extension(p->host_ctx, GN_EXT_PORTMAP);
p->extension_registered = false;
}
return GN_OK;
}
GN_PLUGIN_EXPORT void GN_PLUGIN_SHUTDOWN_NAME(void* self) {
if (!self) return;
auto* p = static_cast<PortmapPluginInstance*>(self);
if (p->impl) {
p->impl->shutdown();
delete p->impl;
p->impl = nullptr;
}
delete p;
}
GN_PLUGIN_EXPORT const gn_plugin_descriptor_t* GN_PLUGIN_DESCRIPTOR_NAME(void) {
return &kDescriptor;
}
} // extern "C"