mirror of
https://github.com/MeexReay/bRAC.git
synced 2025-05-06 13:38:04 +03:00
connect to server on click in server list
This commit is contained in:
parent
d8170d10e5
commit
30133c1198
@ -1,19 +1,30 @@
|
|||||||
use std::sync::{Arc, mpsc::{channel, Receiver}};
|
use std::{sync::{mpsc::{channel, Receiver}, Arc}, time::UNIX_EPOCH};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
|
||||||
use gtk4::{
|
use gtk4 as gtk;
|
||||||
self as gtk, gdk::{Cursor, Display, Texture}, gdk_pixbuf::{Pixbuf, PixbufAnimation, PixbufLoader}, gio::{
|
|
||||||
self, ActionEntry, ApplicationFlags,
|
use gtk::prelude::*;
|
||||||
MemoryInputStream, Menu
|
use gtk::gdk::{Cursor, Display, Texture};
|
||||||
}, glib::{
|
use gtk::gdk_pixbuf::{Pixbuf, PixbufAnimation, PixbufLoader};
|
||||||
self, clone, clone::Downgrade, idle_add_local, idle_add_local_once, source::timeout_add_local_once, timeout_add_local, ControlFlow
|
use gtk::gio::{self, ActionEntry, ApplicationFlags, MemoryInputStream, Menu};
|
||||||
}, pango::WrapMode, prelude::*, AboutDialog, Align, Application, ApplicationWindow, Box as GtkBox,
|
use gtk::glib::clone;
|
||||||
Button, Calendar, CheckButton, CssProvider, Entry, Fixed, Justification, Label, ListBox, Orientation,
|
use gtk::glib::{
|
||||||
Overlay, Picture, ScrolledWindow, Settings, Window
|
self, clone::Downgrade,
|
||||||
|
idle_add_local,
|
||||||
|
idle_add_local_once,
|
||||||
|
timeout_add_local,
|
||||||
|
source::timeout_add_local_once,
|
||||||
|
ControlFlow
|
||||||
|
};
|
||||||
|
use gtk::pango::WrapMode;
|
||||||
|
use gtk::{
|
||||||
|
AboutDialog, Align, Application, ApplicationWindow, Box as GtkBox,
|
||||||
|
Button, Calendar, CheckButton, CssProvider, Entry, Fixed, GestureClick,
|
||||||
|
Justification, Label, ListBox, Orientation, Overlay, Picture, ScrolledWindow, Settings, Window
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{config::{default_max_messages, default_update_time, get_config_path, save_config, Config},
|
use super::{config::{default_max_messages, default_update_time, get_config_path, save_config, Config},
|
||||||
@ -434,9 +445,30 @@ fn build_ui(ctx: Arc<Context>, app: &Application) -> UiModel {
|
|||||||
|
|
||||||
let server_list = ListBox::new();
|
let server_list = ListBox::new();
|
||||||
|
|
||||||
server_list.append(&Label::builder().label("meex.lol:42666").halign(Align::Start).selectable(true).build());
|
for url in ["meex.lol:42666", "meex.lol:11234", "91.192.22.20:42666"] {
|
||||||
server_list.append(&Label::builder().label("meex.lol:11234").halign(Align::Start).selectable(true).build());
|
let url = url.to_string();
|
||||||
server_list.append(&Label::builder().label("91.192.22.20:42666").halign(Align::Start).selectable(true).build());
|
|
||||||
|
let label = Label::builder()
|
||||||
|
.label(&url)
|
||||||
|
.halign(Align::Start)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let click = GestureClick::new();
|
||||||
|
|
||||||
|
click.connect_pressed(clone!(
|
||||||
|
#[weak] ctx,
|
||||||
|
move |_, _, _, _| {
|
||||||
|
let mut config = ctx.config.read().unwrap().clone();
|
||||||
|
config.host = url.clone();
|
||||||
|
ctx.set_config(&config);
|
||||||
|
save_config(get_config_path(), &config);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
|
label.add_controller(click);
|
||||||
|
|
||||||
|
server_list.append(&label);
|
||||||
|
}
|
||||||
|
|
||||||
server_list_vbox.append(&Label::builder().label("Server List:").build());
|
server_list_vbox.append(&Label::builder().label("Server List:").build());
|
||||||
|
|
||||||
@ -626,17 +658,6 @@ fn build_ui(ctx: Arc<Context>, app: &Application) -> UiModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_recv_loop(ctx: Arc<Context>) {
|
|
||||||
thread::spawn(move || {
|
|
||||||
loop {
|
|
||||||
if let Err(e) = recv_tick(ctx.clone()) {
|
|
||||||
let _ = print_message(ctx.clone(), format!("Print messages error: {}", e.to_string()).to_string());
|
|
||||||
thread::sleep(Duration::from_secs(1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup(ctx: Arc<Context>, ui: UiModel) {
|
fn setup(ctx: Arc<Context>, ui: UiModel) {
|
||||||
let (sender, receiver) = channel();
|
let (sender, receiver) = channel();
|
||||||
|
|
||||||
@ -778,6 +799,17 @@ fn on_add_message(ctx: Arc<Context>, ui: &UiModel, message: String) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_recv_loop(ctx: Arc<Context>) {
|
||||||
|
thread::spawn(move || {
|
||||||
|
loop {
|
||||||
|
if let Err(e) = recv_tick(ctx.clone()) {
|
||||||
|
let _ = print_message(ctx.clone(), format!("Print messages error: {}", e.to_string()).to_string());
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run_main_loop(ctx: Arc<Context>) {
|
pub fn run_main_loop(ctx: Arc<Context>) {
|
||||||
let application = Application::builder()
|
let application = Application::builder()
|
||||||
.application_id("ru.themixray.bRAC")
|
.application_id("ru.themixray.bRAC")
|
||||||
|
10
src/proto.rs
10
src/proto.rs
@ -1,6 +1,6 @@
|
|||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::{error::Error, fmt::Debug, io::{Read, Write}, net::TcpStream};
|
use std::{error::Error, fmt::Debug, io::{Read, Write}, net::{SocketAddr, TcpStream, ToSocketAddrs}, str::FromStr, time::Duration};
|
||||||
use native_tls::TlsConnector;
|
use native_tls::TlsConnector;
|
||||||
|
|
||||||
pub trait RacStream: Read + Write + Unpin + Send + Sync + Debug {}
|
pub trait RacStream: Read + Write + Unpin + Send + Sync + Debug {}
|
||||||
@ -29,7 +29,13 @@ pub fn connect(host: &str, ssl: bool) -> Result<Box<dyn RacStream>, Box<dyn Erro
|
|||||||
.connect(&ip, connect(&host, false)?)?))
|
.connect(&ip, connect(&host, false)?)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Box::new(TcpStream::connect(host)?))
|
let addr = host.to_socket_addrs()?.next().ok_or::<Box<dyn Error>>("addr parse error".into())?;
|
||||||
|
let stream = TcpStream::connect_timeout(&addr, Duration::from_secs(3))?;
|
||||||
|
|
||||||
|
stream.set_read_timeout(Some(Duration::from_secs(5)));
|
||||||
|
stream.set_write_timeout(Some(Duration::from_secs(5)));
|
||||||
|
|
||||||
|
Ok(Box::new(stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send message
|
/// Send message
|
||||||
|
Loading…
Reference in New Issue
Block a user