GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector_socket_server.h Lines: 8 8 100.0 %
Date: 2019-05-05 22:32:45 Branches: 4 6 66.7 %

Line Branch Exec Source
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
class InspectorSocketServer;
22
class SocketSession;
23
class ServerSocket;
24
25
74
class SocketServerDelegate {
26
 public:
27
  virtual void AssignServer(InspectorSocketServer* server) = 0;
28
  virtual void StartSession(int session_id, const std::string& target_id) = 0;
29
  virtual void EndSession(int session_id) = 0;
30
  virtual void MessageReceived(int session_id, const std::string& message) = 0;
31
  virtual std::vector<std::string> GetTargetIds() = 0;
32
  virtual std::string GetTargetTitle(const std::string& id) = 0;
33
  virtual std::string GetTargetUrl(const std::string& id) = 0;
34
74
  virtual ~SocketServerDelegate() = default;
35
};
36
37
// HTTP Server, writes messages requested as TransportActions, and responds
38
// to HTTP requests and WS upgrades.
39
40
74
class InspectorSocketServer {
41
 public:
42
  InspectorSocketServer(std::unique_ptr<SocketServerDelegate> delegate,
43
                        uv_loop_t* loop,
44
                        const std::string& host,
45
                        int port,
46
                        FILE* out = stderr);
47
  ~InspectorSocketServer();
48
49
  // Start listening on host/port
50
  bool Start();
51
52
  // Called by the TransportAction sent with InspectorIo::Write():
53
  //   kKill and kStop
54
  void Stop();
55
  //   kSendMessage
56
  void Send(int session_id, const std::string& message);
57
  //   kKill
58
  void TerminateConnections();
59
  int Port() const;
60
61
  // Session connection lifecycle
62
  void Accept(int server_port, uv_stream_t* server_socket);
63
  bool HandleGetRequest(int session_id, const std::string& host,
64
                        const std::string& path);
65
  void SessionStarted(int session_id, const std::string& target_id,
66
                      const std::string& ws_id);
67
  void SessionTerminated(int session_id);
68
113
  void MessageReceived(int session_id, const std::string& message) {
69
113
    delegate_->MessageReceived(session_id, message);
70
113
  }
71
  SocketSession* Session(int session_id);
72
73
  bool done() const {
73

73
    return server_sockets_.empty() && connected_sessions_.empty();
74
  }
75
76
  static void CloseServerSocket(ServerSocket*);
77
  using ServerSocketPtr = DeleteFnPtr<ServerSocket, CloseServerSocket>;
78
79
 private:
80
  void SendListResponse(InspectorSocket* socket, const std::string& host,
81
                        SocketSession* session);
82
  std::string GetFrontendURL(bool is_compat,
83
                             const std::string &formatted_address);
84
  bool TargetExists(const std::string& id);
85
86
  enum class ServerState {kNew, kRunning, kStopped};
87
  uv_loop_t* loop_;
88
  std::unique_ptr<SocketServerDelegate> delegate_;
89
  const std::string host_;
90
  int port_;
91
  std::vector<ServerSocketPtr> server_sockets_;
92
  std::map<int, std::pair<std::string, std::unique_ptr<SocketSession>>>
93
      connected_sessions_;
94
  int next_session_id_;
95
  FILE* out_;
96
  ServerState state_;
97
};
98
99
}  // namespace inspector
100
}  // namespace node
101
102
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
103
104
#endif  // SRC_INSPECTOR_SOCKET_SERVER_H_