11cb0ef41Sopenharmony_ci#ifndef SRC_INSPECTOR_SOCKET_SERVER_H_ 21cb0ef41Sopenharmony_ci#define SRC_INSPECTOR_SOCKET_SERVER_H_ 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci#include "inspector_agent.h" 71cb0ef41Sopenharmony_ci#include "inspector_socket.h" 81cb0ef41Sopenharmony_ci#include "uv.h" 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci#include <map> 111cb0ef41Sopenharmony_ci#include <string> 121cb0ef41Sopenharmony_ci#include <vector> 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci#if !HAVE_INSPECTOR 151cb0ef41Sopenharmony_ci#error("This header can only be used when inspector is enabled") 161cb0ef41Sopenharmony_ci#endif 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cinamespace node { 191cb0ef41Sopenharmony_cinamespace inspector { 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cistd::string FormatWsAddress(const std::string& host, int port, 221cb0ef41Sopenharmony_ci const std::string& target_id, 231cb0ef41Sopenharmony_ci bool include_protocol); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciclass InspectorSocketServer; 261cb0ef41Sopenharmony_ciclass SocketSession; 271cb0ef41Sopenharmony_ciclass ServerSocket; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciclass SocketServerDelegate { 301cb0ef41Sopenharmony_ci public: 311cb0ef41Sopenharmony_ci virtual void AssignServer(InspectorSocketServer* server) = 0; 321cb0ef41Sopenharmony_ci virtual void StartSession(int session_id, const std::string& target_id) = 0; 331cb0ef41Sopenharmony_ci virtual void EndSession(int session_id) = 0; 341cb0ef41Sopenharmony_ci virtual void MessageReceived(int session_id, const std::string& message) = 0; 351cb0ef41Sopenharmony_ci virtual std::vector<std::string> GetTargetIds() = 0; 361cb0ef41Sopenharmony_ci virtual std::string GetTargetTitle(const std::string& id) = 0; 371cb0ef41Sopenharmony_ci virtual std::string GetTargetUrl(const std::string& id) = 0; 381cb0ef41Sopenharmony_ci virtual ~SocketServerDelegate() = default; 391cb0ef41Sopenharmony_ci}; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci// HTTP Server, writes messages requested as TransportActions, and responds 421cb0ef41Sopenharmony_ci// to HTTP requests and WS upgrades. 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciclass InspectorSocketServer { 451cb0ef41Sopenharmony_ci public: 461cb0ef41Sopenharmony_ci InspectorSocketServer(std::unique_ptr<SocketServerDelegate> delegate, 471cb0ef41Sopenharmony_ci uv_loop_t* loop, 481cb0ef41Sopenharmony_ci const std::string& host, 491cb0ef41Sopenharmony_ci int port, 501cb0ef41Sopenharmony_ci const InspectPublishUid& inspect_publish_uid, 511cb0ef41Sopenharmony_ci FILE* out = stderr, 521cb0ef41Sopenharmony_ci int pid = -1); 531cb0ef41Sopenharmony_ci ~InspectorSocketServer(); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci // Start listening on host/port 561cb0ef41Sopenharmony_ci bool Start(); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci // Called by the TransportAction sent with InspectorIo::Write(): 591cb0ef41Sopenharmony_ci // kKill and kStop 601cb0ef41Sopenharmony_ci void Stop(); 611cb0ef41Sopenharmony_ci // kSendMessage 621cb0ef41Sopenharmony_ci void Send(int session_id, const std::string& message); 631cb0ef41Sopenharmony_ci // kKill 641cb0ef41Sopenharmony_ci void TerminateConnections(); 651cb0ef41Sopenharmony_ci int Port() const; 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci // Session connection lifecycle 681cb0ef41Sopenharmony_ci void Accept(int server_port, uv_stream_t* server_socket); 691cb0ef41Sopenharmony_ci bool HandleGetRequest(int session_id, const std::string& host, 701cb0ef41Sopenharmony_ci const std::string& path); 711cb0ef41Sopenharmony_ci void SessionStarted(int session_id, const std::string& target_id, 721cb0ef41Sopenharmony_ci const std::string& ws_id); 731cb0ef41Sopenharmony_ci void SessionTerminated(int session_id); 741cb0ef41Sopenharmony_ci void MessageReceived(int session_id, const std::string& message) { 751cb0ef41Sopenharmony_ci delegate_->MessageReceived(session_id, message); 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci SocketSession* Session(int session_id); 781cb0ef41Sopenharmony_ci bool done() const { 791cb0ef41Sopenharmony_ci return server_sockets_.empty() && connected_sessions_.empty(); 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci static void CloseServerSocket(ServerSocket*); 831cb0ef41Sopenharmony_ci using ServerSocketPtr = DeleteFnPtr<ServerSocket, CloseServerSocket>; 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci private: 861cb0ef41Sopenharmony_ci void SendListResponse(InspectorSocket* socket, const std::string& host, 871cb0ef41Sopenharmony_ci SocketSession* session); 881cb0ef41Sopenharmony_ci std::string GetFrontendURL(bool is_compat, 891cb0ef41Sopenharmony_ci const std::string &formatted_address); 901cb0ef41Sopenharmony_ci bool TargetExists(const std::string& id); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci enum class ServerState {kNew, kRunning, kStopped}; 931cb0ef41Sopenharmony_ci uv_loop_t* loop_; 941cb0ef41Sopenharmony_ci std::unique_ptr<SocketServerDelegate> delegate_; 951cb0ef41Sopenharmony_ci const std::string host_; 961cb0ef41Sopenharmony_ci int port_; 971cb0ef41Sopenharmony_ci InspectPublishUid inspect_publish_uid_; 981cb0ef41Sopenharmony_ci std::vector<ServerSocketPtr> server_sockets_; 991cb0ef41Sopenharmony_ci std::map<int, std::pair<std::string, std::unique_ptr<SocketSession>>> 1001cb0ef41Sopenharmony_ci connected_sessions_; 1011cb0ef41Sopenharmony_ci int next_session_id_; 1021cb0ef41Sopenharmony_ci FILE* out_; 1031cb0ef41Sopenharmony_ci ServerState state_; 1041cb0ef41Sopenharmony_ci int pid_; 1051cb0ef41Sopenharmony_ci}; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci} // namespace inspector 1081cb0ef41Sopenharmony_ci} // namespace node 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci#endif // SRC_INSPECTOR_SOCKET_SERVER_H_ 113