Skip to content

Commit

Permalink
net/proxy/system:stdio-stream support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Jun 16, 2024
1 parent c2156c9 commit 8448d2a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
8 changes: 7 additions & 1 deletion net/proxy/system/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ cc_library(
"//net/proxy:interface",
"@com_google_absl//absl/container:fixed_array",
"@org_iceboy_trunk//net:asio",
],
] + select({
"@platforms//os:windows": [
"@org_iceboy_trunk//io:file-utils",
"@org_iceboy_trunk//io:native-file",
],
"//conditions:default": [],
}),
)

cc_library(
Expand Down
55 changes: 53 additions & 2 deletions net/proxy/system/stdio-stream.cc
Original file line number Diff line number Diff line change
@@ -1,35 +1,86 @@
#include "net/proxy/system/stdio-stream.h"

#include "absl/container/fixed_array.h"
#ifdef _WIN32
#include "io/file-utils.h"
#endif

namespace net {
namespace proxy {
namespace system {

StdioStream::StdioStream(const any_io_executor &executor)
: stdin_(executor, STDIN_FILENO),
stdout_(executor, STDOUT_FILENO) {}
: executor_(executor),
#ifndef _WIN32
stdin_(executor, STDIN_FILENO),
stdout_(executor, STDOUT_FILENO)
#else
stdin_thread_(1),
stdout_thread_(1),
stdin_(io::std_input()),
stdout_(io::std_output())
#endif
{}

void StdioStream::async_read_some(
absl::Span<mutable_buffer const> buffers,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) {
#ifndef _WIN32
stdin_.async_read_some(
absl::FixedArray<mutable_buffer, 1>(buffers.begin(), buffers.end()),
std::move(callback));
#else
post(
stdin_thread_,
[this, buffers, callback = std::move(callback)]() mutable {
if (buffers.empty()) {
std::move(callback)({}, 0);
return;
}
size_t size;
std::error_code ec = stdin_.read(
{buffers.front().data(), buffers.front().size()}, size);
if (ec) {
std::move(callback)(ec, 0);
return;
}
std::move(callback)({}, size);
});
#endif
}

void StdioStream::async_write_some(
absl::Span<const_buffer const> buffers,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) {
#ifndef _WIN32
stdout_.async_write_some(
absl::FixedArray<const_buffer, 1>(buffers.begin(), buffers.end()),
std::move(callback));
#else
post(
stdout_thread_,
[this, buffers, callback = std::move(callback)]() mutable {
size_t size = 0;
for (const_buffer buffer : buffers) {
std::error_code ec = io::write(
stdout_, {buffer.data(), buffer.size()});
if (ec) {
std::move(callback)(ec, size);
return;
}
size += buffer.size();
}
std::move(callback)({}, size);
});
#endif
}

void StdioStream::close() {
#ifndef _WIN32
boost::system::error_code ec;
stdin_.close(ec);
stdout_.close(ec);
#endif
}

} // namespace system
Expand Down
13 changes: 12 additions & 1 deletion net/proxy/system/stdio-stream.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef _NET_PROXY_SYSTEM_STDIO_STREAM_H
#define _NET_PROXY_SYSTEM_STDIO_STREAM_H

#ifdef _WIN32
#include "io/native-file.h"
#endif
#include "net/asio.h"
#include "net/proxy/stream.h"

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

any_io_executor get_executor() override { return stdin_.get_executor(); }
any_io_executor get_executor() override { return executor_; }
void close() override;

private:
any_io_executor executor_;
#ifndef _WIN32
readable_pipe stdin_;
writable_pipe stdout_;
#else
static_thread_pool stdin_thread_;
static_thread_pool stdout_thread_;
io::NativeSharedFile stdin_;
io::NativeSharedFile stdout_;
#endif // _WIN32
};

} // namespace system
Expand Down

0 comments on commit 8448d2a

Please sign in to comment.