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 SRC_INSPECTOR_SOCKET_SERVER_H_ 17 #define SRC_INSPECTOR_SOCKET_SERVER_H_ 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 #include "inspector_socket.h" 24 #include "jsvm_host_port.h" 25 #include "uv.h" 26 27 #ifndef ENABLE_INSPECTOR 28 #error("This header can only be used when inspector is enabled") 29 #endif 30 31 namespace jsvm { 32 namespace inspector { 33 34 std::string FormatWsAddress(const std::string& host, int port, const std::string& targetId, bool includeProtocol); 35 36 class InspectorSocketServer; 37 class SocketSession; 38 class ServerSocket; 39 40 class SocketServerDelegate { 41 public: 42 virtual ~SocketServerDelegate() = default; 43 44 virtual void AssignServer(InspectorSocketServer* server) = 0; 45 46 virtual void StartSession(int sessionId, const std::string& targetId) = 0; 47 48 virtual void EndSession(int sessionId) = 0; 49 50 virtual void MessageReceived(int sessionId, const std::string& message) = 0; 51 52 virtual std::vector<std::string> GetTargetIds() = 0; 53 54 virtual std::string GetTargetTitle(const std::string& id) = 0; 55 56 virtual std::string GetTargetUrl(const std::string& id) = 0; 57 }; 58 59 // HTTP Server, writes messages requested as TransportActions, and responds to HTTP requests and WS upgrades. 60 61 class InspectorSocketServer { 62 public: 63 InspectorSocketServer(std::unique_ptr<SocketServerDelegate> delegateParam, 64 uv_loop_t* loop, 65 const std::string& host, 66 int port, 67 const InspectPublishUid& inspectPublishUid, 68 FILE* out = stderr, 69 int pid = -1); 70 71 ~InspectorSocketServer(); 72 73 // Start listening on host/port 74 bool Start(); 75 76 // Called by the TransportAction sent with InspectorIo::Write(): kKill and kStop 77 void Stop(); 78 79 // kSendMessage 80 void Send(int sessionId, const std::string& message); 81 82 // kKill 83 void TerminateConnections(); 84 85 int GetPort() const; 86 87 // Session connection lifecycle 88 void Accept(int serverPort, uv_stream_t* serverSocket); 89 90 bool HandleGetRequest(int sessionId, const std::string& hostName, const std::string& path); 91 92 void SessionStarted(int sessionId, const std::string& targetId, const std::string& wsKey); 93 94 void SessionTerminated(int sessionId); 95 MessageReceived(int sessionId,const std::string & message)96 void MessageReceived(int sessionId, const std::string& message) 97 { 98 delegate->MessageReceived(sessionId, message); 99 } 100 101 SocketSession* Session(int sessionId); 102 Done()103 bool Done() const 104 { 105 return serverSockets.empty() && connectedSessions.empty(); 106 } 107 108 static void CloseServerSocket(ServerSocket* server); 109 using ServerSocketPtr = DeleteFnPtr<ServerSocket, CloseServerSocket>; 110 111 private: 112 void SendListResponse(InspectorSocket* socket, const std::string& hostName, SocketSession* session); 113 std::string GetFrontendURL(bool isCompat, const std::string& formattedAddress); 114 bool TargetExists(const std::string& id); 115 116 enum class ServerState { NEW, RUNNING, STOPPED }; 117 uv_loop_t* loop; 118 std::unique_ptr<SocketServerDelegate> delegate; 119 const std::string host; 120 int port; 121 InspectPublishUid inspectPublishUid; 122 std::vector<ServerSocketPtr> serverSockets; 123 std::map<int, std::pair<std::string, std::unique_ptr<SocketSession>>> connectedSessions; 124 int nextSessionId; 125 FILE* out; 126 ServerState state; 127 int pid; 128 }; 129 130 } // namespace inspector 131 } // namespace jsvm 132 133 #endif // SRC_INSPECTOR_SOCKET_SERVER_H_ 134