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

Commit

Permalink
Added own buffer update thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Oct 3, 2023
1 parent 7e932e1 commit 6c76cdc
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 228 deletions.
1 change: 1 addition & 0 deletions src/com/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Com for ComRawImpl {
Ok(()) => Ok(buf.len()),
Err(ref e) => {
if e.kind() == io::ErrorKind::WouldBlock {
println!("sleep1");
std::thread::sleep(Duration::from_millis(100));
return self.send(buf);
}
Expand Down
12 changes: 4 additions & 8 deletions src/com/telnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ 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_nonblocking(true)?;
tcp_stream.set_write_timeout(Some(Duration::from_millis(2000)))?;
tcp_stream.set_read_timeout(Some(Duration::from_millis(2000)))?;
Ok(Self {
Expand Down Expand Up @@ -482,23 +483,16 @@ impl Com for ComTelnetImpl {
}

fn read_data(&mut self) -> TermComResult<Option<Vec<u8>>> {
let mut buf = [0; 1024 * 256];
if self.tcp_stream.peek(&mut buf)? == 0 {
return Ok(None);
}

let mut buf = [0; 1024 * 8];
match self.tcp_stream.read(&mut buf) {
Ok(size) => {
self.tcp_stream.set_nonblocking(true)?;

if self.use_raw_transfer {
Ok(Some(buf[0..size].to_vec()))
} else {
self.parse(&buf[0..size])
}
}
Err(ref e) => {
self.tcp_stream.set_nonblocking(true)?;
if e.kind() == io::ErrorKind::WouldBlock {
return Ok(None);
}
Expand Down Expand Up @@ -526,6 +520,8 @@ impl Com for ComTelnetImpl {
Ok(()) => Ok(buf.len()),
Err(ref e) => {
if e.kind() == io::ErrorKind::WouldBlock {
println!("sleep2");

std::thread::sleep(Duration::from_millis(100));
return self.send(buf);
}
Expand Down
21 changes: 16 additions & 5 deletions src/features/auto_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use web_time::Instant;
use crate::{
ui::connect::{Connection, DataConnection},
util::PatternRecognizer,
Address, Options, TerminalResult,
Address, TerminalResult,
};
use std::time::Duration;

Expand All @@ -22,10 +22,19 @@ pub struct AutoLogin {
got_name: bool,
name_recognizer: PatternRecognizer,
login_recognizer: PatternRecognizer,

user_name: String,
password: String,
}

impl AutoLogin {
pub fn new(login_expr: &str) -> Self {
pub fn new(login_expr: &str, adr: &Address) -> Self {
let (user_name, password) = if adr.override_iemsi_settings {
(adr.iemsi_user.clone(), adr.iemsi_password.clone())
} else {
(adr.user_name.clone(), adr.password.clone())
};

Self {
logged_in: false,
disabled: false,
Expand All @@ -38,6 +47,8 @@ impl AutoLogin {
got_name: false,
name_recognizer: PatternRecognizer::from(b"NAME", true),
login_recognizer: PatternRecognizer::from(b"LOGIN:", true),
user_name,
password,
}
}

Expand Down Expand Up @@ -107,11 +118,11 @@ impl AutoLogin {
Ok(true)
}

pub fn try_login(&mut self, connection: &mut Connection, adr: &Address, ch: u8, options: &Options) -> TerminalResult<()> {
pub fn try_login(&mut self, connection: &mut Connection, ch: u8) -> TerminalResult<()> {
if self.logged_in || self.disabled {
return Ok(());
}
if adr.user_name.is_empty() || adr.password.is_empty() {
if self.user_name.is_empty() || self.password.is_empty() {
self.logged_in = true;
return Ok(());
}
Expand All @@ -123,7 +134,7 @@ impl AutoLogin {
self.last_char_recv = Instant::now();
self.got_name |= self.name_recognizer.push_ch(ch) | self.login_recognizer.push_ch(ch);

self.logged_in |= self.iemsi.try_login(connection, adr, ch, options)?;
self.logged_in |= self.iemsi.try_login(connection, &self.user_name, &self.password, ch)?;
Ok(())
}

Expand Down
39 changes: 18 additions & 21 deletions src/features/iemsi_com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use std::fmt;
use icy_engine::{get_crc16, get_crc32, update_crc32};

use crate::{
addresses::Address,
ui::connect::{Connection, DataConnection},
Options, TerminalResult, VERSION,
IEMSISettings, TerminalResult, VERSION,
};

/// EMSI Inquiry is transmitted by the calling system to identify it as
Expand Down Expand Up @@ -399,6 +398,7 @@ pub struct IEmsi {
pub got_invalid_isi: bool,
isi_data: Vec<u8>,

pub settings: IEMSISettings,
pub aborted: bool,
logged_in: bool,
}
Expand Down Expand Up @@ -513,27 +513,27 @@ impl IEmsi {
Ok(false)
}

pub fn try_login(&mut self, con: &mut Connection, adr: &Address, ch: u8, options: &Options) -> TerminalResult<bool> {
pub fn try_login(&mut self, con: &mut Connection, user_name: &str, password: &str, ch: u8) -> TerminalResult<bool> {
if self.aborted {
return Ok(false);
}
if let Some(data) = self.advance_char(adr, ch, options)? {
if let Some(data) = self.advance_char(user_name, password, ch)? {
if con.is_connected() {
con.send(data)?;
}
}
Ok(self.logged_in)
}

pub fn advance_char(&mut self, adr: &Address, ch: u8, options: &Options) -> TerminalResult<Option<Vec<u8>>> {
pub fn advance_char(&mut self, user_name: &str, password: &str, ch: u8) -> TerminalResult<Option<Vec<u8>>> {
if self.aborted {
return Ok(None);
}
self.parse_char(ch)?;
if self.irq_requested {
self.irq_requested = false;
// self.log_file.push("Starting IEMSI negotiation…".to_string());
let data = create_iemsi_ici(adr, options);
let data = create_iemsi_ici(user_name, password, &self.settings);
return Ok(Some(data.encode()?));
} else if let Some(_isi) = &self.isi {
// self.log_file.push("Receiving valid IEMSI server info…".to_string());
Expand All @@ -551,7 +551,7 @@ impl IEmsi {
self.nak_requested = false;
if self.retries < 2 {
// self.log_file.push("IEMSI retry…".to_string());
let data = create_iemsi_ici(adr, options);
let data = create_iemsi_ici(user_name, password, &self.settings);
self.retries += 1;
return Ok(Some(data.encode()?));
}
Expand All @@ -572,20 +572,15 @@ impl IEmsi {
}
}

fn create_iemsi_ici(adr: &Address, options: &Options) -> EmsiICI {
fn create_iemsi_ici(user_name: &str, password: &str, settings: &IEMSISettings) -> EmsiICI {
let mut data = EmsiICI::new();
if adr.override_iemsi_settings {
data.name = adr.iemsi_user.clone();
data.password = adr.iemsi_password.clone();
} else {
data.name = adr.user_name.clone();
data.password = adr.password.clone();
}
data.location = options.iemsi.location.clone();
data.alias = options.iemsi.alias.clone();
data.data_telephone = options.iemsi.data_phone.clone();
data.voice_telephone = options.iemsi.voice_phone.clone();
data.birthdate = options.iemsi.birth_date.clone();
data.name = user_name.to_string();
data.password = password.to_string();
data.location = settings.location.clone();
data.alias = settings.alias.clone();
data.data_telephone = settings.data_phone.clone();
data.voice_telephone = settings.voice_phone.clone();
data.birthdate = settings.birth_date.clone();
data
}

Expand Down Expand Up @@ -690,6 +685,8 @@ fn encode_emsi(data: &[&str]) -> TerminalResult<Vec<u8>> {
#[cfg(test)]
mod tests {
#![allow(clippy::field_reassign_with_default)]
use crate::{Address, Options};

use super::*;

#[test]
Expand Down Expand Up @@ -808,7 +805,7 @@ mod tests {

let mut back_data = Vec::new();
for b in EMSI_IRQ {
if let Some(data) = state.advance_char(&adr, *b, &opt).unwrap() {
if let Some(data) = state.advance_char(&adr.user_name, &adr.password, *b).unwrap() {
back_data = data;
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use directories::UserDirs;
use eframe::egui::{self};
use egui::{mutex::Mutex, FontId};
use icy_engine::Position;
use web_time::Instant;

use crate::{
check_error,
features::{AutoFileTransfer, AutoLogin},
features::AutoFileTransfer,
ui::{
buffer_update_thread::BufferUpdateThread,
dialogs::{self},
Expand Down Expand Up @@ -68,6 +69,11 @@ impl MainWindow {
connection: Arc::new(Mutex::new(Some(Box::new(connection)))),
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())),
}));

crate::ui::buffer_update_thread::run_update_thread(&cc.egui_ctx, buffer_update_thread.clone());
Expand All @@ -77,13 +83,10 @@ impl MainWindow {
//address_list: HoverList::new(),
state: MainWindowState { options, ..Default::default() },
initial_upload_directory,
auto_login: AutoLogin::new(""),
auto_file_transfer: AutoFileTransfer::default(),
screen_mode: ScreenMode::default(),
current_file_transfer: None,
#[cfg(target_arch = "wasm32")]
poll_thread,
sound_thread: SoundThread::new(),
is_fullscreen_mode,
export_dialog: dialogs::export_dialog::DialogState::default(),
upload_dialog: dialogs::upload_dialog::DialogState::default(),
Expand Down Expand Up @@ -160,19 +163,19 @@ impl eframe::App for MainWindow {

match self.get_mode() {
MainWindowMode::ShowTerminal => {
let res = self.update_state(ctx);
let res = self.update_state();
self.handle_terminal_key_binds(ctx, frame);
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
ctx.request_repaint_after(Duration::from_millis(150));
}
MainWindowMode::ShowDialingDirectory => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, true);
check_error!(self, res, false);
}
MainWindowMode::ShowSettings => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
self.state.show_settings(ctx, frame);
Expand Down Expand Up @@ -218,7 +221,7 @@ impl eframe::App for MainWindow {
ctx.request_repaint_after(Duration::from_millis(150));
}
MainWindowMode::ShowCaptureDialog => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
if !self.buffer_update_thread.lock().capture_dialog.show_caputure_dialog(ctx) {
Expand All @@ -227,21 +230,21 @@ impl eframe::App for MainWindow {
ctx.request_repaint_after(Duration::from_millis(150));
}
MainWindowMode::ShowExportDialog => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
self.show_export_dialog(ctx);
ctx.request_repaint_after(Duration::from_millis(150));
}
MainWindowMode::ShowUploadDialog => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
self.show_upload_dialog(ctx);
ctx.request_repaint_after(Duration::from_millis(150));
}
MainWindowMode::ShowIEMSI => {
let res = self.update_state(ctx);
let res = self.update_state();
self.update_terminal_window(ctx, frame, false);
check_error!(self, res, false);
dialogs::show_iemsi::show_iemsi(self, ctx);
Expand Down
Loading

0 comments on commit 6c76cdc

Please sign in to comment.