Skip to content

Commit

Permalink
Ensure messages are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
kognise committed Mar 22, 2022
1 parent 3384136 commit 8e22e48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
15 changes: 9 additions & 6 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Display for EtherType {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Packet {
Message(Id, String),
Message(Id, Id, String),
PresenceReq,
Presence(Id, bool, String),
Disconnect(Id),
Expand All @@ -76,7 +76,7 @@ pub enum Packet {
impl Packet {
fn tag(&self) -> u8 {
match self {
Packet::Message(_, _) => 0,
Packet::Message(_, _, _) => 0,
Packet::PresenceReq => 1,
Packet::Presence(_, _, _) => 2,
Packet::Disconnect(_) => 3,
Expand All @@ -86,10 +86,11 @@ impl Packet {
fn deserialize(tag: u8, data: &[u8]) -> Option<Self> {
match tag {
0 => {
let id: Id = data[..ID_SIZE].try_into().ok()?;
let raw_str = smaz::decompress(&data[ID_SIZE..]).ok()?;
let mid: Id = data[..ID_SIZE].try_into().ok()?;
let id: Id = data[ID_SIZE..ID_SIZE * 2].try_into().ok()?;
let raw_str = smaz::decompress(&data[ID_SIZE * 2..]).ok()?;
let str = String::from_utf8(raw_str).ok()?;
Some(Packet::Message(id, str))
Some(Packet::Message(mid, id, str))
}
1 => Some(Packet::PresenceReq),
2 => {
Expand All @@ -105,7 +106,9 @@ impl Packet {

fn serialize(&self) -> Vec<u8> {
match self {
Packet::Message(id, msg) => [id as &[u8], &smaz::compress(msg.as_bytes())].concat(),
Packet::Message(mid, id, msg) => {
[mid as &[u8], id, &smaz::compress(msg.as_bytes())].concat()
}
Packet::PresenceReq => vec![],
Packet::Presence(id, is_join, str) => {
[id as &[u8], &[*is_join as u8], str.as_bytes()].concat()
Expand Down
17 changes: 8 additions & 9 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use std::thread;

use crossbeam_channel::unbounded;
use cursive::backends::crossterm::crossterm::style::Stylize;
use cursive::traits::Nameable;
use cursive::views::{Dialog, LinearLayout, TextView};
use cursive::views::{Dialog, LinearLayout};

use self::config::CONFIG;
use self::dialog::interface::show_iface_dialog;
Expand Down Expand Up @@ -45,13 +44,13 @@ pub fn run() {
while siv.is_running() {
while let Ok(cmd) = ui_rx.try_recv() {
match cmd {
UICommand::NewMessage(username, id, msg) => {
siv.call_on_name("chat_inner", |chat_inner: &mut LinearLayout| {
chat_inner.add_child(
TextView::new(format!("[{username}] {msg}"))
.with_name(format!("{id:x?}_msg")),
);
});
UICommand::NewMessage(mid, username, msg) => {
update_or_append_txt(
&mut siv,
"chat_inner",
&format!("{mid:x?}_msg"),
format!("[{username}] {msg}"),
);
}
UICommand::UpdateUsername(new_username) => {
if new_username == username {
Expand Down
8 changes: 5 additions & 3 deletions src/ui/net_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ pub(super) fn start_net_thread(tx: Sender<UICommand>, rx: Receiver<NetCommand>)
match rx.try_recv() {
Ok(NetCommand::SetInterface(_)) => Err(ArpchatError::InterfaceAlreadySet)?,
Ok(NetCommand::SetEtherType(ether_type)) => channel.set_ether_type(ether_type),
Ok(NetCommand::SendMessage(msg)) => channel.send(Packet::Message(id, msg))?,
Ok(NetCommand::SendMessage(msg)) => {
channel.send(Packet::Message(rand::thread_rng().gen(), id, msg))?
}
Ok(NetCommand::UpdateUsername(new_username)) => {
username = new_username;
if state == NetThreadState::NeedsUsername {
Expand All @@ -75,12 +77,12 @@ pub(super) fn start_net_thread(tx: Sender<UICommand>, rx: Receiver<NetCommand>)
}

match channel.try_recv()? {
Some(Packet::Message(id, msg)) => {
Some(Packet::Message(mid, id, msg)) => {
let username = match online.get(&id) {
Some((_, username)) => username.clone(),
None => "unknown".to_string(),
};
tx.send(UICommand::NewMessage(username, id, msg)).unwrap()
tx.send(UICommand::NewMessage(mid, username, msg)).unwrap()
}
Some(Packet::PresenceReq) => {
if state == NetThreadState::NeedsInitialPresence {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum UICommand {
SendMessage(String),
SetInterface(String),
SetEtherType(EtherType),
NewMessage(String, Id, String),
NewMessage(Id, String, String),
PresenceUpdate(Id, String, bool, UpdatePresenceKind),
RemovePresence(Id, String),
Error(ArpchatError),
Expand Down

0 comments on commit 8e22e48

Please sign in to comment.