133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci 1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H 1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H 1833eb0b6dSopenharmony_ci 1933eb0b6dSopenharmony_ci#include "event_target.h" 2033eb0b6dSopenharmony_ci 2133eb0b6dSopenharmony_ci#include "napi/native_api.h" 2233eb0b6dSopenharmony_ci#include "napi/native_node_api.h" 2333eb0b6dSopenharmony_ci#include "securec.h" 2433eb0b6dSopenharmony_ci 2533eb0b6dSopenharmony_ci#include "uv.h" 2633eb0b6dSopenharmony_ci 2733eb0b6dSopenharmony_ci#define EVENT_TYPE_BUFFER_SIZE 64 2833eb0b6dSopenharmony_ci 2933eb0b6dSopenharmony_cistruct WriteReq { 3033eb0b6dSopenharmony_ci uv_write_t req = { 0 }; 3133eb0b6dSopenharmony_ci uv_buf_t buf = { 0 }; 3233eb0b6dSopenharmony_ci}; 3333eb0b6dSopenharmony_ci 3433eb0b6dSopenharmony_ciclass NetServerEvent { 3533eb0b6dSopenharmony_cipublic: 3633eb0b6dSopenharmony_ci NetServerEvent(napi_env env, const char* type, char* buffer = nullptr, size_t length = 0) 3733eb0b6dSopenharmony_ci { 3833eb0b6dSopenharmony_ci env_ = env; 3933eb0b6dSopenharmony_ci 4033eb0b6dSopenharmony_ci (void)strncpy_s(type_, EVENT_TYPE_BUFFER_SIZE, type, strlen(type)); 4133eb0b6dSopenharmony_ci 4233eb0b6dSopenharmony_ci buffer_ = buffer; 4333eb0b6dSopenharmony_ci length_ = length; 4433eb0b6dSopenharmony_ci } 4533eb0b6dSopenharmony_ci 4633eb0b6dSopenharmony_ci virtual ~NetServerEvent() {} 4733eb0b6dSopenharmony_ci 4833eb0b6dSopenharmony_ci virtual napi_value ToJsObject() 4933eb0b6dSopenharmony_ci { 5033eb0b6dSopenharmony_ci napi_value result; 5133eb0b6dSopenharmony_ci napi_create_object(env_, &result); 5233eb0b6dSopenharmony_ci 5333eb0b6dSopenharmony_ci napi_value type; 5433eb0b6dSopenharmony_ci napi_create_string_utf8(env_, type_, strlen(type_), &type); 5533eb0b6dSopenharmony_ci napi_set_named_property(env_, result, "type", type); 5633eb0b6dSopenharmony_ci 5733eb0b6dSopenharmony_ci if (length_ > 0) { 5833eb0b6dSopenharmony_ci napi_value data; 5933eb0b6dSopenharmony_ci napi_create_string_utf8(env_, buffer_, length_, &data); 6033eb0b6dSopenharmony_ci napi_set_named_property(env_, result, "data", data); 6133eb0b6dSopenharmony_ci } 6233eb0b6dSopenharmony_ci 6333eb0b6dSopenharmony_ci return result; 6433eb0b6dSopenharmony_ci } 6533eb0b6dSopenharmony_ci 6633eb0b6dSopenharmony_ciprivate: 6733eb0b6dSopenharmony_ci napi_env env_; 6833eb0b6dSopenharmony_ci 6933eb0b6dSopenharmony_ci char type_[EVENT_TYPE_BUFFER_SIZE]; 7033eb0b6dSopenharmony_ci char* buffer_; 7133eb0b6dSopenharmony_ci size_t length_; 7233eb0b6dSopenharmony_ci}; 7333eb0b6dSopenharmony_ci 7433eb0b6dSopenharmony_cistruct NetClient { 7533eb0b6dSopenharmony_ci uv_tcp_t tcp = { 0 }; 7633eb0b6dSopenharmony_ci uv_write_t req = { 0 }; 7733eb0b6dSopenharmony_ci uv_buf_t buf = { 0 }; 7833eb0b6dSopenharmony_ci NetClient* next = nullptr; 7933eb0b6dSopenharmony_ci}; 8033eb0b6dSopenharmony_ci 8133eb0b6dSopenharmony_ciclass NetServer : public EventTarget { 8233eb0b6dSopenharmony_ci NetServer(napi_env env, napi_value thisVar); 8333eb0b6dSopenharmony_ci virtual ~NetServer(); 8433eb0b6dSopenharmony_ci 8533eb0b6dSopenharmony_cipublic: 8633eb0b6dSopenharmony_ci int Start(int port); 8733eb0b6dSopenharmony_ci void Stop(); 8833eb0b6dSopenharmony_ci 8933eb0b6dSopenharmony_ci static napi_value Export(napi_env env, napi_value exports); 9033eb0b6dSopenharmony_ci 9133eb0b6dSopenharmony_ciprivate: 9233eb0b6dSopenharmony_ci // Napi methods and properties 9333eb0b6dSopenharmony_ci static napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo); 9433eb0b6dSopenharmony_ci static napi_value JS_Start(napi_env env, napi_callback_info cbinfo); 9533eb0b6dSopenharmony_ci static napi_value JS_Stop(napi_env env, napi_callback_info cbinfo); 9633eb0b6dSopenharmony_ci static napi_value JS_On(napi_env env, napi_callback_info cbinfo); 9733eb0b6dSopenharmony_ci static napi_value JS_Once(napi_env env, napi_callback_info cbinfo); 9833eb0b6dSopenharmony_ci static napi_value JS_Off(napi_env env, napi_callback_info cbinfo); 9933eb0b6dSopenharmony_ci 10033eb0b6dSopenharmony_ci // C function and members 10133eb0b6dSopenharmony_ci static void EchoAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf); 10233eb0b6dSopenharmony_ci static void AfterShutdown(uv_shutdown_t* req, int status); 10333eb0b6dSopenharmony_ci static void AfterWrite(uv_write_t* req, int status); 10433eb0b6dSopenharmony_ci static void AfterRead(uv_stream_t*, ssize_t nread, const uv_buf_t* buf); 10533eb0b6dSopenharmony_ci static void TakeDiffactionsByStatus(uv_stream_t* handle, ssize_t nread, 10633eb0b6dSopenharmony_ci const uv_buf_t* buf, NetServer* that); 10733eb0b6dSopenharmony_ci static void OnClose(uv_handle_t* peer); 10833eb0b6dSopenharmony_ci static void OnServerClose(uv_handle_t* handle); 10933eb0b6dSopenharmony_ci static void OnConnection(uv_stream_t*, int status); 11033eb0b6dSopenharmony_ci 11133eb0b6dSopenharmony_ci uv_loop_t* loop_; 11233eb0b6dSopenharmony_ci int serverClosed_; 11333eb0b6dSopenharmony_ci uv_tcp_t tcpServer_; 11433eb0b6dSopenharmony_ci NetClient* clients_; 11533eb0b6dSopenharmony_ci}; 11633eb0b6dSopenharmony_ci 11733eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NETSERVER_NETSERVER_H */ 118