Skip to content

Commit b31c9fa

Browse files
committed
Merge remote-tracking branch 'origin/master' into refactor/1
2 parents 912c998 + 3cdf68a commit b31c9fa

File tree

11 files changed

+29
-25
lines changed

11 files changed

+29
-25
lines changed

include/libp2p/multi/multiaddress.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <libp2p/multi/multiaddress_protocol_list.hpp>
1919
#include <libp2p/outcome/outcome.hpp>
2020

21+
namespace libp2p {
22+
using ProtoAddrVec = std::vector<std::pair<multi::Protocol, std::string>>;
23+
} // namespace libp2p
24+
2125
namespace libp2p::multi {
2226

2327
/**
@@ -147,8 +151,7 @@ namespace libp2p::multi {
147151
* @return list of pairs with a protocol as the first element and the value
148152
* as the second one
149153
*/
150-
std::vector<std::pair<Protocol, std::string>> getProtocolsWithValues()
151-
const;
154+
ProtoAddrVec getProtocolsWithValues() const;
152155

153156
bool operator==(const Multiaddress &other) const;
154157

include/libp2p/muxer/yamux/yamuxed_connection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ namespace libp2p::connection {
198198
/// True if waiting for current write operation to complete
199199
bool is_writing_ = false;
200200

201+
std::shared_ptr<Bytes> writing_buf_ = std::make_shared<Bytes>();
202+
201203
/// Write queue
202204
std::deque<WriteQueueItem> write_queue_;
203205

include/libp2p/transport/impl/upgrader_session.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace libp2p::transport {
1919
*/
2020
struct UpgraderSession
2121
: public std::enable_shared_from_this<UpgraderSession> {
22-
using ProtoAddrVec = std::vector<std::pair<multi::Protocol, std::string>>;
2322
using ConnectionCallback =
2423
void(outcome::result<std::shared_ptr<connection::CapableConnection>>);
2524
using HandlerFunc = std::function<ConnectionCallback>;

include/libp2p/transport/tcp/tcp_connection.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ namespace libp2p::transport {
3939
using ConnectCallback = void(const ErrorCode &, const Tcp::endpoint &);
4040
using ConnectCallbackFunc = std::function<ConnectCallback>;
4141

42-
explicit TcpConnection(boost::asio::io_context &ctx);
42+
explicit TcpConnection(boost::asio::io_context &ctx, ProtoAddrVec layers);
4343

44-
TcpConnection(boost::asio::io_context &ctx, Tcp::socket &&socket);
44+
TcpConnection(boost::asio::io_context &ctx,
45+
ProtoAddrVec layers,
46+
Tcp::socket &&socket);
4547

4648
/**
4749
* @brief Resolve service name (DNS).
@@ -122,6 +124,7 @@ namespace libp2p::transport {
122124
outcome::result<void> saveMultiaddresses();
123125

124126
boost::asio::io_context &context_;
127+
ProtoAddrVec layers_;
125128
Tcp::socket socket_;
126129
bool initiator_ = false;
127130
bool connecting_with_timeout_ = false;

include/libp2p/transport/tcp/tcp_listener.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace libp2p::transport {
2020
class TcpListener : public TransportListener,
2121
public std::enable_shared_from_this<TcpListener> {
2222
public:
23-
using ProtoAddrVec = std::vector<std::pair<multi::Protocol, std::string>>;
24-
2523
~TcpListener() override = default;
2624

2725
TcpListener(boost::asio::io_context &context,

include/libp2p/transport/tcp/tcp_util.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
namespace libp2p::transport::detail {
1717
template <typename T>
1818
inline outcome::result<multi::Multiaddress> makeAddress(
19-
T &&endpoint,
20-
const std::vector<std::pair<multi::Protocol, std::string>> *layers =
21-
nullptr) {
19+
T &&endpoint, const ProtoAddrVec *layers = nullptr) {
2220
try {
2321
auto address = endpoint.address();
2422
auto port = endpoint.port();
@@ -97,8 +95,6 @@ namespace libp2p::transport::detail {
9795
return {host, port};
9896
}
9997

100-
using ProtoAddrVec = std::vector<std::pair<multi::Protocol, std::string>>;
101-
10298
// Obtain layers string from provided address
10399
inline ProtoAddrVec getLayers(const multi::Multiaddress &address) {
104100
auto v = address.getProtocolsWithValues();

include/libp2p/transport/upgrader.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ namespace libp2p::transport {
2222
* and using the chosen protocols to actually upgrade the connections
2323
*/
2424
struct Upgrader {
25-
using ProtoAddrVec = std::vector<std::pair<multi::Protocol, std::string>>;
26-
2725
using RawSPtr = std::shared_ptr<connection::RawConnection>;
2826
using LayerSPtr = std::shared_ptr<connection::LayerConnection>;
2927
using SecSPtr = std::shared_ptr<connection::SecureConnection>;

src/muxer/yamux/yamuxed_connection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,16 +649,17 @@ namespace libp2p::connection {
649649
void YamuxedConnection::doWrite(WriteQueueItem packet) {
650650
assert(!is_writing_);
651651

652-
auto span = BytesIn(packet.packet);
652+
writing_buf_->assign(packet.packet.begin(), packet.packet.end());
653653
auto cb = [wptr{weak_from_this()},
654+
buf{writing_buf_},
654655
packet = std::move(packet)](outcome::result<size_t> res) {
655656
if (auto self = wptr.lock()) {
656657
self->onDataWritten(res, packet.stream_id);
657658
}
658659
};
659660

660661
is_writing_ = true;
661-
writeReturnSize(connection_, span, cb);
662+
writeReturnSize(connection_, *writing_buf_, cb);
662663
}
663664

664665
void YamuxedConnection::onDataWritten(outcome::result<size_t> res,

src/transport/tcp/tcp_connection.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ namespace libp2p::transport {
2323
} // namespace
2424

2525
TcpConnection::TcpConnection(boost::asio::io_context &ctx,
26+
ProtoAddrVec layers,
2627
boost::asio::ip::tcp::socket &&socket)
2728
: context_(ctx),
29+
layers_{std::move(layers)},
2830
socket_(std::move(socket)),
2931
connection_phase_done_{false},
3032
deadline_timer_(context_) {
3133
std::ignore = saveMultiaddresses();
3234
}
3335

34-
TcpConnection::TcpConnection(boost::asio::io_context &ctx)
36+
TcpConnection::TcpConnection(boost::asio::io_context &ctx,
37+
ProtoAddrVec layers)
3538
: context_(ctx),
39+
layers_{std::move(layers)},
3640
socket_(context_),
3741
connection_phase_done_{false},
3842
deadline_timer_(context_) {}
@@ -281,15 +285,15 @@ namespace libp2p::transport {
281285
if (!local_multiaddress_) {
282286
auto endpoint(socket_.local_endpoint(ec));
283287
if (!ec) {
284-
OUTCOME_TRY(addr, detail::makeAddress(endpoint));
285-
local_multiaddress_ = std::move(addr);
288+
BOOST_OUTCOME_TRY(local_multiaddress_,
289+
detail::makeAddress(endpoint, &layers_));
286290
}
287291
}
288292
if (!remote_multiaddress_) {
289293
auto endpoint(socket_.remote_endpoint(ec));
290294
if (!ec) {
291-
OUTCOME_TRY(addr, detail::makeAddress(endpoint));
292-
remote_multiaddress_ = std::move(addr);
295+
BOOST_OUTCOME_TRY(remote_multiaddress_,
296+
detail::makeAddress(endpoint, &layers_));
293297
}
294298
}
295299
} else {

src/transport/tcp/tcp_listener.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ namespace libp2p::transport {
9595
return self->handle_(ec);
9696
}
9797

98-
auto conn =
99-
std::make_shared<TcpConnection>(self->context_, std::move(sock));
98+
auto conn = std::make_shared<TcpConnection>(
99+
self->context_, self->layers_, std::move(sock));
100100

101101
auto session = std::make_shared<UpgraderSession>(
102102
self->upgrader_, self->layers_, std::move(conn), self->handle_);

0 commit comments

Comments
 (0)