1 |
|
|
#ifndef SRC_INSPECTOR_SOCKET_SERVER_H_ |
2 |
|
|
#define SRC_INSPECTOR_SOCKET_SERVER_H_ |
3 |
|
|
|
4 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
5 |
|
|
|
6 |
|
|
#include "inspector_agent.h" |
7 |
|
|
#include "inspector_socket.h" |
8 |
|
|
#include "uv.h" |
9 |
|
|
|
10 |
|
|
#include <map> |
11 |
|
|
#include <string> |
12 |
|
|
#include <vector> |
13 |
|
|
|
14 |
|
|
#if !HAVE_INSPECTOR |
15 |
|
|
#error("This header can only be used when inspector is enabled") |
16 |
|
|
#endif |
17 |
|
|
|
18 |
|
|
namespace node { |
19 |
|
|
namespace inspector { |
20 |
|
|
|
21 |
|
|
std::string FormatWsAddress(const std::string& host, int port, |
22 |
|
|
const std::string& target_id, |
23 |
|
|
bool include_protocol); |
24 |
|
|
|
25 |
|
|
class InspectorSocketServer; |
26 |
|
|
class SocketSession; |
27 |
|
|
class ServerSocket; |
28 |
|
|
|
29 |
|
|
class SocketServerDelegate { |
30 |
|
|
public: |
31 |
|
|
virtual void AssignServer(InspectorSocketServer* server) = 0; |
32 |
|
|
virtual void StartSession(int session_id, const std::string& target_id) = 0; |
33 |
|
|
virtual void EndSession(int session_id) = 0; |
34 |
|
|
virtual void MessageReceived(int session_id, const std::string& message) = 0; |
35 |
|
|
virtual std::vector<std::string> GetTargetIds() = 0; |
36 |
|
|
virtual std::string GetTargetTitle(const std::string& id) = 0; |
37 |
|
|
virtual std::string GetTargetUrl(const std::string& id) = 0; |
38 |
|
178 |
virtual ~SocketServerDelegate() = default; |
39 |
|
|
}; |
40 |
|
|
|
41 |
|
|
// HTTP Server, writes messages requested as TransportActions, and responds |
42 |
|
|
// to HTTP requests and WS upgrades. |
43 |
|
|
|
44 |
|
89 |
class InspectorSocketServer { |
45 |
|
|
public: |
46 |
|
|
InspectorSocketServer(std::unique_ptr<SocketServerDelegate> delegate, |
47 |
|
|
uv_loop_t* loop, |
48 |
|
|
const std::string& host, |
49 |
|
|
int port, |
50 |
|
|
const InspectPublishUid& inspect_publish_uid, |
51 |
|
|
FILE* out = stderr); |
52 |
|
|
~InspectorSocketServer(); |
53 |
|
|
|
54 |
|
|
// Start listening on host/port |
55 |
|
|
bool Start(); |
56 |
|
|
|
57 |
|
|
// Called by the TransportAction sent with InspectorIo::Write(): |
58 |
|
|
// kKill and kStop |
59 |
|
|
void Stop(); |
60 |
|
|
// kSendMessage |
61 |
|
|
void Send(int session_id, const std::string& message); |
62 |
|
|
// kKill |
63 |
|
|
void TerminateConnections(); |
64 |
|
|
int Port() const; |
65 |
|
|
|
66 |
|
|
// Session connection lifecycle |
67 |
|
|
void Accept(int server_port, uv_stream_t* server_socket); |
68 |
|
|
bool HandleGetRequest(int session_id, const std::string& host, |
69 |
|
|
const std::string& path); |
70 |
|
|
void SessionStarted(int session_id, const std::string& target_id, |
71 |
|
|
const std::string& ws_id); |
72 |
|
|
void SessionTerminated(int session_id); |
73 |
|
135 |
void MessageReceived(int session_id, const std::string& message) { |
74 |
|
135 |
delegate_->MessageReceived(session_id, message); |
75 |
|
135 |
} |
76 |
|
|
SocketSession* Session(int session_id); |
77 |
|
94 |
bool done() const { |
78 |
✓✗✓✓
|
94 |
return server_sockets_.empty() && connected_sessions_.empty(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
static void CloseServerSocket(ServerSocket*); |
82 |
|
|
using ServerSocketPtr = DeleteFnPtr<ServerSocket, CloseServerSocket>; |
83 |
|
|
|
84 |
|
|
private: |
85 |
|
|
void SendListResponse(InspectorSocket* socket, const std::string& host, |
86 |
|
|
SocketSession* session); |
87 |
|
|
std::string GetFrontendURL(bool is_compat, |
88 |
|
|
const std::string &formatted_address); |
89 |
|
|
bool TargetExists(const std::string& id); |
90 |
|
|
|
91 |
|
|
enum class ServerState {kNew, kRunning, kStopped}; |
92 |
|
|
uv_loop_t* loop_; |
93 |
|
|
std::unique_ptr<SocketServerDelegate> delegate_; |
94 |
|
|
const std::string host_; |
95 |
|
|
int port_; |
96 |
|
|
InspectPublishUid inspect_publish_uid_; |
97 |
|
|
std::vector<ServerSocketPtr> server_sockets_; |
98 |
|
|
std::map<int, std::pair<std::string, std::unique_ptr<SocketSession>>> |
99 |
|
|
connected_sessions_; |
100 |
|
|
int next_session_id_; |
101 |
|
|
FILE* out_; |
102 |
|
|
ServerState state_; |
103 |
|
|
}; |
104 |
|
|
|
105 |
|
|
} // namespace inspector |
106 |
|
|
} // namespace node |
107 |
|
|
|
108 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
109 |
|
|
|
110 |
|
|
#endif // SRC_INSPECTOR_SOCKET_SERVER_H_ |