Skip to content

Commit 8448d2a

Browse files
committed
net/proxy/system:stdio-stream support windows
1 parent c2156c9 commit 8448d2a

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

net/proxy/system/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ cc_library(
5050
"//net/proxy:interface",
5151
"@com_google_absl//absl/container:fixed_array",
5252
"@org_iceboy_trunk//net:asio",
53-
],
53+
] + select({
54+
"@platforms//os:windows": [
55+
"@org_iceboy_trunk//io:file-utils",
56+
"@org_iceboy_trunk//io:native-file",
57+
],
58+
"//conditions:default": [],
59+
}),
5460
)
5561

5662
cc_library(

net/proxy/system/stdio-stream.cc

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,86 @@
11
#include "net/proxy/system/stdio-stream.h"
22

33
#include "absl/container/fixed_array.h"
4+
#ifdef _WIN32
5+
#include "io/file-utils.h"
6+
#endif
47

58
namespace net {
69
namespace proxy {
710
namespace system {
811

912
StdioStream::StdioStream(const any_io_executor &executor)
10-
: stdin_(executor, STDIN_FILENO),
11-
stdout_(executor, STDOUT_FILENO) {}
13+
: executor_(executor),
14+
#ifndef _WIN32
15+
stdin_(executor, STDIN_FILENO),
16+
stdout_(executor, STDOUT_FILENO)
17+
#else
18+
stdin_thread_(1),
19+
stdout_thread_(1),
20+
stdin_(io::std_input()),
21+
stdout_(io::std_output())
22+
#endif
23+
{}
1224

1325
void StdioStream::async_read_some(
1426
absl::Span<mutable_buffer const> buffers,
1527
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) {
28+
#ifndef _WIN32
1629
stdin_.async_read_some(
1730
absl::FixedArray<mutable_buffer, 1>(buffers.begin(), buffers.end()),
1831
std::move(callback));
32+
#else
33+
post(
34+
stdin_thread_,
35+
[this, buffers, callback = std::move(callback)]() mutable {
36+
if (buffers.empty()) {
37+
std::move(callback)({}, 0);
38+
return;
39+
}
40+
size_t size;
41+
std::error_code ec = stdin_.read(
42+
{buffers.front().data(), buffers.front().size()}, size);
43+
if (ec) {
44+
std::move(callback)(ec, 0);
45+
return;
46+
}
47+
std::move(callback)({}, size);
48+
});
49+
#endif
1950
}
2051

2152
void StdioStream::async_write_some(
2253
absl::Span<const_buffer const> buffers,
2354
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) {
55+
#ifndef _WIN32
2456
stdout_.async_write_some(
2557
absl::FixedArray<const_buffer, 1>(buffers.begin(), buffers.end()),
2658
std::move(callback));
59+
#else
60+
post(
61+
stdout_thread_,
62+
[this, buffers, callback = std::move(callback)]() mutable {
63+
size_t size = 0;
64+
for (const_buffer buffer : buffers) {
65+
std::error_code ec = io::write(
66+
stdout_, {buffer.data(), buffer.size()});
67+
if (ec) {
68+
std::move(callback)(ec, size);
69+
return;
70+
}
71+
size += buffer.size();
72+
}
73+
std::move(callback)({}, size);
74+
});
75+
#endif
2776
}
2877

2978
void StdioStream::close() {
79+
#ifndef _WIN32
3080
boost::system::error_code ec;
3181
stdin_.close(ec);
3282
stdout_.close(ec);
83+
#endif
3384
}
3485

3586
} // namespace system

net/proxy/system/stdio-stream.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef _NET_PROXY_SYSTEM_STDIO_STREAM_H
22
#define _NET_PROXY_SYSTEM_STDIO_STREAM_H
33

4+
#ifdef _WIN32
5+
#include "io/native-file.h"
6+
#endif
47
#include "net/asio.h"
58
#include "net/proxy/stream.h"
69

@@ -23,12 +26,20 @@ class StdioStream : public Stream {
2326
absl::Span<const_buffer const> buffers,
2427
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) override;
2528

26-
any_io_executor get_executor() override { return stdin_.get_executor(); }
29+
any_io_executor get_executor() override { return executor_; }
2730
void close() override;
2831

2932
private:
33+
any_io_executor executor_;
34+
#ifndef _WIN32
3035
readable_pipe stdin_;
3136
writable_pipe stdout_;
37+
#else
38+
static_thread_pool stdin_thread_;
39+
static_thread_pool stdout_thread_;
40+
io::NativeSharedFile stdin_;
41+
io::NativeSharedFile stdout_;
42+
#endif // _WIN32
3243
};
3344

3445
} // namespace system

0 commit comments

Comments
 (0)