Introduce transcoder parameter to Wire & Transport

This commit is contained in:
Dr. Maxim Orlovsky 2022-11-14 10:22:30 +01:00 committed by Alexis Sellier
parent 6b87eed670
commit 56fb4d67a9
No known key found for this signature in database
4 changed files with 40 additions and 28 deletions

View File

@ -10,6 +10,7 @@ use crate::clock::RefClock;
use crate::profile::Profile;
use crate::service::routing;
use crate::transport::Transport;
use crate::wire::transcoder::PlainTranscoder;
use crate::wire::Wire;
use crate::{address, service};
@ -130,9 +131,11 @@ impl<R: Reactor> Client<R> {
rng,
);
let transcode = PlainTranscoder::default();
self.reactor.run(
&config.listen,
Transport::new(Wire::new(service)),
Transport::new(Wire::new(service, transcode)),
self.events,
self.commands,
)?;

View File

@ -11,6 +11,7 @@ use crate::crypto;
use crate::service::routing;
use crate::service::{Command, DisconnectReason, Event, Service};
use crate::storage::WriteStorage;
use crate::wire::transcoder::Transcode;
use crate::wire::Wire;
#[derive(Debug)]
@ -19,13 +20,13 @@ struct Peer {
}
#[derive(Debug)]
pub struct Transport<R, S, T, G> {
pub struct Transport<R, S, W, G, T: Transcode> {
_peers: HashMap<net::IpAddr, Peer>,
inner: Wire<R, S, T, G>,
inner: Wire<R, S, W, G, T>,
}
impl<R, S, T, G> Transport<R, S, T, G> {
pub fn new(inner: Wire<R, S, T, G>) -> Self {
impl<R, S, W, G, T: Transcode> Transport<R, S, W, G, T> {
pub fn new(inner: Wire<R, S, W, G, T>) -> Self {
Self {
_peers: HashMap::default(),
inner,
@ -33,12 +34,13 @@ impl<R, S, T, G> Transport<R, S, T, G> {
}
}
impl<R, S, T, G> nakamoto::Protocol for Transport<R, S, T, G>
impl<R, S, W, G, T> nakamoto::Protocol for Transport<R, S, W, G, T>
where
R: routing::Store,
T: WriteStorage + 'static,
W: WriteStorage + 'static,
S: address::Store,
G: crypto::Signer,
T: Transcode,
{
type Event = Event;
type Command = Command;
@ -86,7 +88,7 @@ where
}
}
impl<R, S, T, G> Iterator for Transport<R, S, T, G> {
impl<R, S, W, G, T: Transcode> Iterator for Transport<R, S, W, G, T> {
type Item = Io<Event, DisconnectReason>;
fn next(&mut self) -> Option<Self::Item> {
@ -94,15 +96,15 @@ impl<R, S, T, G> Iterator for Transport<R, S, T, G> {
}
}
impl<R, S, T, G> Deref for Transport<R, S, T, G> {
type Target = Service<R, S, T, G>;
impl<R, S, W, G, T: Transcode> Deref for Transport<R, S, W, G, T> {
type Target = Service<R, S, W, G>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<R, S, T, G> DerefMut for Transport<R, S, T, G> {
impl<R, S, W, G, T: Transcode> DerefMut for Transport<R, S, W, G, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}

View File

@ -1,4 +1,5 @@
pub mod message;
pub mod transcoder;
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
@ -25,6 +26,7 @@ use crate::service::{filter, routing};
use crate::storage::refs::Refs;
use crate::storage::refs::SignedRefs;
use crate::storage::WriteStorage;
use crate::wire::transcoder::Transcode;
/// The default type we use to represent sizes on the wire.
///
@ -423,33 +425,32 @@ impl Decode for node::Features {
}
#[derive(Debug)]
pub struct Wire<R, S, T, G> {
pub struct Wire<R, S, W, G, T: Transcode> {
inboxes: HashMap<net::SocketAddr, Decoder>,
inner: service::Service<R, S, T, G>,
inner: service::Service<R, S, W, G>,
#[allow(dead_code)]
transcoder: T,
}
impl<R, S, T, G> Wire<R, S, T, G> {
pub fn new(inner: service::Service<R, S, T, G>) -> Self {
impl<R, S, W, G, T: Transcode> Wire<R, S, W, G, T> {
pub fn new(inner: service::Service<R, S, W, G>, transcoder: T) -> Self {
Self {
inboxes: HashMap::new(),
inner,
transcoder,
}
}
}
impl<R, S, T, G> Wire<R, S, T, G>
impl<R, S, W, G, T> Wire<R, S, W, G, T>
where
R: routing::Store,
S: address::Store,
T: WriteStorage + 'static,
W: WriteStorage + 'static,
G: Signer,
T: Transcode,
{
pub fn connected(
&mut self,
addr: net::SocketAddr,
local_addr: &net::SocketAddr,
link: Link,
) {
pub fn connected(&mut self, addr: net::SocketAddr, local_addr: &net::SocketAddr, link: Link) {
self.inboxes.insert(addr, Decoder::new(256));
self.inner.connected(addr, local_addr, link)
}
@ -459,7 +460,7 @@ where
addr: &net::SocketAddr,
reason: nakamoto::DisconnectReason<service::DisconnectReason>,
) {
self.inboxes.remove(&addr);
self.inboxes.remove(addr);
self.inner.disconnected(addr, &reason)
}
@ -486,7 +487,7 @@ where
}
}
impl<R, S, T, G> Iterator for Wire<R, S, T, G> {
impl<R, S, W, G, T: Transcode> Iterator for Wire<R, S, W, G, T> {
type Item = nakamoto::Io<service::Event, service::DisconnectReason>;
fn next(&mut self) -> Option<Self::Item> {
@ -511,15 +512,15 @@ impl<R, S, T, G> Iterator for Wire<R, S, T, G> {
}
}
impl<R, S, T, G> Deref for Wire<R, S, T, G> {
type Target = service::Service<R, S, T, G>;
impl<R, S, W, G, T: Transcode> Deref for Wire<R, S, W, G, T> {
type Target = service::Service<R, S, W, G>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<R, S, T, G> DerefMut for Wire<R, S, T, G> {
impl<R, S, W, G, T: Transcode> DerefMut for Wire<R, S, W, G, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}

View File

@ -0,0 +1,6 @@
pub trait Transcode {}
#[derive(Debug, Default)]
pub struct PlainTranscoder;
impl Transcode for PlainTranscoder {}