1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstddef>
17 #include <cstdint>
18
19 #include "net_server.h"
20
JS_Constructor(napi_env env, napi_callback_info cbinfo)21 napi_value NetServer::JS_Constructor(napi_env env, napi_callback_info cbinfo)
22 {
23 napi_value thisVar = nullptr;
24 void* data = nullptr;
25 napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data);
26
27 NetServer* netServer = new NetServer(env, thisVar);
28
29 napi_wrap(
30 env, thisVar, netServer,
31 [](napi_env env, void* data, void* hint) {
32 NetServer* netServer = (NetServer*)data;
33 delete netServer;
34 },
35 nullptr, nullptr);
36
37 return thisVar;
38 }
39
JS_Start(napi_env env, napi_callback_info cbinfo)40 napi_value NetServer::JS_Start(napi_env env, napi_callback_info cbinfo)
41 {
42 size_t requireArgc = 1;
43 size_t argc = 1;
44 napi_value argv[1] = { 0 };
45 napi_value thisVar = nullptr;
46 void* data = nullptr;
47 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
48
49 NetServer* netServer = nullptr;
50 napi_unwrap(env, thisVar, (void**)&netServer);
51
52 NAPI_ASSERT(env, argc >= requireArgc, "requires 1 parameter");
53
54 napi_valuetype valueType = napi_undefined;
55 napi_typeof(env, argv[0], &valueType);
56 NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
57
58 int32_t port = 0;
59 napi_get_value_int32(env, argv[0], &port);
60
61 netServer->Start(port);
62
63 napi_value result = nullptr;
64 napi_get_undefined(env, &result);
65 return result;
66 }
67
JS_Stop(napi_env env, napi_callback_info cbinfo)68 napi_value NetServer::JS_Stop(napi_env env, napi_callback_info cbinfo)
69 {
70 size_t argc = 1;
71 napi_value argv[1] = { 0 };
72 napi_value thisVar = nullptr;
73 void* data = nullptr;
74 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
75
76 NetServer* netServer = nullptr;
77 napi_unwrap(env, thisVar, (void**)&netServer);
78
79 netServer->Stop();
80
81 napi_value result = nullptr;
82 napi_get_undefined(env, &result);
83 return result;
84 }
85
JS_On(napi_env env, napi_callback_info cbinfo)86 napi_value NetServer::JS_On(napi_env env, napi_callback_info cbinfo)
87 {
88 size_t requireArgc = 2;
89 size_t argc = 2;
90 napi_value argv[2] = { 0 };
91 napi_value thisVar = 0;
92 void* data = nullptr;
93 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
94
95 NetServer* netServer = nullptr;
96 napi_unwrap(env, thisVar, (void**)&netServer);
97
98 NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
99
100 napi_valuetype eventValueType = napi_undefined;
101 napi_typeof(env, argv[0], &eventValueType);
102 NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
103
104 napi_valuetype eventHandleType = napi_undefined;
105 napi_typeof(env, argv[1], &eventHandleType);
106 NAPI_ASSERT(env, eventHandleType == napi_function, "type mismatch for parameter 2");
107
108 char type[64] = { 0 };
109 size_t typeLen = 0;
110
111 napi_get_value_string_utf8(env, argv[0], type, sizeof(type), &typeLen);
112
113 netServer->On((const char*)type, argv[1]);
114
115 napi_value result = nullptr;
116 napi_get_undefined(env, &result);
117 return result;
118 }
119
JS_Once(napi_env env, napi_callback_info cbinfo)120 napi_value NetServer::JS_Once(napi_env env, napi_callback_info cbinfo)
121 {
122 size_t requireArgc = 2;
123 size_t argc = 2;
124 napi_value argv[2] = { 0 };
125 napi_value thisVar = 0;
126 void* data = nullptr;
127 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
128
129 NetServer* netServer = nullptr;
130 napi_unwrap(env, thisVar, (void**)&netServer);
131
132 NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
133
134 napi_valuetype eventValueType = napi_undefined;
135 napi_typeof(env, argv[0], &eventValueType);
136 NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
137
138 napi_valuetype eventHandleType = napi_undefined;
139 napi_typeof(env, argv[1], &eventHandleType);
140 NAPI_ASSERT(env, eventValueType == napi_function, "type mismatch for parameter 2");
141
142 char* type = nullptr;
143 size_t typeLen = 0;
144 napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeLen);
145
146 NAPI_ASSERT(env, typeLen > 0, "typeLen == 0");
147 type = new char[typeLen + 1];
148 napi_get_value_string_utf8(env, argv[0], type, typeLen + 1, &typeLen);
149
150 netServer->Once(type, argv[1]);
151
152 delete[] type;
153
154 napi_value result = nullptr;
155 napi_get_undefined(env, &result);
156 return result;
157 }
158
JS_Off(napi_env env, napi_callback_info cbinfo)159 napi_value NetServer::JS_Off(napi_env env, napi_callback_info cbinfo)
160 {
161 size_t requireArgc = 1;
162 size_t argc = 2;
163 napi_value argv[2] = { 0 };
164 napi_value thisVar = 0;
165 void* data = nullptr;
166 napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
167
168 NetServer* netServer = nullptr;
169 napi_unwrap(env, thisVar, (void**)&netServer);
170
171 NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
172
173 napi_valuetype eventValueType = napi_undefined;
174 napi_typeof(env, argv[0], &eventValueType);
175 NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
176
177 napi_valuetype eventHandleType = napi_undefined;
178 napi_typeof(env, argv[1], &eventHandleType);
179 NAPI_ASSERT(env, eventValueType == napi_function, "type mismatch for parameter 2");
180
181 char* type = nullptr;
182 size_t typeLen = 0;
183 napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeLen);
184
185 NAPI_ASSERT(env, typeLen > 0, "typeLen == 0");
186 type = new char[typeLen + 1];
187
188 napi_get_value_string_utf8(env, argv[0], type, typeLen + 1, &typeLen);
189
190 if (argc > requireArgc) {
191 netServer->Off(type, argv[1]);
192 } else {
193 netServer->Off(type);
194 }
195 delete[] type;
196 napi_value result = nullptr;
197 napi_get_undefined(env, &result);
198 return result;
199 }
200
Export(napi_env env, napi_value exports)201 napi_value NetServer::Export(napi_env env, napi_value exports)
202 {
203 const char className[] = "NetServer";
204 napi_property_descriptor properties[] = {
205 DECLARE_NAPI_FUNCTION("start", JS_Start),
206 DECLARE_NAPI_FUNCTION("stop", JS_Stop),
207 DECLARE_NAPI_FUNCTION("on", JS_On),
208 DECLARE_NAPI_FUNCTION("once", JS_Once),
209 DECLARE_NAPI_FUNCTION("off", JS_Off),
210 };
211 napi_value netServerClass = nullptr;
212
213 napi_define_class(env, className, sizeof(className), JS_Constructor, nullptr,
214 sizeof(properties) / sizeof(properties[0]), properties, &netServerClass);
215
216 napi_set_named_property(env, exports, "NetServer", netServerClass);
217
218 return exports;
219 }
220