fixed bug of useing 100% cpu
This commit is contained in:
88
src/main.rs
88
src/main.rs
@@ -10,6 +10,7 @@ use serenity::framework::standard::macros::{command, group, help, hook};
|
||||
use serenity::framework::standard::{StandardFramework, CommandResult, Args, HelpOptions, CommandGroup, help_commands};
|
||||
use serenity::utils::Color;
|
||||
use std::collections::HashSet;
|
||||
use std::process::exit;
|
||||
|
||||
#[group]
|
||||
#[commands(ping, pong, console, git, discord, auth)]
|
||||
@@ -47,11 +48,9 @@ async fn normal_message(_ctx: &Context, msg: &Message) {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
|
||||
let conf = init_conf().await;
|
||||
|
||||
let sock = TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()).unwrap();
|
||||
let sock = TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()).expect("tcp connection failed");
|
||||
|
||||
let framework = StandardFramework::new()
|
||||
.configure(|c| c
|
||||
@@ -139,17 +138,52 @@ async fn console(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
match cons_rw(sock, args.message()) {
|
||||
Ok(n) => {
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Console")
|
||||
.description(n)
|
||||
.color(Color::ROSEWATER)
|
||||
)
|
||||
}).await?;
|
||||
}
|
||||
Err(_e) => {
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Error")
|
||||
.description("Unable to connect to the mindustry server\nCheck if server has restarted\nBot shutting down")
|
||||
.color(Color::RED)
|
||||
)
|
||||
}).await?;
|
||||
exit(1);
|
||||
|
||||
// let mut w_data = ctx.data.write().await;
|
||||
|
||||
// match TcpSock::new(conf.discord_settings.ip.clone(), conf.discord_settings.port.clone()) {
|
||||
// Ok(n) => {
|
||||
// w_data.insert::<TcpSock>(n);
|
||||
// println!("reconnected");
|
||||
// }
|
||||
// Err(_e) => {
|
||||
// msg.channel_id.send_message(ctx, |m| {
|
||||
// m.content("")
|
||||
// .embed(|e| e
|
||||
// .title("Error")
|
||||
// .description("Reconnection unsuccessful\nStopping bot")
|
||||
// .color(Color::RED)
|
||||
// )
|
||||
// }).await?;
|
||||
// println!("unable to reconnect");
|
||||
// exit(1);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Console")
|
||||
.description(cons_rw(sock, args.message()))
|
||||
.color(Color::ROSEWATER)
|
||||
)
|
||||
}).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -164,14 +198,30 @@ async fn auth(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
|
||||
|
||||
let sock = data.get::<TcpSock>().unwrap();
|
||||
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Console")
|
||||
.description(cons_rw(sock, &format!("auth add {}", args.message())))
|
||||
.color(Color::ROSEWATER)
|
||||
)
|
||||
}).await?;
|
||||
match cons_rw(sock, &format!("auth add {}", args.message())) {
|
||||
Ok(n) => {
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Console")
|
||||
.description(n)
|
||||
.color(Color::ROSEWATER)
|
||||
)
|
||||
}).await?;
|
||||
}
|
||||
Err(_e) => {
|
||||
msg.channel_id.send_message(ctx, |m| {
|
||||
m.content("")
|
||||
.embed(|e| e
|
||||
.title("Error")
|
||||
.description("Unable to connect to the mindustry server\nCheck if server has restarted\nBot shutting down")
|
||||
.color(Color::RED)
|
||||
)
|
||||
}).await?;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -10,26 +10,60 @@ use indoc::indoc;
|
||||
|
||||
|
||||
|
||||
pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
|
||||
pub fn cons_rw(sock: &TcpSock, input: &str) -> std::io::Result<String> {
|
||||
|
||||
match sock.stream.peer_addr() {
|
||||
Ok(peer_addr) => {
|
||||
println!("connection to {} is availible", peer_addr);
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Error: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
let mut output = String::new();
|
||||
|
||||
println!("creating writer");
|
||||
let mut writer = std::io::BufWriter::new(sock.stream.try_clone().unwrap());
|
||||
|
||||
println!("creating reader");
|
||||
let mut reader = std::io::BufReader::new(sock.stream.try_clone().unwrap());
|
||||
|
||||
loop {
|
||||
match reader.read_line(&mut output) {
|
||||
Ok(t) => t,
|
||||
Err(_) => break(),
|
||||
};
|
||||
let mut buf = [0; 1024];
|
||||
match sock.stream.peek(&mut buf) {
|
||||
Ok(n) if n > 0 => {
|
||||
println!("data to be cleared from queue \nclearing");
|
||||
loop {
|
||||
match reader.read_line(&mut output) {
|
||||
Ok(t) => t,
|
||||
Err(_) => break(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Ok(_) => {
|
||||
println!("connection to socket lost\nis the server stopped or restarted?");
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::Other, "unable to connect to server"))
|
||||
}
|
||||
Err(e) => {
|
||||
println!("error peeking {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
println!("clearing output variable");
|
||||
output.clear();
|
||||
|
||||
writer.write((input.to_owned() + "\n").as_bytes()).unwrap();
|
||||
println!("writing");
|
||||
writer.write((input.to_owned() + "\n").as_bytes()).expect("could not write to cons");
|
||||
|
||||
println!("flushing writer");
|
||||
writer.flush().expect("flush failed");
|
||||
|
||||
let mut line = 0;
|
||||
|
||||
loop {
|
||||
line += 1;
|
||||
println!("reading line number {}", line);
|
||||
match reader.read_line(&mut output) {
|
||||
Ok(t) => t,
|
||||
Err(_) => break(),
|
||||
@@ -41,7 +75,7 @@ pub fn cons_rw(sock: &TcpSock, input: &str) -> String {
|
||||
|
||||
|
||||
|
||||
output
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::time::Duration;
|
||||
use std::{time::Duration};
|
||||
use std::net::TcpStream;
|
||||
use serenity::prelude::{TypeMapKey};
|
||||
use std::{str};
|
||||
@@ -8,8 +8,12 @@ pub struct TcpSock {
|
||||
}
|
||||
|
||||
impl TcpSock {
|
||||
pub fn new(ip: String, port: String) -> std::io::Result<Self> {
|
||||
let stream = TcpStream::connect(format!("{}:{}", ip, port)).expect("Tcp connection fail");
|
||||
pub fn new(ip: String, port: String) -> Result<Self, std::io::Error> {
|
||||
let stream = match TcpStream::connect(format!("{}:{}", ip, port)) {
|
||||
Ok(stream) => stream,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
stream.set_read_timeout(Some(Duration::from_millis(100)))?;
|
||||
println!("Socket Connected!!");
|
||||
Ok(TcpSock { stream })
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user