feat: use TxSendMessage
This commit is contained in:
10
src/main.rs
10
src/main.rs
@@ -15,7 +15,7 @@ use futures_util::{future, pin_mut, stream::TryStreamExt, StreamExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tungstenite::protocol::Message;
|
||||
use rust_util::XResult;
|
||||
use crate::types::{PeerMap, RoomMap, Tx};
|
||||
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
||||
use crate::msg::{RoomMessageDown, RoomMessage, RoomMessageType, RoomMessageDownType};
|
||||
use crate::slash_handles::{HandleContext, HandleTextMessage, HandleTextMessageTime};
|
||||
use crate::slash_handles::{HandleTextMessageExit, HandleTextMessageStatics};
|
||||
@@ -81,7 +81,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
||||
);
|
||||
let client_replaced_message = format!("Client replaced {:?} -> {:?}", peer_addr, addr);
|
||||
if let Some(tx) = handle_context.peer_map.lock().unwrap().remove(peer_addr) {
|
||||
tx.unbounded_send(Message::Close(None)).ok();
|
||||
tx.send_close();
|
||||
}
|
||||
client_map.insert(msg_client_id.clone(), addr);
|
||||
RoomMessageDown::create_success_reply(client_replaced_message).send(tx);
|
||||
@@ -96,7 +96,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
||||
for (client_id, client_addr) in client_map {
|
||||
if client_id != &msg_client_id {
|
||||
if let Some(client_tx) = handle_context.peer_map.lock().unwrap().get(client_addr) {
|
||||
client_tx.unbounded_send(Message::Text(mm.clone())).ok();
|
||||
client_tx.send_text(mm.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
||||
if let Some(client_map) = room_map.get_mut(room_id) {
|
||||
client_map.remove(client_id);
|
||||
handle_context.peer_map.lock().unwrap().remove(&addr);
|
||||
tx.unbounded_send(Message::Close(None)).ok();
|
||||
tx.send_close();
|
||||
} else {
|
||||
warning!("Not in room: {:?} - {:?}", room_id, client_id);
|
||||
}
|
||||
@@ -141,7 +141,7 @@ fn handle_text_message(handle_context: &mut HandleContext, tx: &Tx, addr: Socket
|
||||
if let Some(client_map) = client_map {
|
||||
for (_client_id, client_addr) in client_map {
|
||||
if let Some(client_tx) = handle_context.peer_map.lock().unwrap().remove(&client_addr) {
|
||||
client_tx.unbounded_send(Message::Close(None)).ok();
|
||||
client_tx.send_close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::types::{PeerMap, RoomMap, Tx};
|
||||
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
||||
use std::net::SocketAddr;
|
||||
use tungstenite::Message;
|
||||
use chrono::Local;
|
||||
use crate::msg::RoomMessageDown;
|
||||
use crate::{NAME, VERSION};
|
||||
use chrono::Local;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct HandleContext {
|
||||
@@ -19,7 +18,6 @@ pub trait HandleTextMessage: Sync {
|
||||
fn is_matches(&self, handle_context: &mut HandleContext, tx: &Tx, addr: SocketAddr, msg: &str) -> bool;
|
||||
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, addr: SocketAddr, msg: &str);
|
||||
}
|
||||
|
||||
pub struct HandleTextMessageExit;
|
||||
impl HandleTextMessage for HandleTextMessageExit {
|
||||
fn is_matches(&self, _handle_context: &mut HandleContext, _tx: &Tx, _addr: SocketAddr, msg: &str) -> bool {
|
||||
@@ -27,7 +25,7 @@ impl HandleTextMessage for HandleTextMessageExit {
|
||||
}
|
||||
|
||||
fn handle(&self, _handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
|
||||
tx.unbounded_send(Message::Close(None)).ok();
|
||||
tx.send_close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,13 +36,11 @@ impl HandleTextMessage for HandleTextMessageStatics {
|
||||
}
|
||||
|
||||
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
|
||||
tx.unbounded_send(Message::Text(
|
||||
format!(
|
||||
"room count: {}\npeer count: {}",
|
||||
handle_context.room_map.lock().unwrap().len(),
|
||||
handle_context.peer_map.lock().unwrap().len(),
|
||||
)
|
||||
)).ok();
|
||||
tx.send_text(format!(
|
||||
"room count: {}\npeer count: {}",
|
||||
handle_context.room_map.lock().unwrap().len(),
|
||||
handle_context.peer_map.lock().unwrap().len(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +55,9 @@ impl HandleTextMessage for HandleTextMessagePass {
|
||||
if let Some(admin_pass) = &handle_context.admin_pass {
|
||||
if admin_pass == pass {
|
||||
handle_context.is_admin = true;
|
||||
tx.unbounded_send(Message::Text("Admin password success".into())).ok();
|
||||
tx.send_text("Admin password success");
|
||||
} else {
|
||||
tx.unbounded_send(Message::Text("Admin password error".into())).ok();
|
||||
tx.send_text("Admin password error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,9 +76,9 @@ impl HandleTextMessage for HandleTextMessageRooms {
|
||||
}
|
||||
let rooms = handle_context.room_map.lock().unwrap().keys().cloned().collect::<Vec<_>>().join("\n");
|
||||
if rooms.is_empty() {
|
||||
tx.unbounded_send(Message::Text("rooms: <empty>".into())).ok();
|
||||
tx.send_text("rooms: <empty>");
|
||||
} else {
|
||||
tx.unbounded_send(Message::Text(format!("rooms:\n{}", rooms))).ok();
|
||||
tx.send_text(format!("rooms:\n{}", rooms));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,9 +101,9 @@ impl HandleTextMessage for HandleTextMessageRoom {
|
||||
for peer_client_id in client_map.keys() {
|
||||
client_ids.push(peer_client_id.clone());
|
||||
}
|
||||
tx.unbounded_send(Message::Text(format!("clients in room {}:\n{}", room_id, client_ids.join("\n")))).ok();
|
||||
tx.send_text(format!("clients in room {}:\n{}", room_id, client_ids.join("\n")));
|
||||
} else {
|
||||
tx.unbounded_send(Message::Text(format!("room not found: {}", room_id))).ok();
|
||||
tx.send_text(format!("room not found: {}", room_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,7 +119,7 @@ impl HandleTextMessage for HandleTextMessageVersion {
|
||||
RoomMessageDown::create_error_reply("Not admin").send(tx);
|
||||
return;
|
||||
}
|
||||
tx.unbounded_send(Message::Text(format!("{} - v{}", NAME, VERSION))).ok();
|
||||
tx.send_text(format!("{} - v{}", NAME, VERSION));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,9 +131,7 @@ impl HandleTextMessage for HandleTextMessageTime {
|
||||
|
||||
fn handle(&self, _handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
|
||||
simpledateformat::fmt("yyyy-MM-dd HH:mm:ss z").map(
|
||||
|f| {
|
||||
tx.unbounded_send(Message::Text(format!("Time: {}", f.format(&Local::now())))).ok();
|
||||
}
|
||||
|f| tx.send_text(format!("Time: {}", f.format(&Local::now())))
|
||||
).ok();
|
||||
}
|
||||
}
|
||||
|
||||
15
src/types.rs
15
src/types.rs
@@ -7,3 +7,18 @@ use std::sync::{Mutex, Arc};
|
||||
pub type Tx = UnboundedSender<Message>;
|
||||
pub type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>;
|
||||
pub type RoomMap = Arc<Mutex<BTreeMap<String, BTreeMap<String, SocketAddr>>>>;
|
||||
|
||||
pub trait TxSendMessage {
|
||||
fn send_close(&self) -> bool;
|
||||
fn send_text<S>(&self, text: S) -> bool where S: Into<String>;
|
||||
}
|
||||
|
||||
impl TxSendMessage for Tx {
|
||||
fn send_close(&self) -> bool {
|
||||
self.unbounded_send(Message::Close(None)).is_ok()
|
||||
}
|
||||
|
||||
fn send_text<S>(&self, text: S) -> bool where S: Into<String> {
|
||||
self.unbounded_send(Message::Text(text.into())).is_ok()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user