pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/nodejs/node/commit/1c211ec90162088008228ed983ecb476a2cb2cbe

imer-b69241e157469407.css" /> inspector: code cleanup · nodejs/node@1c211ec · GitHub
Skip to content

Commit 1c211ec

Browse files
Eugene OstroukhovMylesBorins
authored andcommitted
inspector: code cleanup
Remove some unused code from the WS server implementation and switch to smart pointers where possible. PR-URL: #21070 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 8862f0a commit 1c211ec

File tree

7 files changed

+229
-337
lines changed

7 files changed

+229
-337
lines changed

src/inspector_io.cc

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ class IoSessionDelegate : public InspectorSessionDelegate {
109109
// mostly session start, message received, and session end.
110110
class InspectorIoDelegate: public node::inspector::SocketServerDelegate {
111111
public:
112-
InspectorIoDelegate(InspectorIo* io, const std::string& script_path,
112+
InspectorIoDelegate(InspectorIo* io, const std::string& target_id,
113+
const std::string& script_path,
113114
const std::string& script_name, bool wait);
115+
~InspectorIoDelegate() {
116+
io_->ServerDone();
117+
}
114118
// Calls PostIncomingMessage() with appropriate InspectorAction:
115119
// kStartSession
116120
void StartSession(int session_id, const std::string& target_id) override;
@@ -122,11 +126,8 @@ class InspectorIoDelegate: public node::inspector::SocketServerDelegate {
122126
std::vector<std::string> GetTargetIds() override;
123127
std::string GetTargetTitle(const std::string& id) override;
124128
std::string GetTargetUrl(const std::string& id) override;
125-
void ServerDone() override {
126-
io_->ServerDone();
127-
}
128129

129-
void AssignTransport(InspectorSocketServer* server) {
130+
void AssignServer(InspectorSocketServer* server) override {
130131
server_ = server;
131132
}
132133

@@ -163,11 +164,11 @@ class DispatchMessagesTask : public v8::Task {
163164
InspectorIo::InspectorIo(Environment* env, v8::Platform* platform,
164165
const std::string& path, const DebugOptions& options,
165166
bool wait_for_connect)
166-
: options_(options), thread_(), delegate_(nullptr),
167-
state_(State::kNew), parent_env_(env),
168-
thread_req_(), platform_(platform),
167+
: options_(options), thread_(), state_(State::kNew),
168+
parent_env_(env), thread_req_(), platform_(platform),
169169
dispatching_messages_(false), script_name_(path),
170-
wait_for_connect_(wait_for_connect), port_(-1) {
170+
wait_for_connect_(wait_for_connect), port_(-1),
171+
id_(GenerateID()) {
171172
main_thread_req_ = new AsyncAndAgent({uv_async_t(), env->inspector_agent()});
172173
CHECK_EQ(0, uv_async_init(env->event_loop(), &main_thread_req_->first,
173174
InspectorIo::MainThreadReqAsyncCb));
@@ -244,7 +245,7 @@ void InspectorIo::IoThreadAsyncCb(uv_async_t* async) {
244245
transport->TerminateConnections();
245246
// Fallthrough
246247
case TransportAction::kStop:
247-
transport->Stop(nullptr);
248+
transport->Stop();
248249
break;
249250
case TransportAction::kSendMessage:
250251
transport->Send(session_id,
@@ -271,11 +272,11 @@ void InspectorIo::ThreadMain() {
271272
err = uv_async_init(&loop, &thread_req_, IoThreadAsyncCb<Transport>);
272273
CHECK_EQ(err, 0);
273274
std::string script_path = ScriptPath(&loop, script_name_);
274-
InspectorIoDelegate delegate(this, script_path, script_name_,
275-
wait_for_connect_);
276-
delegate_ = &delegate;
277-
Transport server(&delegate, &loop, options_.host_name(), options_.port());
278-
delegate.AssignTransport(&server);
275+
auto delegate = std::unique_ptr<InspectorIoDelegate>(
276+
new InspectorIoDelegate(this, id_, script_path, script_name_,
277+
wait_for_connect_));
278+
Transport server(std::move(delegate), &loop, options_.host_name(),
279+
options_.port());
279280
TransportAndIo<Transport> queue_transport(&server, this);
280281
thread_req_.data = &queue_transport;
281282
if (!server.Start()) {
@@ -291,8 +292,6 @@ void InspectorIo::ThreadMain() {
291292
uv_run(&loop, UV_RUN_DEFAULT);
292293
thread_req_.data = nullptr;
293294
CHECK_EQ(uv_loop_close(&loop), 0);
294-
delegate.AssignTransport(nullptr);
295-
delegate_ = nullptr;
296295
}
297296

298297
template <typename ActionType>
@@ -328,7 +327,7 @@ void InspectorIo::PostIncomingMessage(InspectorAction action, int session_id,
328327
}
329328

330329
std::vector<std::string> InspectorIo::GetTargetIds() const {
331-
return delegate_ ? delegate_->GetTargetIds() : std::vector<std::string>();
330+
return { id_ };
332331
}
333332

334333
TransportAction InspectorIo::Attach(int session_id) {
@@ -416,14 +415,15 @@ bool InspectorIo::WaitForFrontendEvent() {
416415
}
417416

418417
InspectorIoDelegate::InspectorIoDelegate(InspectorIo* io,
418+
const std::string& target_id,
419419
const std::string& script_path,
420420
const std::string& script_name,
421421
bool wait)
422422
: io_(io),
423423
session_id_(0),
424424
script_name_(script_name),
425425
script_path_(script_path),
426-
target_id_(GenerateID()),
426+
target_id_(target_id),
427427
waiting_(wait),
428428
server_(nullptr) { }
429429

src/inspector_io.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ class InspectorIo {
134134
// and receive a connection if wait_for_connect was requested.
135135
uv_sem_t thread_start_sem_;
136136

137-
InspectorIoDelegate* delegate_;
138137
State state_;
139138
node::Environment* parent_env_;
140139

@@ -161,6 +160,8 @@ class InspectorIo {
161160
const bool wait_for_connect_;
162161
int port_;
163162
std::unordered_map<int, std::unique_ptr<InspectorSession>> sessions_;
163+
// May be accessed from any thread
164+
const std::string id_;
164165

165166
friend class DispatchMessagesTask;
166167
friend class IoSessionDelegate;

src/inspector_socket.cc

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ namespace inspector {
2222

2323
class TcpHolder {
2424
public:
25-
using Pointer = std::unique_ptr<TcpHolder, void(*)(TcpHolder*)>;
25+
static void DisconnectAndDispose(TcpHolder* holder);
26+
using Pointer = DeleteFnPtr<TcpHolder, DisconnectAndDispose>;
2627

2728
static Pointer Accept(uv_stream_t* server,
2829
InspectorSocket::DelegatePointer delegate);
@@ -41,7 +42,6 @@ class TcpHolder {
4142
static void OnClosed(uv_handle_t* handle);
4243
static void OnDataReceivedCb(uv_stream_t* stream, ssize_t nread,
4344
const uv_buf_t* buf);
44-
static void DisconnectAndDispose(TcpHolder* holder);
4545
explicit TcpHolder(InspectorSocket::DelegatePointer delegate);
4646
~TcpHolder() = default;
4747
void ReclaimUvBuf(const uv_buf_t* buf, ssize_t read);
@@ -68,14 +68,10 @@ class ProtocolHandler {
6868
InspectorSocket* inspector() {
6969
return inspector_;
7070
}
71-
72-
static void Shutdown(ProtocolHandler* handler) {
73-
handler->Shutdown();
74-
}
71+
virtual void Shutdown() = 0;
7572

7673
protected:
7774
virtual ~ProtocolHandler() = default;
78-
virtual void Shutdown() = 0;
7975
int WriteRaw(const std::vector<char>& buffer, uv_write_cb write_cb);
8076
InspectorSocket::Delegate* delegate();
8177

@@ -653,10 +649,10 @@ TcpHolder::Pointer TcpHolder::Accept(
653649
err = uv_read_start(tcp, allocate_buffer, OnDataReceivedCb);
654650
}
655651
if (err == 0) {
656-
return { result, DisconnectAndDispose };
652+
return TcpHolder::Pointer(result);
657653
} else {
658654
delete result;
659-
return { nullptr, nullptr };
655+
return nullptr;
660656
}
661657
}
662658

@@ -721,12 +717,13 @@ void TcpHolder::ReclaimUvBuf(const uv_buf_t* buf, ssize_t read) {
721717
delete[] buf->base;
722718
}
723719

724-
// Public interface
725-
InspectorSocket::InspectorSocket()
726-
: protocol_handler_(nullptr, ProtocolHandler::Shutdown) { }
727-
728720
InspectorSocket::~InspectorSocket() = default;
729721

722+
// static
723+
void InspectorSocket::Shutdown(ProtocolHandler* handler) {
724+
handler->Shutdown();
725+
}
726+
730727
// static
731728
InspectorSocket::Pointer InspectorSocket::Accept(uv_stream_t* server,
732729
DelegatePointer delegate) {

src/inspector_socket.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class InspectorSocket {
4040
std::string GetHost();
4141

4242
private:
43-
InspectorSocket();
43+
static void Shutdown(ProtocolHandler*);
44+
InspectorSocket() = default;
4445

45-
std::unique_ptr<ProtocolHandler, void(*)(ProtocolHandler*)> protocol_handler_;
46+
DeleteFnPtr<ProtocolHandler, Shutdown> protocol_handler_;
4647

4748
DISALLOW_COPY_AND_ASSIGN(InspectorSocket);
4849
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy