fix 1.99.2

This commit is contained in:
MeexReay 2025-02-09 12:05:36 +03:00
parent 3ba2b30008
commit a8f49c7fb1
5 changed files with 83 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
main
main.exe
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "bRAC"
version = "1.99.2"

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "bRAC"
version = "1.99.2"
edition = "2021"
[dependencies]
[[bin]]
name = "bRAC"
path = "main.rs"

View File

@ -6,4 +6,6 @@ better RAC client
```bash
rustc main.rs -o main # build
./main # run
cargo build # build with cargo
cargo run # run with cargo
```

92
main.rs
View File

@ -1,4 +1,4 @@
use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread};
use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread, usize::MAX};
const MAX_MESSAGES: usize = 100;
const DEFAULT_HOST: &str = "meex.lol:11234";
@ -7,35 +7,71 @@ fn send_message(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
let mut stream = TcpStream::connect(host)?;
stream.write_all(&[0x01])?;
stream.write_all(message.as_bytes())?;
stream.write_all("\0".repeat(1023 - message.len()).as_bytes())?;
Ok(())
}
fn read_messages(host: &str) -> Result<Vec<String>, Box<dyn Error>> {
fn read_messages(host: &str, skip: usize) -> Result<String, Box<dyn Error>> {
let mut stream = TcpStream::connect(host)?;
stream.write_all(&[0x00])?;
let packet_size = {
let mut buf= vec![0; 10];
stream.read(&mut buf)?;
String::from_utf8(buf)?.trim_matches(char::from(0)).parse()?
};
stream.write_all(&[0x01])?;
let packet_data = {
let mut buf = vec![0; packet_size];
let mut data = Vec::new();
loop {
let mut buf = vec![0; 1];
stream.read_exact(&mut buf)?;
let buf_str = String::from_utf8_lossy(&buf).to_string();
let start_null = buf_str.len() - buf_str.trim_start_matches(char::from(0)).len();
let mut buf = vec![0; start_null];
stream.read_exact(&mut buf)?;
format!("{}{}", &buf_str, String::from_utf8_lossy(&buf).to_string())
};
let mut lines: Vec<String> = packet_data.split("\n").map(|o| o.to_string()).collect();
lines.reverse();
lines.truncate(MAX_MESSAGES);
lines.reverse();
Ok(lines)
let ch = buf[0];
if ch == 0 {
break
}
data.push(ch);
}
fn print_console(messages: Vec<String>) -> Result<(), Box<dyn Error>> {
String::from_utf8(data)?
.trim_matches(char::from(0))
.parse()?
};
// println!("{} {}", skip, packet_size);
if packet_size <= skip {
return Ok(String::new())
}
let to_read = if skip == 0 {
stream.write_all(&[0x01])?;
packet_size
} else {
stream.write_all(&[0x02])?;
stream.write_all(skip.to_string().as_bytes())?;
packet_size - skip
};
let packet_data = {
let mut data = vec![0; to_read];
stream.read_exact(&mut data)?;
data.retain(|x| *x != 0);
while data.len() != to_read {
let mut buf = vec![0; to_read - data.len()];
stream.read_exact(&mut buf)?;
data.append(&mut buf);
data.retain(|x| *x != 0);
}
String::from_utf8_lossy(&data).to_string()
};
// println!("{}", packet_data);
Ok(packet_data)
}
fn print_console(messages: Vec<&str>) -> Result<(), Box<dyn Error>> {
let mut messages = messages.clone();
messages.reverse();
messages.truncate(MAX_MESSAGES);
messages.reverse();
let mut out = stdout().lock();
let text = format!("{}{}\n> ", "\n".repeat(MAX_MESSAGES - messages.len()), messages.join("\n"));
out.write_all(text.as_bytes())?;
@ -44,11 +80,11 @@ fn print_console(messages: Vec<String>) -> Result<(), Box<dyn Error>> {
}
fn recv_loop(host: &str) -> Result<(), Box<dyn Error>> {
let mut cache = Vec::new();
while let Ok(messages) = read_messages(host) {
if cache == messages { continue }
print_console(messages.clone())?;
cache = messages;
let mut cache = String::new();
while let Ok(messages) = read_messages(host, cache.len()) {
if messages.len() == 0 { continue }
cache.push_str(&messages);
print_console(cache.split("\n").collect())?;
}
Ok(())
}
@ -76,7 +112,7 @@ fn get_input(prompt: &str, default: &str) -> String {
fn main() {
let host = get_input("Host (default: meex.lol:11234) > ", DEFAULT_HOST);
let prefix = get_input("Prefix (default: none) > ", "");
let name = get_input("Name (default: Anon) > ", "Anon");
thread::spawn({
let host = host.clone();
@ -89,6 +125,6 @@ fn main() {
let mut lines = stdin().lock().lines();
while let Some(Ok(message)) = lines.next() {
send_message(&host, &format!("{}{}", &prefix, &message)).expect("Error sending message");
send_message(&host, &format!("<{}> {}", &name, &message)).expect("Error sending message");
}
}