6 #ifndef KAGOME_NETWORK_PROTOCOLBASEIMPL 7 #define KAGOME_NETWORK_PROTOCOLBASEIMPL 15 #include <libp2p/host/host.hpp> 16 #include <libp2p/peer/stream_protocols.hpp> 38 std::string
const &log_section)
44 bool start(std::weak_ptr<T> wptr) {
45 host_.setProtocolHandler(
47 [log{
logger()}, wp(std::move(wptr))](
auto &&stream_and_proto) {
49 if (
auto peer_id = stream_and_proto.stream->remotePeerId()) {
51 "Handled {} protocol stream from: {}",
52 stream_and_proto.protocol,
53 peer_id.value().toBase58());
54 if (
auto self = wp.lock()) {
55 self->onIncomingStream(std::move(stream_and_proto.stream));
59 log->warn(
"Handled {} protocol stream from unknown peer",
60 stream_and_proto.protocol);
62 stream_and_proto.stream->close(
63 [stream{stream_and_proto.stream}](
auto &&) {});
85 void closeStream(std::weak_ptr<T> wptr, std::shared_ptr<Stream> stream) {
87 stream->close([log{
logger()}, wptr, stream](
auto &&result) {
88 if (
auto self = wptr.lock()) {
91 "Stream {} was not closed successfully with {}",
93 stream->remotePeerId().value());
97 "Stream {} with {} was closed.",
99 stream->remotePeerId().value());
113 #endif // KAGOME_NETWORK_PROTOCOLBASEIMPL
log::Logger const & logger() const
void closeStream(std::weak_ptr< T > wptr, std::shared_ptr< Stream > stream)
~ProtocolBaseImpl()=default
ProtocolBaseImpl()=delete
void streamReadBuffer(libp2p::StreamAndProtocol &result)
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::Protocol Protocol
libp2p::StreamProtocols Protocols
libp2p::peer::PeerId PeerId
std::shared_ptr< soralog::Logger > Logger
Protocols const protocols_
ProtocolBaseImpl(libp2p::Host &host, Protocols const &protocols, std::string const &log_section)
Protocols const & protocolIds() const
libp2p::connection::Stream Stream
bool start(std::weak_ptr< T > wptr)
Logger createLogger(const std::string &tag)