Skip to content

Commit

Permalink
net/proxy/util:stream-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Sep 15, 2024
1 parent f4d5eb0 commit 2037eef
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions net/proxy/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ cc_library(
],
)

cc_library(
name = "stream-wrapper",
hdrs = ["stream-wrapper.h"],
deps = [
"//net/proxy:interface",
"@org_iceboy_trunk//net:asio",
],
)

cc_library(
name = "write",
srcs = ["write.cc"],
Expand Down
61 changes: 61 additions & 0 deletions net/proxy/util/stream-wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef _NET_PROXY_UTIL_STREAM_WRAPPER_H
#define _NET_PROXY_UTIL_STREAM_WRAPPER_H

#include <utility>

#include "absl/types/span.h"
#include "net/asio.h"
#include "net/proxy/stream.h"

namespace net {
namespace proxy {

class StreamWrapper {
public:
using executor_type = any_io_executor;
using lowest_layer_type = StreamWrapper;

StreamWrapper(Stream &stream, const any_io_executor &executor)
: stream_(stream), executor_(executor) {}

template <typename BuffersT, typename CallbackT>
void async_read_some(const BuffersT &buffers, CallbackT &&callback);

template <typename BuffersT, typename CallbackT>
void async_write_some(const BuffersT &buffers, CallbackT &&callback);

Stream &stream() { return stream_; }
const any_io_executor &get_executor() { return executor_; }

StreamWrapper &lowest_layer() { return *this; }
const StreamWrapper &lowest_layer() const { return *this; }

private:
Stream &stream_;
any_io_executor executor_;
};

template <typename BuffersT, typename CallbackT>
void StreamWrapper::async_read_some(
const BuffersT &buffers, CallbackT &&callback) {
stream_.read(
absl::Span<mutable_buffer const>(
buffer_sequence_begin(buffers),
buffer_sequence_end(buffers) - buffer_sequence_begin(buffers)),
std::forward<CallbackT>(callback));
}

template <typename BuffersT, typename CallbackT>
void StreamWrapper::async_write_some(
const BuffersT &buffers, CallbackT &&callback) {
stream_.write(
absl::Span<const_buffer const>(
buffer_sequence_begin(buffers),
buffer_sequence_end(buffers) - buffer_sequence_begin(buffers)),
std::forward<CallbackT>(callback));
}

} // namespace proxy
} // namespace net

#endif // _NET_PROXY_UTIL_STREAM_WRAPPER_H

0 comments on commit 2037eef

Please sign in to comment.