From fd398aff4afd26cf391ff7f3da7eafc6fb88f401 Mon Sep 17 00:00:00 2001 From: bismuth01 Date: Thu, 10 Jul 2025 19:21:25 +0530 Subject: [PATCH 1/3] Error in variant_reader --- src/basic/varint_reader.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/basic/varint_reader.cpp b/src/basic/varint_reader.cpp index f996c3ba9..7b076d7f7 100644 --- a/src/basic/varint_reader.cpp +++ b/src/basic/varint_reader.cpp @@ -5,6 +5,7 @@ */ #include +#include #include @@ -44,9 +45,9 @@ namespace libp2p::basic { } // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - conn->read( + readReturnSize( + conn, std::span(varint_buf->data() + current_length, 1), - 1, [c = std::move(conn), cb = std::move(cb), current_length, varint_buf]( auto &&res) mutable { if (not res.has_value()) { From 448efa9b7e72706244692886673aaeea0b22b9bf Mon Sep 17 00:00:00 2001 From: bismuth01 Date: Thu, 10 Jul 2025 19:12:46 +0000 Subject: [PATCH 2/3] Remove Reader::read --- src/basic/message_read_writer_bigendian.cpp | 9 +++--- src/basic/message_read_writer_uvarint.cpp | 5 ++-- src/basic/varint_reader.cpp | 2 +- src/muxer/mplex/mplex_frame.cpp | 19 ++++++------ src/muxer/mplex/mplexed_connection.cpp | 3 +- src/protocol/gossip/impl/stream.cpp | 19 ++++++------ src/protocol/ping/ping_client_session.cpp | 11 +++---- src/protocol/ping/ping_server_session.cpp | 16 +++++----- .../multiselect/multiselect_instance.cpp | 21 ++++++------- .../multiselect/simple_stream_negotiate.cpp | 9 +++--- .../plaintext/plaintext_connection.cpp | 3 +- src/security/secio/secio.cpp | 5 ++-- .../p2p/host/protocol/client_test_session.cpp | 21 ++++++------- test/acceptance/p2p/muxer.cpp | 24 ++++++++------- .../loopback_stream/loopback_stream_test.cpp | 11 ++++--- .../plaintext_connection_test.cpp | 3 +- test/libp2p/muxer/muxers_and_streams_test.cpp | 6 ++-- .../transport/tcp/tcp_integration_test.cpp | 30 ++++++++++--------- .../connection/capable_connection_mock.hpp | 3 +- 19 files changed, 121 insertions(+), 99 deletions(-) diff --git a/src/basic/message_read_writer_bigendian.cpp b/src/basic/message_read_writer_bigendian.cpp index f7a848cad..a00d48205 100644 --- a/src/basic/message_read_writer_bigendian.cpp +++ b/src/basic/message_read_writer_bigendian.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -24,9 +25,9 @@ namespace libp2p::basic { void MessageReadWriterBigEndian::read(ReadCallbackFunc cb) { auto buffer = std::make_shared>(); buffer->resize(kLenMarkerSize); - conn_->read( + readReturnSize( + conn_, *buffer, - kLenMarkerSize, [self{shared_from_this()}, buffer, cb{std::move(cb)}](auto &&result) { if (not result) { return cb(result.error()); @@ -35,8 +36,8 @@ namespace libp2p::basic { common::convert(buffer->data())); buffer->resize(msg_len); std::fill(buffer->begin(), buffer->end(), 0u); - self->conn_->read( - *buffer, msg_len, [self, buffer, cb](auto &&result) { + readReturnSize( + self->conn_, *buffer, [self, buffer, cb](auto &&result) { if (not result) { return cb(result.error()); } diff --git a/src/basic/message_read_writer_uvarint.cpp b/src/basic/message_read_writer_uvarint.cpp index 26871ca36..23d687cec 100644 --- a/src/basic/message_read_writer_uvarint.cpp +++ b/src/basic/message_read_writer_uvarint.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -34,9 +35,9 @@ namespace libp2p::basic { auto msg_len = varint_res.value().toUInt64(); if (0 != msg_len) { auto buffer = std::make_shared>(msg_len, 0); - self->conn_->read( + readReturnSize( + self->conn_, *buffer, - msg_len, [self, buffer, cb = std::move(cb)](auto &&res) mutable { if (!res) { return cb(res.error()); diff --git a/src/basic/varint_reader.cpp b/src/basic/varint_reader.cpp index 7b076d7f7..55439aa20 100644 --- a/src/basic/varint_reader.cpp +++ b/src/basic/varint_reader.cpp @@ -48,7 +48,7 @@ namespace libp2p::basic { readReturnSize( conn, std::span(varint_buf->data() + current_length, 1), - [c = std::move(conn), cb = std::move(cb), current_length, varint_buf]( + [c = conn, cb = std::move(cb), current_length, varint_buf]( auto &&res) mutable { if (not res.has_value()) { return cb(res.error()); diff --git a/src/muxer/mplex/mplex_frame.cpp b/src/muxer/mplex/mplex_frame.cpp index 0f2125f8b..6272bfdb9 100644 --- a/src/muxer/mplex/mplex_frame.cpp +++ b/src/muxer/mplex/mplex_frame.cpp @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -100,16 +101,16 @@ namespace libp2p::connection { // read data std::shared_ptr data = std::make_shared(length, 0); - reader->read(*data, - length, - [id_flag, data, cb{std::move(cb)}]( - auto &&read_res) mutable { - if (!read_res) { - return cb(read_res.error()); - } + readReturnSize(reader, + *data, + [id_flag, data, cb{std::move(cb)}]( + auto &&read_res) mutable { + if (!read_res) { + return cb(read_res.error()); + } - cb(createFrame(id_flag, std::move(*data))); - }); + cb(createFrame(id_flag, std::move(*data))); + }); }); }); } diff --git a/src/muxer/mplex/mplexed_connection.cpp b/src/muxer/mplex/mplexed_connection.cpp index 339b38620..47bdda970 100644 --- a/src/muxer/mplex/mplexed_connection.cpp +++ b/src/muxer/mplex/mplexed_connection.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -127,7 +128,7 @@ namespace libp2p::connection { void MplexedConnection::read(BytesOut out, size_t bytes, ReadCallbackFunc cb) { - connection_->read(out, bytes, std::move(cb)); + readReturnSize(connection_, out, std::move(cb)); } void MplexedConnection::readSome(BytesOut out, diff --git a/src/protocol/gossip/impl/stream.cpp b/src/protocol/gossip/impl/stream.cpp index 98baf3a74..e24b230a5 100644 --- a/src/protocol/gossip/impl/stream.cpp +++ b/src/protocol/gossip/impl/stream.cpp @@ -8,6 +8,7 @@ #include +#include #include #include @@ -83,15 +84,15 @@ namespace libp2p::protocol::gossip { read_buffer_->resize(msg_len); - stream_->read(std::span(read_buffer_->data(), msg_len), - msg_len, - [self_wptr = weak_from_this(), this, buffer = read_buffer_]( - auto &&res) { - if (self_wptr.expired()) { - return; - } - onMessageRead(std::forward(res)); - }); + readReturnSize(stream_, + std::span(read_buffer_->data(), msg_len), + [self_wptr = weak_from_this(), this, buffer = read_buffer_]( + auto &&res) { + if (self_wptr.expired()) { + return; + } + onMessageRead(std::forward(res)); + }); } void Stream::onMessageRead(outcome::result res) { diff --git a/src/protocol/ping/ping_client_session.cpp b/src/protocol/ping/ping_client_session.cpp index 57b47d307..58c6c40bc 100644 --- a/src/protocol/ping/ping_client_session.cpp +++ b/src/protocol/ping/ping_client_session.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -84,11 +85,11 @@ namespace libp2p::protocol { }, config_.timeout); - stream_->read(read_buffer_, - config_.message_size, - [self{shared_from_this()}](outcome::result r) { - self->readCompleted(r); - }); + readReturnSize(stream_, + read_buffer_, + [self{shared_from_this()}](outcome::result r) { + self->readCompleted(r); + }); } void PingClientSession::readCompleted(outcome::result r) { diff --git a/src/protocol/ping/ping_server_session.cpp b/src/protocol/ping/ping_server_session.cpp index dea980bee..d5de0231a 100644 --- a/src/protocol/ping/ping_server_session.cpp +++ b/src/protocol/ping/ping_server_session.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -31,14 +32,13 @@ namespace libp2p::protocol { return; } - stream_->read(buffer_, - config_.message_size, - [self{shared_from_this()}](auto &&read_res) { - if (!read_res) { - return; - } - self->readCompleted(); - }); + readReturnSize( + stream_, buffer_, [self{shared_from_this()}](auto &&read_res) { + if (!read_res) { + return; + } + self->readCompleted(); + }); } void PingServerSession::readCompleted() { diff --git a/src/protocol_muxer/multiselect/multiselect_instance.cpp b/src/protocol_muxer/multiselect/multiselect_instance.cpp index f48a0d0e9..701f7c093 100644 --- a/src/protocol_muxer/multiselect/multiselect_instance.cpp +++ b/src/protocol_muxer/multiselect/multiselect_instance.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -213,16 +214,16 @@ namespace libp2p::protocol_muxer::multiselect { BytesOut span(*read_buffer_); span = span.first(static_cast(bytes_needed)); - connection_->read(span, - bytes_needed, - [wptr = weak_from_this(), - round = current_round_, - packet = read_buffer_](outcome::result res) { - auto self = wptr.lock(); - if (self && self->current_round_ == round) { - self->onDataRead(res); - } - }); + readReturnSize(connection_, + span, + [wptr = weak_from_this(), + round = current_round_, + packet = read_buffer_](outcome::result res) { + auto self = wptr.lock(); + if (self && self->current_round_ == round) { + self->onDataRead(res); + } + }); } void MultiselectInstance::onDataRead(outcome::result res) { diff --git a/src/protocol_muxer/multiselect/simple_stream_negotiate.cpp b/src/protocol_muxer/multiselect/simple_stream_negotiate.cpp index 849f9f59d..0345d0931 100644 --- a/src/protocol_muxer/multiselect/simple_stream_negotiate.cpp +++ b/src/protocol_muxer/multiselect/simple_stream_negotiate.cpp @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -76,9 +77,9 @@ namespace libp2p::protocol_muxer::multiselect { // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions) span = span.subspan(kMaxVarintSize, remaining_bytes); - stream->read( + readReturnSize( + stream, span, - span.size(), [stream = stream, cb = std::move(cb), buffers = std::move(buffers)]( outcome::result res) mutable { onLastBytesRead(std::move(stream), cb, *buffers, res); @@ -100,9 +101,9 @@ namespace libp2p::protocol_muxer::multiselect { BytesOut span(buffers->read); span = span.first(kMaxVarintSize); - stream->read( + readReturnSize( + stream, span, - span.size(), [stream = stream, cb = std::move(cb), buffers = std::move(buffers)]( outcome::result res) mutable { onFirstBytesRead(stream, std::move(cb), std::move(buffers), res); diff --git a/src/security/plaintext/plaintext_connection.cpp b/src/security/plaintext/plaintext_connection.cpp index 0985fab61..30e4490e3 100644 --- a/src/security/plaintext/plaintext_connection.cpp +++ b/src/security/plaintext/plaintext_connection.cpp @@ -7,6 +7,7 @@ #include #include +#include #include namespace libp2p::connection { @@ -60,7 +61,7 @@ namespace libp2p::connection { void PlaintextConnection::read(BytesOut in, size_t bytes, Reader::ReadCallbackFunc f) { - return original_connection_->read(in, bytes, std::move(f)); + return readReturnSize(original_connection_, in, std::move(f)); }; void PlaintextConnection::readSome(BytesOut in, diff --git a/src/security/secio/secio.cpp b/src/security/secio/secio.cpp index 4b83d9106..bfc267ef0 100644 --- a/src/security/secio/secio.cpp +++ b/src/security/secio/secio.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -265,9 +266,9 @@ namespace libp2p::security { } const auto kToRead{self->propose_message_.rand.size()}; auto buffer = std::make_shared(kToRead); - secio_conn->read( + readReturnSize( + secio_conn, *buffer, - kToRead, [self, cb, conn, secio_conn, buffer](auto &&read_res) { SECIO_OUTCOME_TRY(read_bytes, read_res, conn, cb) if (read_bytes != buffer->size() diff --git a/test/acceptance/p2p/host/protocol/client_test_session.cpp b/test/acceptance/p2p/host/protocol/client_test_session.cpp index e7dc3f6fd..cb0988401 100644 --- a/test/acceptance/p2p/host/protocol/client_test_session.cpp +++ b/test/acceptance/p2p/host/protocol/client_test_session.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -53,16 +54,16 @@ namespace libp2p::protocol { read_buf_ = std::vector(buffer_size_); - stream_->read(read_buf_, - buffer_size_, - [self = shared_from_this(), - cb{std::move(cb)}](outcome::result rr) mutable { - if (!rr) { - return cb(rr.error()); - } - cb(std::move(self->read_buf_)); - return self->write(std::move(cb)); - }); + readReturnSize(stream_, + read_buf_, + [self = shared_from_this(), + cb{std::move(cb)}](outcome::result rr) mutable { + if (!rr) { + return cb(rr.error()); + } + cb(std::move(self->read_buf_)); + return self->write(std::move(cb)); + }); } } // namespace libp2p::protocol diff --git a/test/acceptance/p2p/muxer.cpp b/test/acceptance/p2p/muxer.cpp index 89f5d1107..a0c67e261 100644 --- a/test/acceptance/p2p/muxer.cpp +++ b/test/acceptance/p2p/muxer.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -255,19 +256,20 @@ struct Client : public std::enable_shared_from_this { auto readbuf = std::make_shared>(); readbuf->resize(write); - stream->read(*readbuf, - readbuf->size(), - [round, streamId, write, buf, readbuf, stream, this]( - outcome::result rread) { - ASSERT_OUTCOME_SUCCESS(read, rread); - this->println(streamId, " readSome ", read, " bytes"); - this->streamReads++; + readReturnSize(stream, + *readbuf, + [round, streamId, write, buf, readbuf, stream, this]( + outcome::result rread) { + ASSERT_OUTCOME_SUCCESS(read, rread); + this->println( + streamId, " readSome ", read, " bytes"); + this->streamReads++; - ASSERT_EQ(write, read); - ASSERT_EQ(*buf, *readbuf); + ASSERT_EQ(write, read); + ASSERT_EQ(*buf, *readbuf); - this->onStream(streamId, round - 1, stream); - }); + this->onStream(streamId, round - 1, stream); + }); }); } diff --git a/test/libp2p/connection/loopback_stream/loopback_stream_test.cpp b/test/libp2p/connection/loopback_stream/loopback_stream_test.cpp index 6fa75ae2f..fa06a0509 100644 --- a/test/libp2p/connection/loopback_stream/loopback_stream_test.cpp +++ b/test/libp2p/connection/loopback_stream/loopback_stream_test.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -42,8 +43,10 @@ class LoopbackStreamTest : public testing::Test { * @then exactly the same data can be read from the stream */ TEST_F(LoopbackStreamTest, Basic) { - ASSERT_OUTCOME_SUCCESS(hash, Multihash::create(libp2p::multi::sha256, kBuffer)); - ASSERT_OUTCOME_SUCCESS(peer_id, PeerId::fromBase58(encodeBase58(hash.toBuffer()))); + ASSERT_OUTCOME_SUCCESS(hash, + Multihash::create(libp2p::multi::sha256, kBuffer)); + ASSERT_OUTCOME_SUCCESS(peer_id, + PeerId::fromBase58(encodeBase58(hash.toBuffer()))); std::shared_ptr stream = std::make_shared(PeerInfo{peer_id, {}}, context); @@ -55,9 +58,9 @@ TEST_F(LoopbackStreamTest, Basic) { auto read_buf = std::make_shared(kBufferSize, 0); ASSERT_EQ(read_buf->size(), kBufferSize); ASSERT_NE(*read_buf, buf); - stream->read( + libp2p::readReturnSize( + stream, *read_buf, - kBufferSize, [buf = read_buf, source_buf = buf, &all_executed](auto result) { ASSERT_OUTCOME_SUCCESS(bytes, result); ASSERT_EQ(bytes, kBufferSize); diff --git a/test/libp2p/connection/security_conn/plaintext_connection_test.cpp b/test/libp2p/connection/security_conn/plaintext_connection_test.cpp index 9ba1a5b9e..eec964198 100644 --- a/test/libp2p/connection/security_conn/plaintext_connection_test.cpp +++ b/test/libp2p/connection/security_conn/plaintext_connection_test.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -124,7 +125,7 @@ TEST_F(PlaintextConnectionTest, Read) { const int size = 100; EXPECT_CALL_READ(*connection_).WILL_READ_SIZE(size); auto buf = std::make_shared>(size, 0); - secure_connection_->read(*buf, size, [size, buf](auto &&res) { + libp2p::readReturnSize(secure_connection_, *buf, [size, buf](auto &&res) { ASSERT_OUTCOME_SUCCESS(res); ASSERT_EQ(res.value(), size); }); diff --git a/test/libp2p/muxer/muxers_and_streams_test.cpp b/test/libp2p/muxer/muxers_and_streams_test.cpp index c09d0b306..a622c7dd7 100644 --- a/test/libp2p/muxer/muxers_and_streams_test.cpp +++ b/test/libp2p/muxer/muxers_and_streams_test.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -173,8 +174,9 @@ namespace libp2p::regression { return behavior_(*this); } // clang-format off - stream->read( - *read_buf_, read_buf_->size(), + readReturnSize( + stream, + *read_buf_, [wptr = weak_from_this(), buf = read_buf_] (auto res) { auto self = wptr.lock(); if (self) { self->onRead(res); } diff --git a/test/libp2p/transport/tcp/tcp_integration_test.cpp b/test/libp2p/transport/tcp/tcp_integration_test.cpp index cb967a4fa..e156969b9 100644 --- a/test/libp2p/transport/tcp/tcp_integration_test.cpp +++ b/test/libp2p/transport/tcp/tcp_integration_test.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -44,7 +45,8 @@ namespace { EXPECT_OUTCOME_SUCCESS(mar, conn->remoteMultiaddr()); EXPECT_OUTCOME_SUCCESS(mal, conn->localMultiaddr()); std::ostringstream s; - s << mar.value().getStringAddress() << " -> " << mal.value().getStringAddress(); + s << mar.value().getStringAddress() << " -> " + << mal.value().getStringAddress(); std::cout << s.str() << '\n'; return conn; @@ -185,14 +187,13 @@ TEST(TCP, SingleListenerCanAcceptManyClients) { conn, *buf, [conn, readback, buf, context](auto &&res) { ASSERT_OUTCOME_SUCCESS(res); ASSERT_EQ(res.value(), buf->size()); - conn->read(*readback, - readback->size(), - [conn, readback, buf, context](auto &&res) { - context->stop(); - ASSERT_OUTCOME_SUCCESS(res); - ASSERT_EQ(res.value(), readback->size()); - ASSERT_EQ(*buf, *readback); - }); + libp2p::readReturnSize( + conn, *readback, [conn, readback, buf, context](auto &&res) { + context->stop(); + ASSERT_OUTCOME_SUCCESS(res); + ASSERT_EQ(res.value(), readback->size()); + ASSERT_EQ(*buf, *readback); + }); }); }); @@ -344,11 +345,12 @@ TEST(TCP, OneTransportServerHandlesManyClients) { conn, *buf, [conn, kSize, readback, buf](auto &&res) { ASSERT_OUTCOME_SUCCESS(res); ASSERT_EQ(res.value(), buf->size()); - conn->read(*readback, kSize, [conn, readback, buf](auto &&res) { - ASSERT_OUTCOME_SUCCESS(res); - ASSERT_EQ(res.value(), readback->size()); - ASSERT_EQ(*buf, *readback); - }); + libp2p::readReturnSize( + conn, *readback, [conn, readback, buf](auto &&res) { + ASSERT_OUTCOME_SUCCESS(res); + ASSERT_EQ(res.value(), readback->size()); + ASSERT_EQ(*buf, *readback); + }); }); }); diff --git a/test/mock/libp2p/connection/capable_connection_mock.hpp b/test/mock/libp2p/connection/capable_connection_mock.hpp index e6d40968f..cb2ea9e4e 100644 --- a/test/mock/libp2p/connection/capable_connection_mock.hpp +++ b/test/mock/libp2p/connection/capable_connection_mock.hpp @@ -8,6 +8,7 @@ #include +#include #include namespace libp2p::connection { @@ -82,7 +83,7 @@ namespace libp2p::connection { }; void read(BytesOut in, size_t bytes, Reader::ReadCallbackFunc f) override { - return real_->read(in, bytes, f); + return readReturnSize(real_, in, f); }; void readSome(BytesOut in, From f80c8e02227df8c7a33559b123b49ed9af140398 Mon Sep 17 00:00:00 2001 From: bismuth01 Date: Fri, 11 Jul 2025 08:53:56 +0000 Subject: [PATCH 3/3] Removed Reader::read interface --- include/libp2p/basic/reader.hpp | 14 -------------- include/libp2p/connection/loopback_stream.hpp | 2 -- include/libp2p/layer/websocket/ssl_connection.hpp | 1 - include/libp2p/layer/websocket/ws_connection.hpp | 2 -- include/libp2p/muxer/mplex/mplex_stream.hpp | 2 -- include/libp2p/muxer/mplex/mplexed_connection.hpp | 1 - include/libp2p/muxer/yamux/yamux_stream.hpp | 2 -- include/libp2p/muxer/yamux/yamuxed_connection.hpp | 1 - include/libp2p/security/noise/noise_connection.hpp | 2 -- .../security/plaintext/plaintext_connection.hpp | 2 -- include/libp2p/security/secio/secio_connection.hpp | 2 -- include/libp2p/transport/quic/connection.hpp | 1 - include/libp2p/transport/quic/stream.hpp | 1 - include/libp2p/transport/tcp/tcp_connection.hpp | 2 -- src/connection/loopback_stream.cpp | 7 ------- src/layer/websocket/ssl_connection.cpp | 7 ------- src/layer/websocket/ws_connection.cpp | 8 -------- src/muxer/mplex/mplex_stream.cpp | 5 ----- src/muxer/mplex/mplexed_connection.cpp | 6 ------ src/muxer/yamux/yamux_stream.cpp | 5 ----- src/muxer/yamux/yamuxed_connection.cpp | 7 ------- src/security/noise/noise_connection.cpp | 7 ------- src/security/plaintext/plaintext_connection.cpp | 6 ------ src/security/secio/secio_connection.cpp | 7 ------- src/security/tls/tls_connection.cpp | 8 -------- src/security/tls/tls_connection.hpp | 3 --- src/transport/quic/connection.cpp | 4 ---- src/transport/quic/stream.cpp | 7 ------- src/transport/tcp/tcp_connection.cpp | 8 -------- .../libp2p/connection/capable_connection_mock.hpp | 4 ---- .../libp2p/connection/layer_connection_mock.hpp | 6 ------ test/mock/libp2p/connection/stream_mock.hpp | 6 ------ 32 files changed, 146 deletions(-) diff --git a/include/libp2p/basic/reader.hpp b/include/libp2p/basic/reader.hpp index 661e03f55..a6cf95b72 100644 --- a/include/libp2p/basic/reader.hpp +++ b/include/libp2p/basic/reader.hpp @@ -20,20 +20,6 @@ namespace libp2p::basic { virtual ~Reader() = default; - /** - * @brief Reads exactly {@code} min(out.size(), bytes) {@nocode} bytes to - * the buffer. - * @param out output argument. Read data will be written to this buffer. - * @param bytes number of bytes to read - * @param cb callback with result of operation - * - * @note caller should maintain validity of an output buffer until callback - * is executed. It is usually done with either wrapping buffer as shared - * pointer, or having buffer as part of some class/struct, and using - * enable_shared_from_this() - */ - virtual void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) = 0; - /** * @brief Reads up to {@code} min(out.size(), bytes) {@nocode} bytes to the * buffer. diff --git a/include/libp2p/connection/loopback_stream.hpp b/include/libp2p/connection/loopback_stream.hpp index 948ecc5df..325d75ede 100644 --- a/include/libp2p/connection/loopback_stream.hpp +++ b/include/libp2p/connection/loopback_stream.hpp @@ -40,8 +40,6 @@ namespace libp2p::connection { const override; protected: - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void writeSome(BytesIn in, size_t bytes, WriteCallbackFunc cb) override; diff --git a/include/libp2p/layer/websocket/ssl_connection.hpp b/include/libp2p/layer/websocket/ssl_connection.hpp index 549368485..45e619602 100644 --- a/include/libp2p/layer/websocket/ssl_connection.hpp +++ b/include/libp2p/layer/websocket/ssl_connection.hpp @@ -32,7 +32,6 @@ namespace libp2p::connection { outcome::result close() override; bool isClosed() const override; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, ReadCallbackFunc cb) override; diff --git a/include/libp2p/layer/websocket/ws_connection.hpp b/include/libp2p/layer/websocket/ws_connection.hpp index 139fc69c5..418983c55 100644 --- a/include/libp2p/layer/websocket/ws_connection.hpp +++ b/include/libp2p/layer/websocket/ws_connection.hpp @@ -68,8 +68,6 @@ namespace libp2p::connection { bool isClosed() const override; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/muxer/mplex/mplex_stream.hpp b/include/libp2p/muxer/mplex/mplex_stream.hpp index bd696c2be..0a642faf5 100644 --- a/include/libp2p/muxer/mplex/mplex_stream.hpp +++ b/include/libp2p/muxer/mplex/mplex_stream.hpp @@ -50,8 +50,6 @@ namespace libp2p::connection { ~MplexStream() override = default; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/muxer/mplex/mplexed_connection.hpp b/include/libp2p/muxer/mplex/mplexed_connection.hpp index ea5ac229f..08cc6583a 100644 --- a/include/libp2p/muxer/mplex/mplexed_connection.hpp +++ b/include/libp2p/muxer/mplex/mplexed_connection.hpp @@ -64,7 +64,6 @@ namespace libp2p::connection { /// usage of these four methods is highly not recommended or even forbidden: /// use stream over this connection instead - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void writeSome(BytesIn in, size_t bytes, WriteCallbackFunc cb) override; diff --git a/include/libp2p/muxer/yamux/yamux_stream.hpp b/include/libp2p/muxer/yamux/yamux_stream.hpp index 4d4ad0484..bc66ad39b 100644 --- a/include/libp2p/muxer/yamux/yamux_stream.hpp +++ b/include/libp2p/muxer/yamux/yamux_stream.hpp @@ -54,8 +54,6 @@ namespace libp2p::connection { size_t maximum_window_size, size_t write_queue_limit); - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/muxer/yamux/yamuxed_connection.hpp b/include/libp2p/muxer/yamux/yamuxed_connection.hpp index 29e869375..7a95e0072 100644 --- a/include/libp2p/muxer/yamux/yamuxed_connection.hpp +++ b/include/libp2p/muxer/yamux/yamuxed_connection.hpp @@ -111,7 +111,6 @@ namespace libp2p::connection { /// usage of these four methods is highly not recommended or even forbidden: /// use stream over this connection instead - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void writeSome(BytesIn in, size_t bytes, WriteCallbackFunc cb) override; diff --git a/include/libp2p/security/noise/noise_connection.hpp b/include/libp2p/security/noise/noise_connection.hpp index fa77c56d1..5be3357a0 100644 --- a/include/libp2p/security/noise/noise_connection.hpp +++ b/include/libp2p/security/noise/noise_connection.hpp @@ -38,8 +38,6 @@ namespace libp2p::connection { outcome::result close() override; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/security/plaintext/plaintext_connection.hpp b/include/libp2p/security/plaintext/plaintext_connection.hpp index a147ac3d7..d30580f8c 100644 --- a/include/libp2p/security/plaintext/plaintext_connection.hpp +++ b/include/libp2p/security/plaintext/plaintext_connection.hpp @@ -35,8 +35,6 @@ namespace libp2p::connection { outcome::result remoteMultiaddr() override; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/security/secio/secio_connection.hpp b/include/libp2p/security/secio/secio_connection.hpp index af6c7d70e..740785f27 100644 --- a/include/libp2p/security/secio/secio_connection.hpp +++ b/include/libp2p/security/secio/secio_connection.hpp @@ -97,8 +97,6 @@ namespace libp2p::connection { outcome::result remoteMultiaddr() override; - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/include/libp2p/transport/quic/connection.hpp b/include/libp2p/transport/quic/connection.hpp index b510c46af..c363b3a35 100644 --- a/include/libp2p/transport/quic/connection.hpp +++ b/include/libp2p/transport/quic/connection.hpp @@ -39,7 +39,6 @@ namespace libp2p::transport { void operator=(QuicConnection &&) = delete; // Reader - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, ReadCallbackFunc cb) override; diff --git a/include/libp2p/transport/quic/stream.hpp b/include/libp2p/transport/quic/stream.hpp index 42ccc9b8a..ca5aa1821 100644 --- a/include/libp2p/transport/quic/stream.hpp +++ b/include/libp2p/transport/quic/stream.hpp @@ -32,7 +32,6 @@ namespace libp2p::connection { void operator=(QuicStream &&) = delete; // Reader - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, ReadCallbackFunc cb) override; diff --git a/include/libp2p/transport/tcp/tcp_connection.hpp b/include/libp2p/transport/tcp/tcp_connection.hpp index 57274d07f..e40ed9613 100644 --- a/include/libp2p/transport/tcp/tcp_connection.hpp +++ b/include/libp2p/transport/tcp/tcp_connection.hpp @@ -61,8 +61,6 @@ namespace libp2p::transport { ConnectCallbackFunc cb, std::chrono::milliseconds timeout); - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; void deferReadCallback(outcome::result res, diff --git a/src/connection/loopback_stream.cpp b/src/connection/loopback_stream.cpp index 8cc92ff1e..2f7a81566 100644 --- a/src/connection/loopback_stream.cpp +++ b/src/connection/loopback_stream.cpp @@ -77,13 +77,6 @@ namespace libp2p::connection { return outcome::success(own_peer_info_.addresses.front()); } - void LoopbackStream::read(BytesOut out, - size_t bytes, - libp2p::basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void LoopbackStream::writeSome(BytesIn in, size_t bytes, libp2p::basic::Writer::WriteCallbackFunc cb) { diff --git a/src/layer/websocket/ssl_connection.cpp b/src/layer/websocket/ssl_connection.cpp index 96d16b1f8..caccd0226 100644 --- a/src/layer/websocket/ssl_connection.cpp +++ b/src/layer/websocket/ssl_connection.cpp @@ -43,13 +43,6 @@ namespace libp2p::connection { return connection_->close(); } - void SslConnection::read(BytesOut out, - size_t bytes, - libp2p::basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void SslConnection::readSome(BytesOut out, size_t bytes, libp2p::basic::Reader::ReadCallbackFunc cb) { diff --git a/src/layer/websocket/ws_connection.cpp b/src/layer/websocket/ws_connection.cpp index aca11b629..497f2446c 100644 --- a/src/layer/websocket/ws_connection.cpp +++ b/src/layer/websocket/ws_connection.cpp @@ -85,14 +85,6 @@ namespace libp2p::connection { return connection_->close(); } - void WsConnection::read(BytesOut out, - size_t bytes, - libp2p::basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - SL_TRACE(log_, "read {} bytes", bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void WsConnection::readSome(BytesOut out, size_t bytes, libp2p::basic::Reader::ReadCallbackFunc cb) { diff --git a/src/muxer/mplex/mplex_stream.cpp b/src/muxer/mplex/mplex_stream.cpp index abe4ec921..1e2720eaa 100644 --- a/src/muxer/mplex/mplex_stream.cpp +++ b/src/muxer/mplex/mplex_stream.cpp @@ -34,11 +34,6 @@ namespace libp2p::connection { StreamId stream_id) : connection_{std::move(connection)}, stream_id_{stream_id} {} - void MplexStream::read(BytesOut out, size_t bytes, ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void MplexStream::readDone(outcome::result res) { auto cb{std::move(reading_->cb)}; reading_.reset(); diff --git a/src/muxer/mplex/mplexed_connection.cpp b/src/muxer/mplex/mplexed_connection.cpp index 47bdda970..ed20e4fb2 100644 --- a/src/muxer/mplex/mplexed_connection.cpp +++ b/src/muxer/mplex/mplexed_connection.cpp @@ -125,12 +125,6 @@ namespace libp2p::connection { return !is_active_ || connection_->isClosed(); } - void MplexedConnection::read(BytesOut out, - size_t bytes, - ReadCallbackFunc cb) { - readReturnSize(connection_, out, std::move(cb)); - } - void MplexedConnection::readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) { diff --git a/src/muxer/yamux/yamux_stream.cpp b/src/muxer/yamux/yamux_stream.cpp index f622b2f7e..f17d8afbc 100644 --- a/src/muxer/yamux/yamux_stream.cpp +++ b/src/muxer/yamux/yamux_stream.cpp @@ -67,11 +67,6 @@ namespace libp2p::connection { assert(write_queue_limit >= maximum_window_size_); } - void YamuxStream::read(BytesOut out, size_t bytes, ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void YamuxStream::readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) { ambigousSize(out, bytes); doRead(out, std::move(cb)); diff --git a/src/muxer/yamux/yamuxed_connection.cpp b/src/muxer/yamux/yamuxed_connection.cpp index 6dc79b610..d6a634f3a 100644 --- a/src/muxer/yamux/yamuxed_connection.cpp +++ b/src/muxer/yamux/yamuxed_connection.cpp @@ -163,13 +163,6 @@ namespace libp2p::connection { return !started_ || connection_->isClosed(); } - void YamuxedConnection::read(BytesOut out, - size_t bytes, - ReadCallbackFunc cb) { - log()->error("YamuxedConnection::read : invalid direct call"); - deferReadCallback(Error::CONNECTION_DIRECT_IO_FORBIDDEN, std::move(cb)); - } - void YamuxedConnection::readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) { diff --git a/src/security/noise/noise_connection.cpp b/src/security/noise/noise_connection.cpp index 7092f0e1c..dec92734b 100644 --- a/src/security/noise/noise_connection.cpp +++ b/src/security/noise/noise_connection.cpp @@ -46,13 +46,6 @@ namespace libp2p::connection { return connection_->close(); } - void NoiseConnection::read(BytesOut out, - size_t bytes, - libp2p::basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void NoiseConnection::readSome(BytesOut out, size_t bytes, libp2p::basic::Reader::ReadCallbackFunc cb) { diff --git a/src/security/plaintext/plaintext_connection.cpp b/src/security/plaintext/plaintext_connection.cpp index 30e4490e3..fe4f189cb 100644 --- a/src/security/plaintext/plaintext_connection.cpp +++ b/src/security/plaintext/plaintext_connection.cpp @@ -58,12 +58,6 @@ namespace libp2p::connection { return original_connection_->remoteMultiaddr(); } - void PlaintextConnection::read(BytesOut in, - size_t bytes, - Reader::ReadCallbackFunc f) { - return readReturnSize(original_connection_, in, std::move(f)); - }; - void PlaintextConnection::readSome(BytesOut in, size_t bytes, Reader::ReadCallbackFunc f) { diff --git a/src/security/secio/secio_connection.cpp b/src/security/secio/secio_connection.cpp index 520ed1bcc..5af7060e1 100644 --- a/src/security/secio/secio_connection.cpp +++ b/src/security/secio/secio_connection.cpp @@ -183,13 +183,6 @@ namespace libp2p::connection { } } - void SecioConnection::read(BytesOut out, - size_t bytes, - basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - libp2p::readReturnSize(shared_from_this(), out, std::move(cb)); - } - void SecioConnection::readSome(BytesOut out, size_t bytes, basic::Reader::ReadCallbackFunc cb) { diff --git a/src/security/tls/tls_connection.cpp b/src/security/tls/tls_connection.cpp index 5d4b54a59..cadb162d4 100644 --- a/src/security/tls/tls_connection.cpp +++ b/src/security/tls/tls_connection.cpp @@ -140,14 +140,6 @@ namespace libp2p::connection { }; } - void TlsConnection::read(BytesOut out, - size_t bytes, - Reader::ReadCallbackFunc f) { - ambigousSize(out, bytes); - SL_TRACE(log(), "reading {} bytes", bytes); - readReturnSize(shared_from_this(), out, std::move(f)); - } - void TlsConnection::readSome(BytesOut out, size_t bytes, Reader::ReadCallbackFunc cb) { diff --git a/src/security/tls/tls_connection.hpp b/src/security/tls/tls_connection.hpp index 41d7e3326..4618361e9 100644 --- a/src/security/tls/tls_connection.hpp +++ b/src/security/tls/tls_connection.hpp @@ -76,9 +76,6 @@ namespace libp2p::connection { /// Returns remote network address outcome::result remoteMultiaddr() override; - /// Async reads exactly the # of bytes given - void read(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; - /// Async reads up to the # of bytes given void readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) override; diff --git a/src/transport/quic/connection.cpp b/src/transport/quic/connection.cpp index 11ce0e9ab..92d57b2b8 100644 --- a/src/transport/quic/connection.cpp +++ b/src/transport/quic/connection.cpp @@ -32,10 +32,6 @@ namespace libp2p::transport { std::ignore = close(); } - void QuicConnection::read(BytesOut out, size_t bytes, ReadCallbackFunc cb) { - throw std::logic_error{"QuicConnection::read must not be called"}; - } - void QuicConnection::readSome(BytesOut out, size_t bytes, ReadCallbackFunc cb) { diff --git a/src/transport/quic/stream.cpp b/src/transport/quic/stream.cpp index 15066a475..5c1fb9cc6 100644 --- a/src/transport/quic/stream.cpp +++ b/src/transport/quic/stream.cpp @@ -26,13 +26,6 @@ namespace libp2p::connection { reset(); } - void QuicStream::read(BytesOut out, - size_t bytes, - basic::Reader::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void QuicStream::readSome(BytesOut out, size_t bytes, basic::Reader::ReadCallbackFunc cb) { diff --git a/src/transport/tcp/tcp_connection.cpp b/src/transport/tcp/tcp_connection.cpp index 9ac7767f8..8c987548d 100644 --- a/src/transport/tcp/tcp_connection.cpp +++ b/src/transport/tcp/tcp_connection.cpp @@ -172,14 +172,6 @@ namespace libp2p::transport { }); } - void TcpConnection::read(BytesOut out, - size_t bytes, - TcpConnection::ReadCallbackFunc cb) { - ambigousSize(out, bytes); - TRACE("{} read {}", debug_str_, bytes); - readReturnSize(shared_from_this(), out, std::move(cb)); - } - void TcpConnection::readSome(BytesOut out, size_t bytes, TcpConnection::ReadCallbackFunc cb) { diff --git a/test/mock/libp2p/connection/capable_connection_mock.hpp b/test/mock/libp2p/connection/capable_connection_mock.hpp index cb2ea9e4e..713a745ba 100644 --- a/test/mock/libp2p/connection/capable_connection_mock.hpp +++ b/test/mock/libp2p/connection/capable_connection_mock.hpp @@ -82,10 +82,6 @@ namespace libp2p::connection { return real_->remoteMultiaddr(); }; - void read(BytesOut in, size_t bytes, Reader::ReadCallbackFunc f) override { - return readReturnSize(real_, in, f); - }; - void readSome(BytesOut in, size_t bytes, Reader::ReadCallbackFunc f) override { diff --git a/test/mock/libp2p/connection/layer_connection_mock.hpp b/test/mock/libp2p/connection/layer_connection_mock.hpp index 3380ee558..2df74a252 100644 --- a/test/mock/libp2p/connection/layer_connection_mock.hpp +++ b/test/mock/libp2p/connection/layer_connection_mock.hpp @@ -21,12 +21,6 @@ namespace libp2p::connection { MOCK_METHOD0(close, outcome::result()); - void read(BytesOut out, - size_t ambigous_size, - Reader::ReadCallbackFunc cb) override { - ASSERT_EQ(out.size(), ambigous_size); - readReturnSize(shared_from_this(), out, cb); - } MOCK_METHOD3(readSome, void(BytesOut, size_t, Reader::ReadCallbackFunc)); MOCK_METHOD3(writeSome, void(BytesIn, size_t, Writer::WriteCallbackFunc)); MOCK_METHOD2(deferReadCallback, diff --git a/test/mock/libp2p/connection/stream_mock.hpp b/test/mock/libp2p/connection/stream_mock.hpp index ce70a3b89..33928b92a 100644 --- a/test/mock/libp2p/connection/stream_mock.hpp +++ b/test/mock/libp2p/connection/stream_mock.hpp @@ -27,12 +27,6 @@ namespace libp2p::connection { MOCK_METHOD1(close, void(VoidResultHandlerFunc)); - void read(BytesOut out, - size_t ambigous_size, - Reader::ReadCallbackFunc cb) override { - ASSERT_EQ(out.size(), ambigous_size); - readReturnSize(shared_from_this(), out, cb); - } MOCK_METHOD3(readSome, void(BytesOut, size_t, Reader::ReadCallbackFunc)); MOCK_METHOD3(writeSome, void(BytesIn, size_t, Writer::WriteCallbackFunc));