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     auto status = 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     if (status != napi_ok) {
37         delete netServer;
38     }
39 
40     return thisVar;
41 }
42 
JS_Start(napi_env env,napi_callback_info cbinfo)43 napi_value NetServer::JS_Start(napi_env env, napi_callback_info cbinfo)
44 {
45     size_t requireArgc = 1;
46     size_t argc = 1;
47     napi_value argv[1] = { 0 };
48     napi_value thisVar = nullptr;
49     void* data = nullptr;
50     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
51 
52     NetServer* netServer = nullptr;
53     napi_unwrap(env, thisVar, (void**)&netServer);
54 
55     NAPI_ASSERT(env, argc >= requireArgc, "requires 1 parameter");
56 
57     napi_valuetype valueType = napi_undefined;
58     napi_typeof(env, argv[0], &valueType);
59     NAPI_ASSERT(env, valueType == napi_number, "type mismatch for parameter 1");
60 
61     int32_t port = 0;
62     napi_get_value_int32(env, argv[0], &port);
63 
64     netServer->Start(port);
65 
66     napi_value result = nullptr;
67     napi_get_undefined(env, &result);
68     return result;
69 }
70 
JS_Stop(napi_env env,napi_callback_info cbinfo)71 napi_value NetServer::JS_Stop(napi_env env, napi_callback_info cbinfo)
72 {
73     size_t argc = 1;
74     napi_value argv[1] = { 0 };
75     napi_value thisVar = nullptr;
76     void* data = nullptr;
77     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
78 
79     NetServer* netServer = nullptr;
80     napi_unwrap(env, thisVar, (void**)&netServer);
81 
82     netServer->Stop();
83 
84     napi_value result = nullptr;
85     napi_get_undefined(env, &result);
86     return result;
87 }
88 
JS_On(napi_env env,napi_callback_info cbinfo)89 napi_value NetServer::JS_On(napi_env env, napi_callback_info cbinfo)
90 {
91     size_t requireArgc = 2;
92     size_t argc = 2;
93     napi_value argv[2] = { 0 };
94     napi_value thisVar = 0;
95     void* data = nullptr;
96     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
97 
98     NetServer* netServer = nullptr;
99     napi_unwrap(env, thisVar, (void**)&netServer);
100 
101     NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
102 
103     napi_valuetype eventValueType = napi_undefined;
104     napi_typeof(env, argv[0], &eventValueType);
105     NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
106 
107     napi_valuetype eventHandleType = napi_undefined;
108     napi_typeof(env, argv[1], &eventHandleType);
109     NAPI_ASSERT(env, eventHandleType == napi_function, "type mismatch for parameter 2");
110 
111     char type[64] = { 0 };
112     size_t typeLen = 0;
113 
114     napi_get_value_string_utf8(env, argv[0], type, sizeof(type), &typeLen);
115 
116     netServer->On((const char*)type, argv[1]);
117 
118     napi_value result = nullptr;
119     napi_get_undefined(env, &result);
120     return result;
121 }
122 
JS_Once(napi_env env,napi_callback_info cbinfo)123 napi_value NetServer::JS_Once(napi_env env, napi_callback_info cbinfo)
124 {
125     size_t requireArgc = 2;
126     size_t argc = 2;
127     napi_value argv[2] = { 0 };
128     napi_value thisVar = 0;
129     void* data = nullptr;
130     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
131 
132     NetServer* netServer = nullptr;
133     napi_unwrap(env, thisVar, (void**)&netServer);
134 
135     NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
136 
137     napi_valuetype eventValueType = napi_undefined;
138     napi_typeof(env, argv[0], &eventValueType);
139     NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
140 
141     napi_valuetype eventHandleType = napi_undefined;
142     napi_typeof(env, argv[1], &eventHandleType);
143     NAPI_ASSERT(env, eventValueType == napi_function, "type mismatch for parameter 2");
144 
145     char* type = nullptr;
146     size_t typeLen = 0;
147     napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeLen);
148 
149     NAPI_ASSERT(env, typeLen > 0, "typeLen == 0");
150     type = new char[typeLen + 1];
151     napi_get_value_string_utf8(env, argv[0], type, typeLen + 1, &typeLen);
152 
153     netServer->Once(type, argv[1]);
154 
155     delete[] type;
156 
157     napi_value result = nullptr;
158     napi_get_undefined(env, &result);
159     return result;
160 }
161 
JS_Off(napi_env env,napi_callback_info cbinfo)162 napi_value NetServer::JS_Off(napi_env env, napi_callback_info cbinfo)
163 {
164     size_t requireArgc = 1;
165     size_t argc = 2;
166     napi_value argv[2] = { 0 };
167     napi_value thisVar = 0;
168     void* data = nullptr;
169     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, &data);
170 
171     NetServer* netServer = nullptr;
172     napi_unwrap(env, thisVar, (void**)&netServer);
173 
174     NAPI_ASSERT(env, argc >= requireArgc, "requires 2 parameter");
175 
176     napi_valuetype eventValueType = napi_undefined;
177     napi_typeof(env, argv[0], &eventValueType);
178     NAPI_ASSERT(env, eventValueType == napi_string, "type mismatch for parameter 1");
179 
180     napi_valuetype eventHandleType = napi_undefined;
181     napi_typeof(env, argv[1], &eventHandleType);
182     NAPI_ASSERT(env, eventValueType == napi_function, "type mismatch for parameter 2");
183 
184     char* type = nullptr;
185     size_t typeLen = 0;
186     napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeLen);
187 
188     NAPI_ASSERT(env, typeLen > 0, "typeLen == 0");
189     type = new char[typeLen + 1];
190 
191     napi_get_value_string_utf8(env, argv[0], type, typeLen + 1, &typeLen);
192 
193     if (argc > requireArgc) {
194         netServer->Off(type, argv[1]);
195     } else {
196         netServer->Off(type);
197     }
198     delete[] type;
199     napi_value result = nullptr;
200     napi_get_undefined(env, &result);
201     return result;
202 }
203 
Export(napi_env env,napi_value exports)204 napi_value NetServer::Export(napi_env env, napi_value exports)
205 {
206     const char className[] = "NetServer";
207     napi_property_descriptor properties[] = {
208         DECLARE_NAPI_FUNCTION("start", JS_Start),
209         DECLARE_NAPI_FUNCTION("stop", JS_Stop),
210         DECLARE_NAPI_FUNCTION("on", JS_On),
211         DECLARE_NAPI_FUNCTION("once", JS_Once),
212         DECLARE_NAPI_FUNCTION("off", JS_Off),
213     };
214     napi_value netServerClass = nullptr;
215 
216     napi_define_class(env, className, sizeof(className), JS_Constructor, nullptr,
217         sizeof(properties) / sizeof(properties[0]), properties, &netServerClass);
218 
219     napi_set_named_property(env, exports, "NetServer", netServerClass);
220 
221     return exports;
222 }
223