Skip to content

Commit

Permalink
net/proxy prefer non-templated I/O functions
Browse files Browse the repository at this point in the history
  • Loading branch information
iceboy233 committed Sep 5, 2024
1 parent 9ab011b commit 0d71ce9
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 60 deletions.
14 changes: 7 additions & 7 deletions net/proxy/ares/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ ares_ssize_t TcpSocket::recvfrom(
return -1;
}
read_buffer_.resize(buf_size);
stream_->async_read_some(
buffer(read_buffer_),
stream_->read(
{{read_buffer_.data(), buf_size}},
[socket = boost::intrusive_ptr<TcpSocket>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand Down Expand Up @@ -164,7 +164,7 @@ ares_ssize_t TcpSocket::sendv(const iovec *data, int len) {
}
async_write(
*stream_,
buffer(write_buffer_),
const_buffer(write_buffer_.data(), write_buffer_.size()),
[socket = boost::intrusive_ptr<TcpSocket>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand Down Expand Up @@ -240,8 +240,8 @@ ares_ssize_t UdpSocket::recvfrom(
return -1;
}
receive_buffer_.resize(buf_size);
datagram_->async_receive_from(
buffer(receive_buffer_),
datagram_->receive_from(
{{receive_buffer_.data(), buf_size}},
receive_endpoint_,
[socket = boost::intrusive_ptr<UdpSocket>(this)](
std::error_code ec, size_t size) {
Expand Down Expand Up @@ -279,8 +279,8 @@ void UdpSocket::send_next() {
if (!datagram_) {
return;
}
datagram_->async_send_to(
const_buffer(send_queue_.front().data(), send_queue_.front().size()),
datagram_->send_to(
{{send_queue_.front().data(), send_queue_.front().size()}},
send_endpoint_,
[socket = boost::intrusive_ptr<UdpSocket>(this)](
std::error_code, size_t) {
Expand Down
14 changes: 7 additions & 7 deletions net/proxy/misc/echo-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
buffer_(8192) {}

void StreamConnection::read() {
stream_->async_read_some(
buffer(buffer_.data(), buffer_.size()),
stream_->read(
{{buffer_.data(), buffer_.size()}},
[this](std::error_code ec, size_t size) {
if (ec) {
finish();
Expand All @@ -65,7 +65,7 @@ void StreamConnection::read() {
void StreamConnection::write() {
async_write(
*stream_,
buffer(buffer_.data(), size_),
const_buffer(buffer_.data(), size_),
[this](std::error_code ec, size_t) {
if (ec) {
finish();
Expand All @@ -80,8 +80,8 @@ DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(buffer_.data(), buffer_.size()),
datagram_->receive_from(
{{buffer_.data(), buffer_.size()}},
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
Expand All @@ -94,8 +94,8 @@ void DatagramConnection::read() {
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(buffer_.data(), size_),
datagram_->send_to(
{{buffer_.data(), size_}},
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
Expand Down
8 changes: 4 additions & 4 deletions net/proxy/misc/null-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
buffer_(8192) {}

void StreamConnection::read() {
stream_->async_read_some(
buffer(buffer_.data(), buffer_.size()),
stream_->read(
{{buffer_.data(), buffer_.size()}},
[this](std::error_code ec, size_t) {
if (ec) {
finish();
Expand All @@ -62,8 +62,8 @@ DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(buffer_.data(), buffer_.size()),
datagram_->receive_from(
{{buffer_.data(), buffer_.size()}},
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
Expand Down
16 changes: 8 additions & 8 deletions net/proxy/misc/random-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
}

void StreamConnection::read() {
stream_->async_read_some(
buffer(read_buffer_.data(), read_buffer_.size()),
stream_->read(
{{read_buffer_.data(), read_buffer_.size()}},
[connection = boost::intrusive_ptr<StreamConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand All @@ -71,8 +71,8 @@ void StreamConnection::read() {
}

void StreamConnection::write() {
stream_->async_write_some(
const_buffer(write_buffer_.data(), write_buffer_.size()),
stream_->write(
{{write_buffer_.data(), write_buffer_.size()}},
[connection = boost::intrusive_ptr<StreamConnection>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand All @@ -92,8 +92,8 @@ DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(read_buffer_.data(), read_buffer_.size()),
datagram_->receive_from(
{{read_buffer_.data(), read_buffer_.size()}},
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
Expand All @@ -106,8 +106,8 @@ void DatagramConnection::read() {
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(write_buffer_.data(), size_),
datagram_->send_to(
{{write_buffer_.data(), size_}},
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
Expand Down
16 changes: 8 additions & 8 deletions net/proxy/misc/zero-handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ StreamConnection::StreamConnection(std::unique_ptr<Stream> stream)
write_buffer_(8192) {}

void StreamConnection::read() {
stream_->async_read_some(
buffer(read_buffer_.data(), read_buffer_.size()),
stream_->read(
{{read_buffer_.data(), read_buffer_.size()}},
[connection = boost::intrusive_ptr<StreamConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand All @@ -68,8 +68,8 @@ void StreamConnection::read() {
}

void StreamConnection::write() {
stream_->async_write_some(
const_buffer(write_buffer_.data(), write_buffer_.size()),
stream_->write(
{{write_buffer_.data(), write_buffer_.size()}},
[connection = boost::intrusive_ptr<StreamConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand All @@ -86,8 +86,8 @@ DatagramConnection::DatagramConnection(std::unique_ptr<Datagram> datagram)
write_buffer_(8192) {}

void DatagramConnection::read() {
datagram_->async_receive_from(
buffer(read_buffer_.data(), read_buffer_.size()),
datagram_->receive_from(
{{read_buffer_.data(), read_buffer_.size()}},
endpoint_,
[this](std::error_code ec, size_t size) {
if (ec) {
Expand All @@ -100,8 +100,8 @@ void DatagramConnection::read() {
}

void DatagramConnection::write() {
datagram_->async_send_to(
const_buffer(write_buffer_.data(), size_),
datagram_->send_to(
{{write_buffer_.data(), size_}},
endpoint_,
[this](std::error_code ec, size_t) {
if (ec) {
Expand Down
8 changes: 4 additions & 4 deletions net/proxy/shadowsocks/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ void Connector::TcpStream::read_internal(
absl::FixedArray<mutable_buffer, 1> buffers_copy(
buffers.begin(), buffers.end());
BufferSpan read_buffer = decryptor_.buffer();
base_stream_->async_read_some(
buffer(read_buffer.data(), read_buffer.size()),
base_stream_->read(
{{read_buffer.data(), read_buffer.size()}},
[this, buffers = std::move(buffers_copy),
callback = std::move(callback)](
std::error_code ec, size_t size) mutable {
Expand All @@ -425,7 +425,7 @@ void Connector::TcpStream::read_internal(
return;
}
decryptor_.advance(size);
async_read_some(buffers, std::move(callback));
read(buffers, std::move(callback));
});
}

Expand All @@ -445,7 +445,7 @@ void Connector::TcpStream::write(
ConstBufferSpan write_buffer = encryptor_.buffer();
async_write(
*base_stream_,
buffer(write_buffer.data(), write_buffer.size()),
const_buffer(write_buffer.data(), write_buffer.size()),
[total_size, callback = std::move(callback)](
std::error_code ec, size_t) mutable {
if (ec) {
Expand Down
12 changes: 6 additions & 6 deletions net/proxy/shadowsocks/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ Handler::TcpConnection::TcpConnection(

void Handler::TcpConnection::forward_read() {
BufferSpan read_buffer = decryptor_.buffer();
stream_->async_read_some(
buffer(read_buffer.data(), read_buffer.size()),
stream_->read(
{{read_buffer.data(), read_buffer.size()}},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand Down Expand Up @@ -340,7 +340,7 @@ void Handler::TcpConnection::forward_parse_host(size_t header_length) {
void Handler::TcpConnection::forward_write() {
async_write(
*remote_stream_,
buffer(decryptor_.pop_buffer(read_length_), read_length_),
const_buffer(decryptor_.pop_buffer(read_length_), read_length_),
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand All @@ -354,8 +354,8 @@ void Handler::TcpConnection::forward_write() {
}

void Handler::TcpConnection::backward_read() {
remote_stream_->async_read_some(
buffer(backward_read_buffer_.data(), backward_read_buffer_.size()),
remote_stream_->read(
{{backward_read_buffer_.data(), backward_read_buffer_.size()}},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand Down Expand Up @@ -394,7 +394,7 @@ void Handler::TcpConnection::backward_write() {
ConstBufferSpan write_buffer = encryptor_.buffer();
async_write(
*stream_,
buffer(write_buffer.data(), write_buffer.size()),
const_buffer(write_buffer.data(), write_buffer.size()),
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand Down
18 changes: 8 additions & 10 deletions net/proxy/socks/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ Handler::TcpConnection::TcpConnection(
backward_buffer_(4096) {}

void Handler::TcpConnection::forward_read() {
stream_->async_read_some(
buffer(
&forward_buffer_[forward_size_],
forward_buffer_.size() - forward_size_),
stream_->read(
{{&forward_buffer_[forward_size_],
forward_buffer_.size() - forward_size_}},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand Down Expand Up @@ -245,7 +244,7 @@ void Handler::TcpConnection::connect_host(ConstBufferSpan buffer) {
void Handler::TcpConnection::forward_write() {
async_write(
*remote_stream_,
buffer(forward_buffer_.data(), forward_size_),
const_buffer(forward_buffer_.data(), forward_size_),
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand Down Expand Up @@ -275,7 +274,7 @@ void Handler::TcpConnection::reply() {
void Handler::TcpConnection::backward_write() {
async_write(
*stream_,
buffer(backward_buffer_.data(), backward_size_),
const_buffer(backward_buffer_.data(), backward_size_),
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t) {
if (ec) {
Expand All @@ -300,10 +299,9 @@ void Handler::TcpConnection::backward_dispatch() {
}

void Handler::TcpConnection::backward_read() {
remote_stream_->async_read_some(
buffer(
&backward_buffer_[backward_size_],
backward_buffer_.size() - backward_size_),
remote_stream_->read(
{{&backward_buffer_[backward_size_],
backward_buffer_.size() - backward_size_}},
[connection = boost::intrusive_ptr<TcpConnection>(this)](
std::error_code ec, size_t size) {
if (ec) {
Expand Down
12 changes: 6 additions & 6 deletions net/proxy/util/copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void CopyBidirOperation::start() {
}

void CopyBidirOperation::forward_read() {
stream0_->async_read_some(
buffer(forward_buffer_.data(), forward_buffer_.size()),
stream0_->read(
{{forward_buffer_.data(), forward_buffer_.size()}},
[this](std::error_code ec, size_t size) {
if (ec) {
finish_one(ec);
Expand All @@ -66,7 +66,7 @@ void CopyBidirOperation::forward_read() {
void CopyBidirOperation::forward_write() {
async_write(
*stream1_,
buffer(forward_buffer_.data(), forward_size_),
const_buffer(forward_buffer_.data(), forward_size_),
[this](std::error_code ec, size_t) {
if (ec) {
finish_one(ec);
Expand All @@ -77,8 +77,8 @@ void CopyBidirOperation::forward_write() {
}

void CopyBidirOperation::backward_read() {
stream1_->async_read_some(
buffer(backward_buffer_.data(), backward_buffer_.size()),
stream1_->read(
{{backward_buffer_.data(), backward_buffer_.size()}},
[this](std::error_code ec, size_t size) {
if (ec) {
finish_one(ec);
Expand All @@ -92,7 +92,7 @@ void CopyBidirOperation::backward_read() {
void CopyBidirOperation::backward_write() {
async_write(
*stream0_,
buffer(backward_buffer_.data(), backward_size_),
const_buffer(backward_buffer_.data(), backward_size_),
[this](std::error_code ec, size_t) {
if (ec) {
finish_one(ec);
Expand Down

0 comments on commit 0d71ce9

Please sign in to comment.