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 #ifndef FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H 17 #define FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H 18 19 #include "event_target.h" 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "securec.h" 24 25 #include "uv.h" 26 27 #define EVENT_TYPE_BUFFER_SIZE 64 28 29 struct WriteReq { 30 uv_write_t req = { 0 }; 31 uv_buf_t buf = { 0 }; 32 }; 33 34 class NetServerEvent { 35 public: 36 NetServerEvent(napi_env env, const char* type, char* buffer = nullptr, size_t length = 0) 37 { 38 env_ = env; 39 40 (void)strncpy_s(type_, EVENT_TYPE_BUFFER_SIZE, type, strlen(type)); 41 42 buffer_ = buffer; 43 length_ = length; 44 } 45 ~NetServerEvent()46 virtual ~NetServerEvent() {} 47 ToJsObject()48 virtual napi_value ToJsObject() 49 { 50 napi_value result; 51 napi_create_object(env_, &result); 52 53 napi_value type; 54 napi_create_string_utf8(env_, type_, strlen(type_), &type); 55 napi_set_named_property(env_, result, "type", type); 56 57 if (length_ > 0) { 58 napi_value data; 59 napi_create_string_utf8(env_, buffer_, length_, &data); 60 napi_set_named_property(env_, result, "data", data); 61 } 62 63 return result; 64 } 65 66 private: 67 napi_env env_; 68 69 char type_[EVENT_TYPE_BUFFER_SIZE]; 70 char* buffer_; 71 size_t length_; 72 }; 73 74 struct NetClient { 75 uv_tcp_t tcp = { 0 }; 76 uv_write_t req = { 0 }; 77 uv_buf_t buf = { 0 }; 78 NetClient* next = nullptr; 79 }; 80 81 class NetServer : public EventTarget { 82 NetServer(napi_env env, napi_value thisVar); 83 virtual ~NetServer(); 84 85 public: 86 int Start(int port); 87 void Stop(); 88 89 static napi_value Export(napi_env env, napi_value exports); 90 91 private: 92 // Napi methods and properties 93 static napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo); 94 static napi_value JS_Start(napi_env env, napi_callback_info cbinfo); 95 static napi_value JS_Stop(napi_env env, napi_callback_info cbinfo); 96 static napi_value JS_On(napi_env env, napi_callback_info cbinfo); 97 static napi_value JS_Once(napi_env env, napi_callback_info cbinfo); 98 static napi_value JS_Off(napi_env env, napi_callback_info cbinfo); 99 100 // C function and members 101 static void EchoAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf); 102 static void AfterShutdown(uv_shutdown_t* req, int status); 103 static void AfterWrite(uv_write_t* req, int status); 104 static void AfterRead(uv_stream_t*, ssize_t nread, const uv_buf_t* buf); 105 static void TakeDiffactionsByStatus(uv_stream_t* handle, ssize_t nread, 106 const uv_buf_t* buf, NetServer* that); 107 static void OnClose(uv_handle_t* peer); 108 static void OnServerClose(uv_handle_t* handle); 109 static void OnConnection(uv_stream_t*, int status); 110 111 uv_loop_t* loop_; 112 int serverClosed_; 113 uv_tcp_t tcpServer_; 114 NetClient* clients_; 115 }; 116 117 #endif /* FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H */ 118