forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_agent.cc
More file actions
57 lines (51 loc) · 1.63 KB
/
io_agent.cc
File metadata and controls
57 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "io_agent.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include "crdtp/dispatch.h"
#include "inspector/network_resource_manager.h"
namespace node::inspector::protocol {
void IoAgent::Wire(UberDispatcher* dispatcher) {
frontend_ = std::make_shared<IO::Frontend>(dispatcher->channel());
IO::Dispatcher::wire(dispatcher, this);
}
DispatchResponse IoAgent::read(const String& in_handle,
std::optional<int> in_offset,
std::optional<int> in_size,
String* out_data,
bool* out_eof) {
std::string url = in_handle;
std::string txt = network_resource_manager_->Get(url);
std::string_view txt_view(txt);
int offset = 0;
bool offset_was_specified = false;
if (in_offset.has_value()) {
offset = *in_offset;
offset_was_specified = true;
} else if (offset_map_.contains(url)) {
offset = offset_map_[url];
}
int size = 1 << 20;
if (in_size.has_value()) {
size = *in_size;
}
if (static_cast<std::size_t>(offset) < txt_view.length()) {
std::string_view out_view = txt_view.substr(offset, size);
out_data->assign(out_view.data(), out_view.size());
*out_eof = false;
if (!offset_was_specified) {
offset_map_[url] = offset + size;
}
} else {
*out_data = "";
*out_eof = true;
}
return DispatchResponse::Success();
}
DispatchResponse IoAgent::close(const String& in_handle) {
std::string url = in_handle;
network_resource_manager_->Erase(url);
return DispatchResponse::Success();
}
} // namespace node::inspector::protocol