Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Improved buffer thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Oct 3, 2023
1 parent 6d873dc commit 9358b2c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
13 changes: 8 additions & 5 deletions src/com/telnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
io::{self, ErrorKind, Read, Write},
net::TcpStream,
};
use web_time::Duration;
use web_time::{Duration, Instant};

#[derive(Debug)]
pub struct ComTelnetImpl {
Expand Down Expand Up @@ -307,9 +307,9 @@ mod telnet_option {
impl ComTelnetImpl {
pub fn connect(connection_data: &super::OpenConnectionData) -> TermComResult<Self> {
let tcp_stream = TcpStream::connect(&connection_data.address)?;
tcp_stream.set_write_timeout(Some(Duration::from_millis(2000)))?;
tcp_stream.set_read_timeout(Some(Duration::from_millis(2000)))?;
tcp_stream.set_nonblocking(true)?;
tcp_stream.set_write_timeout(Some(Duration::from_millis(50)))?;
tcp_stream.set_read_timeout(Some(Duration::from_millis(50)))?;
tcp_stream.set_nonblocking(false)?;
Ok(Self {
tcp_stream,
state: ParserState::Data,
Expand Down Expand Up @@ -484,9 +484,12 @@ impl Com for ComTelnetImpl {

fn read_data(&mut self) -> TermComResult<Option<Vec<u8>>> {
let mut buf = [0; 1024 * 256];

let m = Instant::now();
match self.tcp_stream.read(&mut buf) {
Ok(size) => {
if size == 0 {
return Ok(None);
}
let data = self.parse(&buf[0..size])?;
/*
for ch in &data {
Expand Down
8 changes: 7 additions & 1 deletion src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,26 @@ impl MainWindow {
}
let buffer_update_view = Arc::new(eframe::epaint::mutex::Mutex::new(view));


let connection = Arc::new(Mutex::new(Some(Box::new(connection))));


let buffer_update_thread = Arc::new(Mutex::new(BufferUpdateThread {
connection: Arc::new(Mutex::new(Some(Box::new(connection)))),
connection: connection.clone(),
buffer_view: buffer_update_view.clone(),
capture_dialog: dialogs::capture_dialog::DialogState::default(),
last_update: Instant::now(),
auto_file_transfer: AutoFileTransfer::default(),
auto_transfer: None,
auto_login: None,
sound_thread: Arc::new(eframe::epaint::mutex::Mutex::new(SoundThread::new())),
enabled: true,
}));

crate::ui::buffer_update_thread::run_update_thread(&cc.egui_ctx, buffer_update_thread.clone());

let mut view = MainWindow {
connection,
buffer_view: buffer_update_view.clone(),
//address_list: HoverList::new(),
state: MainWindowState { options, ..Default::default() },
Expand Down
15 changes: 9 additions & 6 deletions src/ui/buffer_update_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct BufferUpdateThread {
pub sound_thread: Arc<Mutex<SoundThread>>,

pub auto_transfer: Option<(TransferType, bool)>,
pub enabled: bool,
}

impl BufferUpdateThread {
Expand All @@ -38,15 +39,15 @@ impl BufferUpdateThread {
let data = if let Some(con) = self.connection.lock().as_mut() {
con.update_state()?;
if con.is_disconnected() {
return Ok(false);
return Ok(true);
}
if con.is_data_available()? {
con.read_buffer()
} else {
Vec::new()
}
} else {
return Ok(false);
return Ok(true);
};
Ok(self.update_buffer(ctx, data))
}
Expand All @@ -56,10 +57,12 @@ impl BufferUpdateThread {
let has_data = !data.is_empty();
let mut set_buffer_dirty = false;
if !data.is_empty() {
println!("data : {}", self.last_update.elapsed().as_millis());
// println!("data : {} {}", self.last_update.elapsed().as_millis(), data.len());
}
let buffer_view = &mut self.buffer_view.lock();

if !self.enabled {
return true;
}
for ch in data {
if let Some(autologin) = &mut self.auto_login {
if let Some(con) = self.connection.lock().as_mut() {
Expand Down Expand Up @@ -102,7 +105,7 @@ impl BufferUpdateThread {
ctx.request_repaint();
return false;
}
true
!has_data
}

pub fn print_char(&self, buffer_view: &mut BufferView, c: u8) -> bool {
Expand Down Expand Up @@ -149,7 +152,7 @@ impl BufferUpdateThread {
Ok(icy_engine::CallbackAction::Update) => {
return true;
}

Err(err) => {
log::error!("{err}");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/dialogs/upload_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl MainWindow {
if matches!(self.upload_dialog.protocol_type, crate::protocol::TransferType::Text) {
match std::fs::read(path) {
Ok(bytes) => {
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let r = con.send(bytes);
check_error!(self, r, true);
}
Expand Down
28 changes: 15 additions & 13 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod util;
pub use util::*;

use self::buffer_update_thread::BufferUpdateThread;
use self::connect::Connection;
use self::file_transfer_thread::FileTransferThread;
pub mod dialogs;

Expand Down Expand Up @@ -95,6 +96,7 @@ impl MainWindowState {

pub struct MainWindow {
buffer_view: Arc<eframe::epaint::mutex::Mutex<BufferView>>,
pub connection: Arc<eframe::epaint::mutex::Mutex<Option<Box<Connection>>>>,

pub state: MainWindowState,

Expand Down Expand Up @@ -146,23 +148,23 @@ impl MainWindow {
pub fn output_char(&mut self, ch: char) {
let translated_char = self.buffer_view.lock().get_parser().convert_from_unicode(ch, 0);
let mut print = true;
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
if con.is_connected() {
let r = con.send(vec![translated_char as u8]);
check_error!(self, r, false);
print = false;
}
}

if !print {
if print {
self.print_char(translated_char as u8);
}
}

pub fn output_string(&self, str: &str) {
let mut print = true;

if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
if con.is_connected() {
let mut v = Vec::new();
for ch in str.chars() {
Expand Down Expand Up @@ -200,15 +202,15 @@ impl MainWindow {

let r = crate::protocol::DiskStorageHandler::new();
check_error!(self, r, false);
if let Some(mut con) = self.buffer_update_thread.lock().connection.lock().take() {
if let Some(mut con) = self.connection.lock().take() {
con.start_transfer();
self.current_file_transfer = Some(FileTransferThread::new(con, protocol_type, download, files_opt));
}
}

pub(crate) fn initiate_file_transfer(&mut self, protocol_type: crate::protocol::TransferType, download: bool) {
self.set_mode(MainWindowMode::ShowTerminal);
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
if con.is_disconnected() {
return;
}
Expand Down Expand Up @@ -283,7 +285,7 @@ impl MainWindow {

let timeout = self.get_options().connect_timeout;
let window_size = self.screen_mode.get_window_size();
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let r = con.connect(&cloned_addr, timeout, window_size);
check_error!(self, r, false);
let r = con.set_baud_rate(cloned_addr.baud_emulation.get_baud_rate());
Expand All @@ -294,7 +296,7 @@ impl MainWindow {
pub fn update_state(&mut self) -> TerminalResult<()> {
#[cfg(target_arch = "wasm32")]
self.poll_thread.poll();
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let r = con.update_state();
check_error!(self, r, false);
}
Expand All @@ -307,15 +309,15 @@ impl MainWindow {
}

pub fn hangup(&mut self) {
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
check_error!(self, con.disconnect(), false);
}
self.buffer_update_thread.lock().sound_thread.lock().clear();
self.set_mode(MainWindowMode::ShowDialingDirectory);
}

pub fn send_login(&mut self) {
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
if con.is_disconnected() {
return;
}
Expand Down Expand Up @@ -345,12 +347,12 @@ impl MainWindow {
}

self.output_string(&user_name);
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let r = con.send(cr.clone());
check_error!(self, r, false);
}
self.output_string(&password);
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let r = con.send(cr);
check_error!(self, r, false);
}
Expand All @@ -361,10 +363,10 @@ impl MainWindow {
if let MainWindowMode::ShowDialingDirectory = self.get_mode() {
frame.set_window_title(&crate::DEFAULT_TITLE);
} else {
if self.buffer_update_thread.lock().connection.lock().is_none() {
if self.connection.lock().is_none() {
return;
}
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
let title = if con.is_connected() {
let d = Instant::now().duration_since(con.get_connection_time());
let sec = d.as_secs();
Expand Down
8 changes: 6 additions & 2 deletions src/ui/terminal_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ impl MainWindow {

monitor_settings.selection_fg = self.screen_mode.get_selection_fg();
monitor_settings.selection_bg = self.screen_mode.get_selection_bg();

/* if ui.input(|i| i.key_down(egui::Key::W)) {
let enabled = self.buffer_update_thread.lock().enabled;
self.buffer_update_thread.lock().enabled = !enabled;
}*/

let opt = icy_engine_egui::TerminalOptions {
filter: self.get_options().scaling.get_filter(),
monitor_settings,
Expand Down Expand Up @@ -388,7 +392,7 @@ impl MainWindow {
for (k, m) in key_map {
if *k == key_code {
let mut print = true;
if let Some(con) = self.buffer_update_thread.lock().connection.lock().as_mut() {
if let Some(con) = self.connection.lock().as_mut() {
if con.is_connected() {
let res = con.send(m.to_vec());
check_error!(self, res, true);
Expand Down

0 comments on commit 9358b2c

Please sign in to comment.