mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 01:31:33 +00:00
Split ConnectionUpgrade
. (#642)
Introduce `InboundUpgrade` and `OutboundUpgrade`.
This commit is contained in:
committed by
Pierre Krieger
parent
466385a58a
commit
2e549884ef
@ -18,10 +18,12 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::protocol::{FloodsubCodec, FloodsubConfig, FloodsubRpc};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::nodes::{NodeHandlerEndpoint, ProtocolsHandler, ProtocolsHandlerEvent};
|
||||
use libp2p_core::ConnectionUpgrade;
|
||||
use protocol::{FloodsubCodec, FloodsubConfig, FloodsubRpc};
|
||||
use libp2p_core::{
|
||||
nodes::{ProtocolsHandler, ProtocolsHandlerEvent},
|
||||
upgrade::{InboundUpgrade, OutboundUpgrade}
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
use std::{fmt, io};
|
||||
use tokio_codec::Framed;
|
||||
@ -103,32 +105,39 @@ where
|
||||
type InEvent = FloodsubRpc;
|
||||
type OutEvent = FloodsubRpc;
|
||||
type Substream = TSubstream;
|
||||
type Protocol = FloodsubConfig;
|
||||
type InboundProtocol = FloodsubConfig;
|
||||
type OutboundProtocol = FloodsubConfig;
|
||||
type OutboundOpenInfo = FloodsubRpc;
|
||||
|
||||
#[inline]
|
||||
fn listen_protocol(&self) -> Self::Protocol {
|
||||
fn listen_protocol(&self) -> Self::InboundProtocol {
|
||||
self.config.clone()
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated(
|
||||
#[inline]
|
||||
fn dialer_protocol(&self) -> Self::OutboundProtocol {
|
||||
self.config.clone()
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
protocol: <Self::Protocol as ConnectionUpgrade<TSubstream>>::Output,
|
||||
endpoint: NodeHandlerEndpoint<Self::OutboundOpenInfo>,
|
||||
protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output
|
||||
) {
|
||||
if self.shutting_down {
|
||||
return;
|
||||
return ()
|
||||
}
|
||||
self.substreams.push(SubstreamState::WaitingInput(protocol))
|
||||
}
|
||||
|
||||
match endpoint {
|
||||
NodeHandlerEndpoint::Dialer(message) => {
|
||||
self.substreams
|
||||
.push(SubstreamState::PendingSend(protocol, message));
|
||||
}
|
||||
NodeHandlerEndpoint::Listener => {
|
||||
self.substreams.push(SubstreamState::WaitingInput(protocol));
|
||||
}
|
||||
fn inject_fully_negotiated_outbound(
|
||||
&mut self,
|
||||
protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
|
||||
message: Self::OutboundOpenInfo
|
||||
) {
|
||||
if self.shutting_down {
|
||||
return ()
|
||||
}
|
||||
self.substreams.push(SubstreamState::PendingSend(protocol, message))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -154,7 +163,7 @@ where
|
||||
fn poll(
|
||||
&mut self,
|
||||
) -> Poll<
|
||||
Option<ProtocolsHandlerEvent<Self::Protocol, Self::OutboundOpenInfo, Self::OutEvent>>,
|
||||
Option<ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent>>,
|
||||
io::Error,
|
||||
> {
|
||||
if !self.send_queue.is_empty() {
|
||||
|
@ -19,10 +19,10 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use crate::rpc_proto;
|
||||
use futures::future;
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PeerId};
|
||||
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, PeerId};
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
use rpc_proto;
|
||||
use std::{io, iter};
|
||||
use tokio_codec::{Decoder, Encoder, Framed};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
@ -41,29 +41,41 @@ impl FloodsubConfig {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSocket> ConnectionUpgrade<TSocket> for FloodsubConfig
|
||||
where
|
||||
TSocket: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeIdentifier)>;
|
||||
type UpgradeIdentifier = ();
|
||||
impl UpgradeInfo for FloodsubConfig {
|
||||
type UpgradeId = ();
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeId)>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/floodsub/1.0.0".into(), ()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSocket> InboundUpgrade<TSocket> for FloodsubConfig
|
||||
where
|
||||
TSocket: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Output = Framed<TSocket, FloodsubCodec>;
|
||||
type Future = future::FutureResult<Self::Output, io::Error>;
|
||||
type Error = io::Error;
|
||||
type Future = future::FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade(self, socket: TSocket, _: Self::UpgradeIdentifier, _: Endpoint) -> Self::Future {
|
||||
future::ok(Framed::new(
|
||||
socket,
|
||||
FloodsubCodec {
|
||||
length_prefix: Default::default(),
|
||||
},
|
||||
))
|
||||
fn upgrade_inbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSocket> OutboundUpgrade<TSocket> for FloodsubConfig
|
||||
where
|
||||
TSocket: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Output = Framed<TSocket, FloodsubCodec>;
|
||||
type Error = io::Error;
|
||||
type Future = future::FutureResult<Self::Output, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade_outbound(self, socket: TSocket, _: Self::UpgradeId) -> Self::Future {
|
||||
future::ok(Framed::new(socket, FloodsubCodec { length_prefix: Default::default() }))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user