Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse newStream #2337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 27 additions & 37 deletions core/network/impl/protocols/request_response_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "network/impl/protocols/protocol_base_impl.hpp"

#include "common/main_thread_pool.hpp"
#include "network/helpers/new_stream.hpp"
#include "protocol_error.hpp"
#include "utils/box.hpp"

Expand Down Expand Up @@ -124,43 +125,32 @@ namespace kagome::network {
protocolName(),
peer_id);

auto addresses_res =
base_.host().getPeerRepository().getAddressRepository().getAddresses(
peer_id);
if (not addresses_res.has_value()) {
main_pool_handler_->execute(
[cb(std::move(cb)), addresses_res(std::move(addresses_res))] {
cb(addresses_res.as_failure());
});
return;
}

base_.host().newStream(
PeerInfo{peer_id, std::move(addresses_res.value())},
base_.protocolIds(),
[wptr{this->weak_from_this()}, peer_id, cb{std::move(cb)}](
auto &&stream_and_proto) mutable {
if (!stream_and_proto.has_value()) {
cb(stream_and_proto.as_failure());
return;
}

auto &stream = stream_and_proto.value().stream;
BOOST_ASSERT(stream);

auto self = wptr.lock();
if (not self) {
cb(ProtocolError::GONE);
self->base_.closeStream(std::move(wptr), std::move(stream));
return;
}

SL_DEBUG(self->base_.logger(),
"Established connection over {} stream with {}",
self->protocolName(),
peer_id);
cb(std::move(stream));
});
newStream(base_.host(),
peer_id,
base_.protocolIds(),
[wptr{this->weak_from_this()}, peer_id, cb{std::move(cb)}](
auto &&stream_and_proto) mutable {
if (!stream_and_proto.has_value()) {
cb(stream_and_proto.as_failure());
return;
}

auto &stream = stream_and_proto.value().stream;
BOOST_ASSERT(stream);

auto self = wptr.lock();
if (not self) {
cb(ProtocolError::GONE);
self->base_.closeStream(std::move(wptr), std::move(stream));
return;
}

SL_DEBUG(self->base_.logger(),
"Established connection over {} stream with {}",
self->protocolName(),
peer_id);
cb(std::move(stream));
});
}

template <typename M>
Expand Down
4 changes: 3 additions & 1 deletion core/network/impl/protocols/state_protocol_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "network/adapters/protobuf_state_request.hpp"
#include "network/adapters/protobuf_state_response.hpp"
#include "network/common.hpp"
#include "network/helpers/new_stream.hpp"
#include "network/helpers/protobuf_message_read_writer.hpp"
#include "network/impl/protocols/protocol_error.hpp"

Expand Down Expand Up @@ -45,7 +46,8 @@ namespace kagome::network {
protocolName(),
peer_id);

base_.host().newStream(
newStream(
base_.host(),
peer_id,
base_.protocolIds(),
[wp{weak_from_this()}, peer_id, cb = std::move(cb)](
Expand Down
14 changes: 4 additions & 10 deletions core/network/impl/protocols/sync_protocol_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "network/adapters/protobuf_block_request.hpp"
#include "network/adapters/protobuf_block_response.hpp"
#include "network/common.hpp"
#include "network/helpers/new_stream.hpp"
#include "network/helpers/protobuf_message_read_writer.hpp"
#include "network/impl/protocols/protocol_error.hpp"
#include "network/rpc.hpp"
Expand Down Expand Up @@ -160,16 +161,9 @@ namespace kagome::network {
protocolName(),
peer_id);

auto addresses_res =
base_.host().getPeerRepository().getAddressRepository().getAddresses(
peer_id);
if (not addresses_res.has_value()) {
cb(addresses_res.as_failure());
return;
}

base_.host().newStream(
PeerInfo{peer_id, std::move(addresses_res.value())},
newStream(
base_.host(),
peer_id,
base_.protocolIds(),
[wp{weak_from_this()}, peer_id, cb = std::move(cb)](
auto &&stream_res) mutable {
Expand Down
13 changes: 2 additions & 11 deletions core/network/notifications/connect_and_handshake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include "network/helpers/new_stream.hpp"
#include "network/helpers/stream_read_buffer.hpp"
#include "network/impl/protocols/protocol_base_impl.hpp"
#include "network/notifications/handshake.hpp"
Expand Down Expand Up @@ -64,16 +65,6 @@ namespace kagome::network::notifications {
std::move(stream), std::move(frame_stream), handshake, std::move(cb));
};

auto addresses_res =
base.host().getPeerRepository().getAddressRepository().getAddresses(
peer_id);
if (not addresses_res.has_value()) {
cb(addresses_res.as_failure());
return;
}

base.host().newStream(PeerInfo{peer_id, std::move(addresses_res.value())},
base.protocolIds(),
std::move(cb));
newHost(base.host(), peer_id, base.protocolIds(), std::move(cb));
}
} // namespace kagome::network::notifications
Loading