1 /*
2  * Copyright (c) 2024 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 ECMASCRIPT_TOOLING_CLIENT_TCPSERVER_H
17 #define ECMASCRIPT_TOOLING_CLIENT_TCPSERVER_H
18 
19 #include <algorithm>
20 #include <arpa/inet.h>
21 #include <cstring>
22 #include <iostream>
23 #include <mutex>
24 #include <pthread.h>
25 #include <securec.h>
26 #include <sstream>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <thread>
31 #include <unistd.h>
32 #include <uv.h>
33 #include <vector>
34 
35 #include "arkcompiler/toolchain/common/log_wrapper.h"
36 
37 namespace OHOS::ArkCompiler::Toolchain {
38 extern uv_async_t* g_inputSignal;
39 extern uv_async_t* g_releaseHandle;
40 class TcpServer {
41 public:
getInstance()42     static TcpServer& getInstance()
43     {
44         static TcpServer instance;
45         return instance;
46     }
47 
48     int CreateTcpServer([[maybe_unused]] void* arg);
49     void StartTcpServer([[maybe_unused]] void* arg);
50     void TcpServerWrite(std::string msg = "inner");
IsServerActive() const51     bool IsServerActive() const
52     {
53         return isServerActive;
54     }
55 
SetServerState(const bool state)56     void SetServerState(const bool state)
57     {
58         isServerActive = state;
59     }
60 
61 private:
62     TcpServer() = default;
63     TcpServer(const TcpServer&) = delete;
64     TcpServer& operator=(const TcpServer&) = delete;
65 
66     void SendCommand(std::string inputStr);
67     void CloseServer();
68     void ServerConnect();
69     bool FindCommand(std::vector<std::string> cmds, std::string cmd);
70 
71     uv_thread_t serverTid_ = 0;
72     bool isServerActive = false;
73     int lfd = -1;
74     int cfd = -1;
75 };
76 } // namespace OHOS::ArkCompiler::Toolchain
77 #endif // ECMASCRIPT_TOOLING_CLIENT_TCPSERVER_H