Skip to content

Commit

Permalink
net/proxy:interface purify interface
Browse files Browse the repository at this point in the history
In most cases the asio compatible part is not needed. In the few cases where it is needed, a wrapper will be introduced.
  • Loading branch information
iceboy233 committed Sep 7, 2024
1 parent 7da320c commit a700d66
Show file tree
Hide file tree
Showing 11 changed files with 7 additions and 97 deletions.
42 changes: 0 additions & 42 deletions net/proxy/datagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <cstddef>
#include <system_error>
#include <utility>

#include "absl/functional/any_invocable.h"
#include "absl/types/span.h"
Expand All @@ -14,8 +13,6 @@ namespace proxy {

class Datagram {
public:
using executor_type = any_io_executor;

virtual ~Datagram() = default;

virtual void receive_from(
Expand All @@ -28,48 +25,9 @@ class Datagram {
const udp::endpoint &endpoint,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) = 0;

virtual any_io_executor get_executor() = 0;
virtual void close() = 0;

template <typename BuffersT, typename CallbackT>
void async_receive_from(
const BuffersT &buffers,
udp::endpoint &endpoint,
CallbackT &&callback);

template <typename BuffersT, typename CallbackT>
void async_send_to(
const BuffersT &buffers,
const udp::endpoint &endpoint,
CallbackT &&callback);
};

template <typename BuffersT, typename CallbackT>
void Datagram::async_receive_from(
const BuffersT &buffers,
udp::endpoint &endpoint,
CallbackT &&callback) {
receive_from(
absl::Span<mutable_buffer const>(
buffer_sequence_begin(buffers),
buffer_sequence_end(buffers) - buffer_sequence_begin(buffers)),
endpoint,
std::forward<CallbackT>(callback));
}

template <typename BuffersT, typename CallbackT>
void Datagram::async_send_to(
const BuffersT &buffers,
const udp::endpoint &endpoint,
CallbackT &&callback) {
send_to(
absl::Span<const_buffer const>(
buffer_sequence_begin(buffers),
buffer_sequence_end(buffers) - buffer_sequence_begin(buffers)),
endpoint,
std::forward<CallbackT>(callback));
}

} // namespace proxy
} // namespace net

Expand Down
5 changes: 2 additions & 3 deletions net/proxy/shadowsocks/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ REGISTER_HANDLER(shadowsocks, [](
LOG(error) << "invalid connector: " << connector_str;
return nullptr;
}
auto handler = std::make_unique<Handler>(proxy.executor(), *connector);
auto handler = std::make_unique<Handler>(*connector);
if (!handler->init(options)) {
LOG(error) << "init failed";
return nullptr;
Expand Down Expand Up @@ -67,8 +67,7 @@ REGISTER_CONNECTOR(shadowsocks, [](
LOG(error) << "invalid connector: " << connector_str;
return nullptr;
}
auto connector = std::make_unique<Connector>(
proxy.executor(), *base_connector);
auto connector = std::make_unique<Connector>(*base_connector);
if (!connector->init(options)) {
LOG(error) << "init failed";
return nullptr;
Expand Down
7 changes: 0 additions & 7 deletions net/proxy/shadowsocks/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class Connector::TcpStream : public proxy::Stream {
absl::Span<const_buffer const> buffers,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) override;

any_io_executor get_executor() override { return connector_.executor_; }
void close() override { base_stream_->close(); }

private:
Expand All @@ -71,12 +70,6 @@ class Connector::TcpStream : public proxy::Stream {
ConstBufferSpan read_buffer_;
};

Connector::Connector(
const any_io_executor &executor,
proxy::Connector &base_connector)
: executor_(executor),
base_connector_(base_connector) {}

bool Connector::init(const InitOptions &options) {
endpoints_ = options.endpoints;
if (endpoints_.empty()) {
Expand Down
6 changes: 2 additions & 4 deletions net/proxy/shadowsocks/connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ namespace shadowsocks {

class Connector : public proxy::Connector {
public:
Connector(
const any_io_executor &executor,
proxy::Connector &base_connector);
explicit Connector(proxy::Connector &base_connector)
: base_connector_(base_connector) {}

Connector(const Connector &) = delete;
Connector &operator=(const Connector &) = delete;
Expand Down Expand Up @@ -63,7 +62,6 @@ class Connector : public proxy::Connector {
private:
class TcpStream;

any_io_executor executor_;
proxy::Connector &base_connector_;
std::vector<Endpoint> endpoints_;
std::vector<Endpoint>::iterator endpoints_iter_;
Expand Down
5 changes: 0 additions & 5 deletions net/proxy/shadowsocks/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ class Handler::TcpConnection : public boost::intrusive_ref_counter<
bool write_header_;
};

Handler::Handler(
const any_io_executor &executor,
proxy::Connector &connector)
: connector_(connector) {}

bool Handler::init(const InitOptions &options) {
return pre_shared_key_.init(*options.method, options.password);
}
Expand Down
5 changes: 2 additions & 3 deletions net/proxy/shadowsocks/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ namespace shadowsocks {

class Handler : public proxy::Handler {
public:
Handler(
const any_io_executor &executor,
proxy::Connector &connector);
explicit Handler(proxy::Connector &connector)
: connector_(connector) {}

struct InitOptions {
const Method *method = &Method::aes_128_gcm();
Expand Down
28 changes: 0 additions & 28 deletions net/proxy/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <cstddef>
#include <system_error>
#include <utility>

#include "absl/functional/any_invocable.h"
#include "absl/types/span.h"
Expand All @@ -14,8 +13,6 @@ namespace proxy {

class Stream {
public:
using executor_type = any_io_executor;

virtual ~Stream() = default;

virtual void read(
Expand All @@ -26,34 +23,9 @@ class Stream {
absl::Span<const_buffer const> buffers,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) = 0;

virtual any_io_executor get_executor() = 0;
virtual void close() = 0;

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);
};

template <typename BuffersT, typename CallbackT>
void Stream::async_read_some(const BuffersT &buffers, CallbackT &&callback) {
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 Stream::async_write_some(const BuffersT &buffers, CallbackT &&callback) {
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

Expand Down
2 changes: 1 addition & 1 deletion net/proxy/system/stdio-stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace proxy {
namespace system {

StdioStream::StdioStream(const any_io_executor &executor)
: executor_(executor),
:
#ifndef _WIN32
stdin_(executor, STDIN_FILENO),
stdout_(executor, STDOUT_FILENO)
Expand Down
2 changes: 0 additions & 2 deletions net/proxy/system/stdio-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ 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 executor_; }
void close() override;

private:
any_io_executor executor_;
#ifndef _WIN32
readable_pipe stdin_;
writable_pipe stdout_;
Expand Down
1 change: 0 additions & 1 deletion net/proxy/system/tcp-socket-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TcpSocketStream : 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 socket_.get_executor(); }
void close() override;

tcp::socket &socket() { return socket_; }
Expand Down
1 change: 0 additions & 1 deletion net/proxy/system/udp-socket-datagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class UdpSocketDatagram : public Datagram {
const udp::endpoint &endpoint,
absl::AnyInvocable<void(std::error_code, size_t) &&> callback) override;

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

udp::socket &socket() { return socket_; }
Expand Down

0 comments on commit a700d66

Please sign in to comment.