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_NET_SERVER_NETSERVER_H
1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NET_SERVER_NETSERVER_H
1833eb0b6dSopenharmony_ci
1933eb0b6dSopenharmony_ci#include <cstddef>
2033eb0b6dSopenharmony_ci
2133eb0b6dSopenharmony_ci#include "event_target.h"
2233eb0b6dSopenharmony_ci#include "napi/native_api.h"
2333eb0b6dSopenharmony_ci#include "napi/native_node_api.h"
2433eb0b6dSopenharmony_ci#include "uv.h"
2533eb0b6dSopenharmony_ci
2633eb0b6dSopenharmony_ci#define EVENT_TYPE_BUFFER_SIZE 64
2733eb0b6dSopenharmony_ci
2833eb0b6dSopenharmony_cistruct WriteReq {
2933eb0b6dSopenharmony_ci    uv_write_t req = { 0 };
3033eb0b6dSopenharmony_ci    uv_buf_t buf = { 0 };
3133eb0b6dSopenharmony_ci};
3233eb0b6dSopenharmony_ci
3333eb0b6dSopenharmony_cistruct NetClient {
3433eb0b6dSopenharmony_ci    uv_tcp_t tcp = { 0 };
3533eb0b6dSopenharmony_ci    uv_write_t req = { 0 };
3633eb0b6dSopenharmony_ci    uv_buf_t buf = { 0 };
3733eb0b6dSopenharmony_ci    NetClient* next = nullptr;
3833eb0b6dSopenharmony_ci};
3933eb0b6dSopenharmony_ci
4033eb0b6dSopenharmony_ciclass NetServer : public EventTarget {
4133eb0b6dSopenharmony_ci    NetServer(napi_env env, napi_value thisVar);
4233eb0b6dSopenharmony_ci    virtual ~NetServer();
4333eb0b6dSopenharmony_ci
4433eb0b6dSopenharmony_cipublic:
4533eb0b6dSopenharmony_ci    int Start(int port);
4633eb0b6dSopenharmony_ci    void Stop();
4733eb0b6dSopenharmony_ci
4833eb0b6dSopenharmony_ci    static napi_value Export(napi_env env, napi_value exports);
4933eb0b6dSopenharmony_ci
5033eb0b6dSopenharmony_ciprivate:
5133eb0b6dSopenharmony_ci    // Napi methods and properties
5233eb0b6dSopenharmony_ci    static napi_value JS_Constructor(napi_env env, napi_callback_info cbinfo);
5333eb0b6dSopenharmony_ci    static napi_value JS_Start(napi_env env, napi_callback_info cbinfo);
5433eb0b6dSopenharmony_ci    static napi_value JS_Stop(napi_env env, napi_callback_info cbinfo);
5533eb0b6dSopenharmony_ci    static napi_value JS_On(napi_env env, napi_callback_info cbinfo);
5633eb0b6dSopenharmony_ci    static napi_value JS_Once(napi_env env, napi_callback_info cbinfo);
5733eb0b6dSopenharmony_ci    static napi_value JS_Off(napi_env env, napi_callback_info cbinfo);
5833eb0b6dSopenharmony_ci
5933eb0b6dSopenharmony_ci    // C function and members
6033eb0b6dSopenharmony_ci    static void EchoAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf);
6133eb0b6dSopenharmony_ci    static void AfterShutdown(uv_shutdown_t* req, int status);
6233eb0b6dSopenharmony_ci    static void AfterWrite(uv_write_t* req, int status);
6333eb0b6dSopenharmony_ci    static void AfterRead(uv_stream_t*, ssize_t nread, const uv_buf_t* buf);
6433eb0b6dSopenharmony_ci    static void TakeDiffactionsByStatus(uv_stream_t* handle, ssize_t nread,
6533eb0b6dSopenharmony_ci                                        const uv_buf_t* buf, NetServer* that);
6633eb0b6dSopenharmony_ci    static void OnClose(uv_handle_t* peer);
6733eb0b6dSopenharmony_ci    static void OnServerClose(uv_handle_t* handle);
6833eb0b6dSopenharmony_ci    static void OnConnection(uv_stream_t*, int status);
6933eb0b6dSopenharmony_ci
7033eb0b6dSopenharmony_ci    uv_loop_t* loop_;
7133eb0b6dSopenharmony_ci    int serverClosed_;
7233eb0b6dSopenharmony_ci    uv_tcp_t tcpServer_;
7333eb0b6dSopenharmony_ci    NetClient* clients_;
7433eb0b6dSopenharmony_ci};
7533eb0b6dSopenharmony_ci
7633eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_TEST_NATIVE_MODULE_NET_SERVER_NETSERVER_H */
77