Rename: In --> C2S, Out --> S2C
This commit is contained in:
parent
8dbbfd922a
commit
ef658be8e4
|
@ -6,7 +6,7 @@ use futures::SinkExt;
|
|||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut frames = Framed::new(TcpStream::connect("127.0.0.1:8001").await?,
|
||||
packets::Codec::<packets::Out, packets::In>::standard());
|
||||
frames.send(packets::In::Connect(Value::from("producer-consumer-example").wrap())).await?;
|
||||
packets::Codec::<packets::S2C, packets::C2S>::standard());
|
||||
frames.send(packets::C2S::Connect(Value::from("producer-consumer-example").wrap())).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ pub type DataspaceError = (String, V);
|
|||
|
||||
#[derive(Debug)]
|
||||
struct Actor {
|
||||
tx: UnboundedSender<packets::Out>,
|
||||
tx: UnboundedSender<packets::S2C>,
|
||||
endpoints: Map<EndpointName, ActorEndpoint>,
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ impl Dataspace {
|
|||
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));
|
||||
self.peers.insert(id, Actor {
|
||||
tx,
|
||||
|
@ -137,7 +137,7 @@ impl Dataspace {
|
|||
|
||||
fn deliver_outbound_turns(&mut self, outbound_turns: Map<ConnId, Vec<packets::Event>>) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ pub enum Event {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub enum In {
|
||||
pub enum C2S {
|
||||
Connect(V),
|
||||
Turn(Vec<Action>),
|
||||
Ping(),
|
||||
|
@ -35,7 +35,7 @@ pub enum In {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub enum Out {
|
||||
pub enum S2C {
|
||||
Err(String, V),
|
||||
Turn(Vec<Event>),
|
||||
Ping(),
|
||||
|
|
26
src/peer.rs
26
src/peer.rs
|
@ -18,14 +18,14 @@ use tokio_util::codec::Framed;
|
|||
|
||||
pub struct Peer {
|
||||
id: ConnId,
|
||||
tx: UnboundedSender<packets::Out>,
|
||||
rx: UnboundedReceiver<packets::Out>,
|
||||
frames: Framed<TcpStream, packets::Codec<packets::In, packets::Out>>,
|
||||
tx: UnboundedSender<packets::S2C>,
|
||||
rx: UnboundedReceiver<packets::S2C>,
|
||||
frames: Framed<TcpStream, packets::Codec<packets::C2S, packets::S2C>>,
|
||||
space: Option<dataspace::DataspaceRef>,
|
||||
}
|
||||
|
||||
fn err(s: &str, ctx: V) -> packets::Out {
|
||||
packets::Out::Err(s.into(), ctx)
|
||||
fn err(s: &str, ctx: V) -> packets::S2C {
|
||||
packets::S2C::Err(s.into(), ctx)
|
||||
}
|
||||
|
||||
impl Peer {
|
||||
|
@ -39,7 +39,7 @@ impl Peer {
|
|||
println!("{:?}: {:?}", self.id, &self.frames.get_ref());
|
||||
|
||||
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
|
||||
} else {
|
||||
let e: String = format!("Expected initial Connect, got {:?}", firstpacket);
|
||||
|
@ -57,13 +57,13 @@ impl Peer {
|
|||
while running {
|
||||
let mut to_send = Vec::new();
|
||||
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 {
|
||||
Some(res) => match res {
|
||||
Ok(p) => {
|
||||
println!("{:?}: input {:?}", self.id, &p);
|
||||
match p {
|
||||
packets::In::Turn(actions) => {
|
||||
packets::C2S::Turn(actions) => {
|
||||
match self.space.as_ref().unwrap().write().unwrap()
|
||||
.turn(self.id, actions)
|
||||
{
|
||||
|
@ -74,11 +74,11 @@ impl Peer {
|
|||
}
|
||||
}
|
||||
}
|
||||
packets::In::Ping() =>
|
||||
to_send.push(packets::Out::Pong()),
|
||||
packets::In::Pong() =>
|
||||
packets::C2S::Ping() =>
|
||||
to_send.push(packets::S2C::Pong()),
|
||||
packets::C2S::Pong() =>
|
||||
(),
|
||||
packets::In::Connect(_) => {
|
||||
packets::C2S::Connect(_) => {
|
||||
to_send.push(err("Unexpected Connect", value::to_value(p).unwrap()));
|
||||
running = false;
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ impl Peer {
|
|||
},
|
||||
}
|
||||
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);
|
||||
} else {
|
||||
println!("{:?}: output {:?}", self.id, &v);
|
||||
|
|
Loading…
Reference in New Issue