11#include " io_agent.h"
2+ #include < string>
23#include " crdtp/dispatch.h"
4+ #include " node_mutex.h"
35
46namespace node ::inspector::protocol {
57
@@ -8,17 +10,47 @@ void IoAgent::Wire(UberDispatcher* dispatcher) {
810 IO::Dispatcher::wire (dispatcher, this );
911}
1012
13+ std::unordered_map<int , int > IoAgent::offset_map_;
14+ std::unordered_map<int , std::string> IoAgent::data_map_;
15+ std::atomic<int > IoAgent::stream_counter_{1 };
16+ Mutex IoAgent::data_mutex_;
17+
18+ int IoAgent::setData (const std::string& value) {
19+ int key = getNextStreamId ();
20+ Mutex::ScopedLock lock (data_mutex_);
21+ data_map_[key] = value;
22+
23+ return key;
24+ }
25+
26+ int IoAgent::getNextStreamId () {
27+ return stream_counter_++;
28+ }
29+
1130DispatchResponse IoAgent::read (const String& in_handle,
1231 Maybe<int > in_offset,
1332 Maybe<int > in_size,
1433 String* out_data,
1534 bool * out_eof) {
16- std::string txt = data_map_[in_handle];
35+ Mutex::ScopedReadLock lock (data_mutex_);
36+ std::string in_handle_str = in_handle;
37+ int stream_id = 0 ;
38+ bool is_number = std::all_of (in_handle_str.begin (),
39+ in_handle_str.end (),
40+ ::isdigit);
41+ if (is_number) {
42+ stream_id = std::stoi (in_handle_str);
43+ } else {
44+ out_data = new String (" " );
45+ *out_eof = true ;
46+ }
47+
48+ std::string txt = data_map_[stream_id];
1749 int offset = 0 ;
1850 if (in_offset.isJust ()) {
1951 offset = in_offset.fromJust ();
20- } else if (offset_map_.find (in_handle ) != offset_map_.end ()) {
21- offset = offset_map_[in_handle ];
52+ } else if (offset_map_.find (stream_id ) != offset_map_.end ()) {
53+ offset = offset_map_[stream_id ];
2254 }
2355 int size = 1 << 20 ;
2456 if (in_size.isJust ()) {
@@ -28,18 +60,28 @@ DispatchResponse IoAgent::read(const String& in_handle,
2860 if (static_cast <std::size_t >(offset) < txt.length ()) {
2961 std::string out_txt = txt.substr (offset, size);
3062 out_data->assign (out_txt);
63+ *out_eof = false ;
3164 } else {
3265 *out_eof = true ;
3366 }
3467
35- offset_map_[in_handle ] = offset + size;
68+ offset_map_[stream_id ] = offset + size;
3669
3770 return DispatchResponse::Success ();
3871}
3972
4073DispatchResponse IoAgent::close (const String& in_handle) {
41- offset_map_.erase (in_handle);
42- data_map_.erase (in_handle);
74+ Mutex::ScopedWriteLock lock (data_mutex_);
75+ std::string in_handle_str = in_handle;
76+ int stream_id = 0 ;
77+ bool is_number = std::all_of (in_handle_str.begin (),
78+ in_handle_str.end (),
79+ ::isdigit);
80+ if (is_number) {
81+ stream_id = std::stoi (in_handle_str);
82+ offset_map_.erase (stream_id);
83+ data_map_.erase (stream_id);
84+ }
4385 return DispatchResponse::Success ();
4486}
4587} // namespace node::inspector::protocol
0 commit comments