mirror of
https://github.com/MeexReay/bRAC.git
synced 2025-05-06 13:38:04 +03:00
message storing refactor
This commit is contained in:
parent
0d03824d02
commit
ce63efeba6
@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
error::Error, io::{stdin, stdout, BufRead, Write}, sync::{Arc, RwLock}
|
error::Error, io::{stdin, stdout, BufRead, Write}, sync::{atomic::AtomicUsize, Arc, RwLock}
|
||||||
};
|
};
|
||||||
|
|
||||||
use colored::Color;
|
use colored::Color;
|
||||||
@ -149,14 +149,14 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if args.read_messages {
|
if args.read_messages {
|
||||||
print!("{}", read_messages(&config.host).expect("Error reading messages"));
|
print!("{}", read_messages(&config.host, config.max_messages).expect("Error reading messages").0.join("\n"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let disable_formatting = args.disable_formatting;
|
let disable_formatting = args.disable_formatting;
|
||||||
let disable_commands = args.disable_commands;
|
let disable_commands = args.disable_commands;
|
||||||
|
|
||||||
let messages = Arc::new(RwLock::new(String::new()));
|
let messages = Arc::new((RwLock::new(Vec::new()), AtomicUsize::new(0)));
|
||||||
let input = Arc::new(RwLock::new(String::new()));
|
let input = Arc::new(RwLock::new(String::new()));
|
||||||
let config = Arc::new(config);
|
let config = Arc::new(config);
|
||||||
|
|
||||||
|
25
src/rac.rs
25
src/rac.rs
@ -1,4 +1,4 @@
|
|||||||
use std::{error::Error, io::{Read, Write}, net::TcpStream, sync::{Arc, RwLock}, thread, time::Duration};
|
use std::{error::Error, io::{Read, Write}, net::TcpStream, sync::{atomic::{AtomicUsize, Ordering}, Arc, RwLock}, thread, time::Duration};
|
||||||
|
|
||||||
use crate::{config::Config, term::print_console, ADVERTISEMENT, ADVERTISEMENT_ENABLED};
|
use crate::{config::Config, term::print_console, ADVERTISEMENT, ADVERTISEMENT_ENABLED};
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ fn skip_null(stream: &mut TcpStream) -> Result<Vec<u8>, Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_messages(host: &str) -> Result<String, Box<dyn Error>> {
|
pub fn read_messages(host: &str, max_messages: usize) -> Result<(Vec<String>, usize), Box<dyn Error>> {
|
||||||
let mut stream = TcpStream::connect(host)?;
|
let mut stream = TcpStream::connect(host)?;
|
||||||
|
|
||||||
stream.write_all(&[0x00])?;
|
stream.write_all(&[0x00])?;
|
||||||
@ -69,23 +69,30 @@ pub fn read_messages(host: &str) -> Result<String, Box<dyn Error>> {
|
|||||||
String::from_utf8_lossy(&data).to_string()
|
String::from_utf8_lossy(&data).to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(packet_data)
|
let lines: Vec<&str> = packet_data.split("\n").collect();
|
||||||
|
let lines: Vec<String> = lines.clone().into_iter()
|
||||||
|
.skip(lines.len() - max_messages)
|
||||||
|
.map(|o| o.to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok((lines, packet_size))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recv_loop(config: Arc<Config>, host: &str, cache: Arc<RwLock<String>>, input: Arc<RwLock<String>>, disable_formatting: bool) -> Result<(), Box<dyn Error>> {
|
fn recv_loop(config: Arc<Config>, host: &str, cache: Arc<(RwLock<Vec<String>>, AtomicUsize)>, input: Arc<RwLock<String>>, disable_formatting: bool) -> Result<(), Box<dyn Error>> {
|
||||||
while let Ok(data) = read_messages(host) {
|
while let Ok(data) = read_messages(host, config.max_messages) {
|
||||||
if data == cache.read().unwrap().clone() {
|
if data.1 == cache.1.load(Ordering::SeqCst) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
*cache.write().unwrap() = data;
|
*cache.0.write().unwrap() = data.0.clone();
|
||||||
print_console(config.clone(), &cache.read().unwrap(), &input.read().unwrap(), disable_formatting)?;
|
cache.1.store(data.1, Ordering::SeqCst);
|
||||||
|
print_console(config.clone(), data.0, &input.read().unwrap(), disable_formatting)?;
|
||||||
thread::sleep(Duration::from_millis(config.update_time as u64));
|
thread::sleep(Duration::from_millis(config.update_time as u64));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_recv_loop(config: Arc<Config>, host: String, messages: Arc<RwLock<String>>, input: Arc<RwLock<String>>, disable_formatting: bool) {
|
pub fn run_recv_loop(config: Arc<Config>, host: String, messages: Arc<(RwLock<Vec<String>>, AtomicUsize)>, input: Arc<RwLock<String>>, disable_formatting: bool) {
|
||||||
thread::spawn({
|
thread::spawn({
|
||||||
move || {
|
move || {
|
||||||
let _ = recv_loop(config.clone(), &host, messages, input, disable_formatting);
|
let _ = recv_loop(config.clone(), &host, messages, input, disable_formatting);
|
||||||
|
45
src/term.rs
45
src/term.rs
@ -1,4 +1,4 @@
|
|||||||
use std::{error::Error, io::{stdout, Write}, sync::{Arc, RwLock}, time::Duration};
|
use std::{error::Error, io::{stdout, Write}, sync::{atomic::AtomicUsize, Arc, RwLock}, time::Duration};
|
||||||
|
|
||||||
use colored::{Color, Colorize};
|
use colored::{Color, Colorize};
|
||||||
use crossterm::{cursor::MoveLeft, event::{self, Event, KeyCode}, terminal::{disable_raw_mode, enable_raw_mode}, ExecutableCommand};
|
use crossterm::{cursor::MoveLeft, event::{self, Event, KeyCode}, terminal::{disable_raw_mode, enable_raw_mode}, ExecutableCommand};
|
||||||
@ -6,22 +6,15 @@ use regex::Regex;
|
|||||||
|
|
||||||
use crate::{config::Config, on_command, rac::send_message, ADVERTISEMENT, COLORED_USERNAMES, DATE_REGEX};
|
use crate::{config::Config, on_command, rac::send_message, ADVERTISEMENT, COLORED_USERNAMES, DATE_REGEX};
|
||||||
|
|
||||||
pub fn print_console(config: Arc<Config>, messages: &str, input: &str, disable_formatting: bool) -> Result<(), Box<dyn Error>> {
|
pub fn print_console(config: Arc<Config>, messages: Vec<String>, input: &str, disable_formatting: bool) -> Result<(), Box<dyn Error>> {
|
||||||
let mut messages = messages.split("\n")
|
|
||||||
.map(|o| o.to_string())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
messages.reverse();
|
|
||||||
messages.truncate(config.max_messages);
|
|
||||||
messages.reverse();
|
|
||||||
let messages: Vec<String> = if disable_formatting {
|
|
||||||
messages
|
|
||||||
} else {
|
|
||||||
messages.into_iter().filter_map(format_message).collect()
|
|
||||||
};
|
|
||||||
let text = format!(
|
let text = format!(
|
||||||
"{}{}\n> {}",
|
"{}{}\n> {}",
|
||||||
"\n".repeat(config.max_messages - messages.len()),
|
"\n".repeat(config.max_messages - messages.len()),
|
||||||
messages.join("\n"),
|
if disable_formatting {
|
||||||
|
messages
|
||||||
|
} else {
|
||||||
|
messages.into_iter().filter_map(format_message).collect()
|
||||||
|
}.join("\n"),
|
||||||
// if sound { "\x07" } else { "" },
|
// if sound { "\x07" } else { "" },
|
||||||
input
|
input
|
||||||
);
|
);
|
||||||
@ -84,7 +77,16 @@ fn find_username_color(message: &str) -> Option<(String, String, Color)> {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_events(config: Arc<Config>, input: Arc<RwLock<String>>, messages: Arc<RwLock<String>>, host: String, name: String, disable_formatting: bool, disable_commands: bool, disable_hiding_ip: bool) {
|
fn poll_events(
|
||||||
|
config: Arc<Config>,
|
||||||
|
input: Arc<RwLock<String>>,
|
||||||
|
messages: Arc<(RwLock<Vec<String>>, AtomicUsize)>,
|
||||||
|
host: String,
|
||||||
|
name: String,
|
||||||
|
disable_formatting: bool,
|
||||||
|
disable_commands: bool,
|
||||||
|
disable_hiding_ip: bool
|
||||||
|
) {
|
||||||
loop {
|
loop {
|
||||||
if !event::poll(Duration::from_millis(50)).unwrap_or(false) { continue }
|
if !event::poll(Duration::from_millis(50)).unwrap_or(false) { continue }
|
||||||
|
|
||||||
@ -119,7 +121,7 @@ fn poll_events(config: Arc<Config>, input: Arc<RwLock<String>>, messages: Arc<Rw
|
|||||||
} else {
|
} else {
|
||||||
print_console(
|
print_console(
|
||||||
config.clone(),
|
config.clone(),
|
||||||
&messages.read().unwrap(),
|
messages.0.read().unwrap().clone(),
|
||||||
&input.read().unwrap(),
|
&input.read().unwrap(),
|
||||||
disable_formatting
|
disable_formatting
|
||||||
).expect("Error printing console");
|
).expect("Error printing console");
|
||||||
@ -154,7 +156,16 @@ fn poll_events(config: Arc<Config>, input: Arc<RwLock<String>>, messages: Arc<Rw
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_main_loop(config: Arc<Config>, messages: Arc<RwLock<String>>, input: Arc<RwLock<String>>, host: String, name: String, disable_formatting: bool, disable_commands: bool, disable_hiding_ip: bool) {
|
pub fn run_main_loop(
|
||||||
|
config: Arc<Config>,
|
||||||
|
messages: Arc<(RwLock<Vec<String>>, AtomicUsize)>,
|
||||||
|
input: Arc<RwLock<String>>,
|
||||||
|
host: String,
|
||||||
|
name: String,
|
||||||
|
disable_formatting: bool,
|
||||||
|
disable_commands: bool,
|
||||||
|
disable_hiding_ip: bool
|
||||||
|
) {
|
||||||
enable_raw_mode().unwrap();
|
enable_raw_mode().unwrap();
|
||||||
|
|
||||||
// thread::spawn({
|
// thread::spawn({
|
||||||
|
Loading…
Reference in New Issue
Block a user