GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/inspector_socket_server.h Lines: 7 7 100.0 %
Date: 2017-06-14 Branches: 0 0 0.0 %

Line Branch Exec Source
1
#ifndef SRC_INSPECTOR_SOCKET_SERVER_H_
2
#define SRC_INSPECTOR_SOCKET_SERVER_H_
3
4
#include "inspector_agent.h"
5
#include "inspector_socket.h"
6
#include "uv.h"
7
8
#include <map>
9
#include <string>
10
#include <vector>
11
12
#if !HAVE_INSPECTOR
13
#error("This header can only be used when inspector is enabled")
14
#endif
15
16
namespace node {
17
namespace inspector {
18
19
class Closer;
20
class SocketSession;
21
22
20
class SocketServerDelegate {
23
 public:
24
  virtual bool StartSession(int session_id, const std::string& target_id) = 0;
25
  virtual void EndSession(int session_id) = 0;
26
  virtual void MessageReceived(int session_id, const std::string& message) = 0;
27
  virtual std::vector<std::string> GetTargetIds() = 0;
28
  virtual std::string GetTargetTitle(const std::string& id) = 0;
29
  virtual std::string GetTargetUrl(const std::string& id) = 0;
30
  virtual void ServerDone() = 0;
31
};
32
33
// HTTP Server, writes messages requested as TransportActions, and responds
34
// to HTTP requests and WS upgrades.
35
36
37
38
3
class InspectorSocketServer {
39
 public:
40
  using ServerCallback = void (*)(InspectorSocketServer*);
41
  InspectorSocketServer(SocketServerDelegate* delegate,
42
                        uv_loop_t* loop,
43
                        const std::string& host,
44
                        int port,
45
                        FILE* out = stderr);
46
  // Start listening on host/port
47
  bool Start();
48
49
  // Called by the TransportAction sent with InspectorIo::Write():
50
  //   kKill and kStop
51
  void Stop(ServerCallback callback);
52
  //   kSendMessage
53
  void Send(int session_id, const std::string& message);
54
  //   kKill
55
  void TerminateConnections();
56
57
20
  int port() {
58
20
    return port_;
59
  }
60
61
 private:
62
  static bool HandshakeCallback(InspectorSocket* socket,
63
                                enum inspector_handshake_event state,
64
                                const std::string& path);
65
  static void SocketConnectedCallback(uv_stream_t* server, int status);
66
  static void ServerClosedCallback(uv_handle_t* server);
67
  template<typename SomeUvStruct>
68
23
  static InspectorSocketServer* From(SomeUvStruct* server) {
69
    return node::ContainerOf(&InspectorSocketServer::server_,
70
23
                             reinterpret_cast<uv_tcp_t*>(server));
71
  }
72
  bool RespondToGet(InspectorSocket* socket, const std::string& path);
73
  void SendListResponse(InspectorSocket* socket);
74
  void ReadCallback(InspectorSocket* socket, ssize_t read, const uv_buf_t* buf);
75
  bool SessionStarted(SocketSession* session, const std::string& id);
76
  void SessionTerminated(SocketSession* session);
77
  bool TargetExists(const std::string& id);
78
43
  SocketServerDelegate* Delegate() { return delegate_; }
79
80
  enum class ServerState {kNew, kRunning, kStopping, kStopped};
81
  uv_loop_t* loop_;
82
  SocketServerDelegate* const delegate_;
83
  const std::string host_;
84
  int port_;
85
  std::string path_;
86
  uv_tcp_t server_;
87
  Closer* closer_;
88
  std::map<int, SocketSession*> connected_sessions_;
89
  int next_session_id_;
90
  FILE* out_;
91
  ServerState state_;
92
93
  friend class SocketSession;
94
  friend class Closer;
95
};
96
97
}  // namespace inspector
98
}  // namespace node
99
100
#endif  // SRC_INSPECTOR_SOCKET_SERVER_H_