Rename: In --> C2S, Out --> S2C

This commit is contained in:
Tony Garnock-Jones 2020-05-11 22:08:27 +02:00
parent 8dbbfd922a
commit ef658be8e4
4 changed files with 20 additions and 20 deletions

View File

@ -6,7 +6,7 @@ use futures::SinkExt;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut frames = Framed::new(TcpStream::connect("127.0.0.1:8001").await?, let mut frames = Framed::new(TcpStream::connect("127.0.0.1:8001").await?,
packets::Codec::<packets::Out, packets::In>::standard()); packets::Codec::<packets::S2C, packets::C2S>::standard());
frames.send(packets::In::Connect(Value::from("producer-consumer-example").wrap())).await?; frames.send(packets::C2S::Connect(Value::from("producer-consumer-example").wrap())).await?;
Ok(()) Ok(())
} }

View File

@ -12,7 +12,7 @@ pub type DataspaceError = (String, V);
#[derive(Debug)] #[derive(Debug)]
struct Actor { struct Actor {
tx: UnboundedSender<packets::Out>, tx: UnboundedSender<packets::S2C>,
endpoints: Map<EndpointName, ActorEndpoint>, endpoints: Map<EndpointName, ActorEndpoint>,
} }
@ -38,7 +38,7 @@ impl Dataspace {
Arc::new(RwLock::new(Self::new(name))) Arc::new(RwLock::new(Self::new(name)))
} }
pub fn register(&mut self, id: ConnId, tx: UnboundedSender<packets::Out>) { pub fn register(&mut self, id: ConnId, tx: UnboundedSender<packets::S2C>) {
assert!(!self.peers.contains_key(&id)); assert!(!self.peers.contains_key(&id));
self.peers.insert(id, Actor { self.peers.insert(id, Actor {
tx, tx,
@ -137,7 +137,7 @@ impl Dataspace {
fn deliver_outbound_turns(&mut self, outbound_turns: Map<ConnId, Vec<packets::Event>>) { fn deliver_outbound_turns(&mut self, outbound_turns: Map<ConnId, Vec<packets::Event>>) {
for (target, events) in outbound_turns { for (target, events) in outbound_turns {
let _ = self.peers.get_mut(&target).unwrap().tx.send(packets::Out::Turn(events)); let _ = self.peers.get_mut(&target).unwrap().tx.send(packets::S2C::Turn(events));
} }
} }
} }

View File

@ -27,7 +27,7 @@ pub enum Event {
} }
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum In { pub enum C2S {
Connect(V), Connect(V),
Turn(Vec<Action>), Turn(Vec<Action>),
Ping(), Ping(),
@ -35,7 +35,7 @@ pub enum In {
} }
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum Out { pub enum S2C {
Err(String, V), Err(String, V),
Turn(Vec<Event>), Turn(Vec<Event>),
Ping(), Ping(),

View File

@ -18,14 +18,14 @@ use tokio_util::codec::Framed;
pub struct Peer { pub struct Peer {
id: ConnId, id: ConnId,
tx: UnboundedSender<packets::Out>, tx: UnboundedSender<packets::S2C>,
rx: UnboundedReceiver<packets::Out>, rx: UnboundedReceiver<packets::S2C>,
frames: Framed<TcpStream, packets::Codec<packets::In, packets::Out>>, frames: Framed<TcpStream, packets::Codec<packets::C2S, packets::S2C>>,
space: Option<dataspace::DataspaceRef>, space: Option<dataspace::DataspaceRef>,
} }
fn err(s: &str, ctx: V) -> packets::Out { fn err(s: &str, ctx: V) -> packets::S2C {
packets::Out::Err(s.into(), ctx) packets::S2C::Err(s.into(), ctx)
} }
impl Peer { impl Peer {
@ -39,7 +39,7 @@ impl Peer {
println!("{:?}: {:?}", self.id, &self.frames.get_ref()); println!("{:?}: {:?}", self.id, &self.frames.get_ref());
let firstpacket = self.frames.next().await; let firstpacket = self.frames.next().await;
let dsname = if let Some(Ok(packets::In::Connect(dsname))) = firstpacket { let dsname = if let Some(Ok(packets::C2S::Connect(dsname))) = firstpacket {
dsname dsname
} else { } else {
let e: String = format!("Expected initial Connect, got {:?}", firstpacket); let e: String = format!("Expected initial Connect, got {:?}", firstpacket);
@ -57,13 +57,13 @@ impl Peer {
while running { while running {
let mut to_send = Vec::new(); let mut to_send = Vec::new();
select! { select! {
_instant = ping_timer.next().boxed().fuse() => to_send.push(packets::Out::Ping()), _instant = ping_timer.next().boxed().fuse() => to_send.push(packets::S2C::Ping()),
frame = self.frames.next().boxed().fuse() => match frame { frame = self.frames.next().boxed().fuse() => match frame {
Some(res) => match res { Some(res) => match res {
Ok(p) => { Ok(p) => {
println!("{:?}: input {:?}", self.id, &p); println!("{:?}: input {:?}", self.id, &p);
match p { match p {
packets::In::Turn(actions) => { packets::C2S::Turn(actions) => {
match self.space.as_ref().unwrap().write().unwrap() match self.space.as_ref().unwrap().write().unwrap()
.turn(self.id, actions) .turn(self.id, actions)
{ {
@ -74,11 +74,11 @@ impl Peer {
} }
} }
} }
packets::In::Ping() => packets::C2S::Ping() =>
to_send.push(packets::Out::Pong()), to_send.push(packets::S2C::Pong()),
packets::In::Pong() => packets::C2S::Pong() =>
(), (),
packets::In::Connect(_) => { packets::C2S::Connect(_) => {
to_send.push(err("Unexpected Connect", value::to_value(p).unwrap())); to_send.push(err("Unexpected Connect", value::to_value(p).unwrap()));
running = false; running = false;
} }
@ -110,7 +110,7 @@ impl Peer {
}, },
} }
for v in to_send { for v in to_send {
if let packets::Out::Err(ref msg, ref ctx) = v { if let packets::S2C::Err(ref msg, ref ctx) = v {
println!("{:?}: connection crashed: {}; context {:?}", self.id, msg, ctx); println!("{:?}: connection crashed: {}; context {:?}", self.id, msg, ctx);
} else { } else {
println!("{:?}: output {:?}", self.id, &v); println!("{:?}: output {:?}", self.id, &v);