Fix and simplify packets
This commit is contained in:
parent
7458eac931
commit
0e6acdbff4
4 changed files with 173 additions and 24 deletions
|
|
@ -1,10 +1,10 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{ Identity, MessageId};
|
||||
use crate::{Identity, MessageId};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Message {
|
||||
pub pred: Option<MessageId>,
|
||||
pub pred: MessageId,
|
||||
pub parent: Option<MessageId>,
|
||||
pub identity: Identity,
|
||||
pub nick: String,
|
||||
|
|
@ -13,10 +13,7 @@ pub struct Message {
|
|||
|
||||
impl Message {
|
||||
pub fn id(&self) -> MessageId {
|
||||
let pred = match self.pred {
|
||||
Some(id) => format!("{id}"),
|
||||
None => "none".to_string(),
|
||||
};
|
||||
let pred = self.pred;
|
||||
let parent = match self.parent {
|
||||
Some(id) => format!("{id}"),
|
||||
None => "none".to_string(),
|
||||
|
|
|
|||
|
|
@ -29,14 +29,13 @@ pub struct NickCmd {
|
|||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum NickRpl {
|
||||
Success { you: User },
|
||||
Success,
|
||||
NickTooLong,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct SendCmd {
|
||||
pub parent: Option<MessageId>,
|
||||
pub nick: Option<String>,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
|
|
@ -49,32 +48,32 @@ pub enum SendRpl {
|
|||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct WhoCmd;
|
||||
pub struct WhoCmd {}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct WhoRpl {
|
||||
you: User,
|
||||
others: Vec<User>,
|
||||
pub you: User,
|
||||
pub others: Vec<User>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct JoinNtf {
|
||||
user: User,
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct NickNtf {
|
||||
user: User,
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PartNtf {
|
||||
user: User,
|
||||
pub user: User,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct SendNtf {
|
||||
message: Message,
|
||||
pub message: Message,
|
||||
}
|
||||
|
||||
// Create a Cmd enum for all commands, a Rpl enum for all replies and a Ntf enum
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use crate::{Identity, SessionId};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct User {
|
||||
nick: String,
|
||||
identity: Identity,
|
||||
sid: SessionId,
|
||||
pub nick: String,
|
||||
pub identity: Identity,
|
||||
pub sid: SessionId,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,165 @@
|
|||
use cove_core::packets::{
|
||||
Cmd, HelloCmd, HelloRpl, JoinNtf, NickCmd, NickNtf, NickRpl, Ntf, Packet, PartNtf, Rpl,
|
||||
SendCmd, SendNtf, SendRpl, WhoCmd, WhoRpl,
|
||||
};
|
||||
use cove_core::{Identity, Message, MessageId, SessionId, User};
|
||||
use futures::{future, StreamExt, TryStreamExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let listener = TcpListener::bind(("::0", 40080)).await.unwrap();
|
||||
while let Ok((stream, _)) = listener.accept().await {
|
||||
tokio::spawn(conn(stream));
|
||||
}
|
||||
let user = User {
|
||||
nick: "Garmy".to_string(),
|
||||
identity: Identity::of("random garbage"),
|
||||
sid: SessionId::of("12345"),
|
||||
};
|
||||
let message = Message {
|
||||
pred: MessageId::of("pred"),
|
||||
parent: None,
|
||||
identity: Identity::of("asd"),
|
||||
nick: "Foo".to_string(),
|
||||
content: "Bar".to_string(),
|
||||
};
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Cmd {
|
||||
id: 12345,
|
||||
cmd: Cmd::Hello(HelloCmd {
|
||||
nick: "Garmy".to_string(),
|
||||
identity: "random garbage".to_string()
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Hello(HelloRpl::Success {
|
||||
you: user.clone(),
|
||||
others: vec![],
|
||||
last_message: MessageId::of("Blarg")
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Hello(HelloRpl::NickTooLong)
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Cmd {
|
||||
id: 12345,
|
||||
cmd: Cmd::Nick(NickCmd {
|
||||
nick: "Garmelon".to_string()
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Nick(NickRpl::Success)
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Nick(NickRpl::NickTooLong)
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Cmd {
|
||||
id: 12345,
|
||||
cmd: Cmd::Send(SendCmd {
|
||||
parent: None,
|
||||
// parent: Some(MessageId::of("Booh!")),
|
||||
content: "Hello world!".to_string()
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Send(SendRpl::Success {
|
||||
message: message.clone()
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Send(SendRpl::ContentTooLong)
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Cmd {
|
||||
id: 12345,
|
||||
cmd: Cmd::Who(WhoCmd {})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Rpl {
|
||||
id: 67890,
|
||||
rpl: Rpl::Who(WhoRpl {
|
||||
you: user.clone(),
|
||||
others: vec![]
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Ntf {
|
||||
ntf: Ntf::Join(JoinNtf { user: user.clone() })
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Ntf {
|
||||
ntf: Ntf::Nick(NickNtf { user: user.clone() })
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Ntf {
|
||||
ntf: Ntf::Part(PartNtf { user: user.clone() })
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
println!(
|
||||
"{}",
|
||||
serde_json::to_string_pretty(&Packet::Ntf {
|
||||
ntf: Ntf::Send(SendNtf {
|
||||
message: message.clone()
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
);
|
||||
// let listener = TcpListener::bind(("::0", 40080)).await.unwrap();
|
||||
// while let Ok((stream, _)) = listener.accept().await {
|
||||
// tokio::spawn(conn(stream));
|
||||
// }
|
||||
}
|
||||
|
||||
async fn conn(stream: TcpStream) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue