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

Commit

Permalink
Fixed some update thread issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Oct 3, 2023
1 parent 38ed9a9 commit 6d873dc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 68 deletions.
40 changes: 25 additions & 15 deletions src/com/telnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl ComTelnetImpl {
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)?;
Ok(Self {
tcp_stream,
state: ParserState::Data,
Expand All @@ -318,9 +319,9 @@ impl ComTelnetImpl {
})
}

fn parse(&mut self, data: &[u8]) -> TermComResult<Option<Vec<u8>>> {
fn parse(&mut self, data: &[u8]) -> TermComResult<Vec<u8>> {
if self.use_raw_transfer {
return Ok(Some(data.to_vec()));
return Ok(data.to_vec());
}
let mut buf = Vec::with_capacity(data.len());
for b in data {
Expand Down Expand Up @@ -460,7 +461,7 @@ impl ComTelnetImpl {
}
}
}
Ok(Some(buf))
Ok(buf)
}
}

Expand All @@ -483,23 +484,32 @@ impl Com for ComTelnetImpl {

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

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])
}
let data = self.parse(&buf[0..size])?;
/*
for ch in &data {
let ch = *ch;
match ch {
b'\\' => print!("\\\\"),
b'\n' => println!("\\n"),
b'\r' => print!("\\r"),
b'\"' => print!("\\\""),
_ => {
if ch < b' ' || ch == b'\x7F' {
print!("\\x{ch:02X}");
} else if ch > b'\x7F' {
print!("\\u{{{ch:02X}}}");
} else {
print!("{}", ch as char);
}
}
}
}*/
Ok(Some(data))
}
Err(ref e) => {
self.tcp_stream.set_nonblocking(true)?;
if e.kind() == io::ErrorKind::WouldBlock {
return Ok(None);
}
Expand Down
47 changes: 16 additions & 31 deletions src/ui/buffer_update_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use egui::mutex::Mutex;
use icy_engine_egui::BufferView;
use std::{collections::VecDeque, sync::Arc, thread};
use std::{sync::Arc, thread};
use web_time::{Duration, Instant};

use super::{
Expand All @@ -30,8 +30,10 @@ pub struct BufferUpdateThread {

impl BufferUpdateThread {
pub fn update_state(&mut self, ctx: &egui::Context) -> TerminalResult<bool> {
let r: Result<_, _> = self.sound_thread.lock().update_state();
// check_error!(self, r, false);
let r = self.sound_thread.lock().update_state();
if let Err(err) = r {
log::error!("{err}");
}

let data = if let Some(con) = self.connection.lock().as_mut() {
con.update_state()?;
Expand All @@ -41,50 +43,48 @@ impl BufferUpdateThread {
if con.is_data_available()? {
con.read_buffer()
} else {
VecDeque::new()
Vec::new()
}
} else {
return Ok(false);
};
Ok(self.update_buffer(ctx, data))
}

fn update_buffer(&mut self, ctx: &egui::Context, mut data: std::collections::VecDeque<u8>) -> bool {
self.capture_dialog.append_data(&mut data);
fn update_buffer(&mut self, ctx: &egui::Context, data: Vec<u8>) -> bool {
self.capture_dialog.append_data(&data);
let has_data = !data.is_empty();
let mut set_buffer_dirty = false;
if !data.is_empty() {
println!("data : {}", self.last_update.elapsed().as_millis());
}
let buffer_view = &mut self.buffer_view.lock();

while !data.is_empty() {
let ch = data.pop_front().unwrap();
if let Some(autologin) = &mut self.auto_login {
for ch in data {
if let Some(autologin) = &mut self.auto_login {
if let Some(con) = self.connection.lock().as_mut() {
if let Err(err) = autologin.try_login(con, ch) {
log::error!("{err}");
}
if autologin.logged_in {
self.auto_login = None;
}
}
}
/*
match ch {
b'\\' => print!("\\\\"),
b'\n' => println!("\\n"),
b'\r' => print!("\\r"),
b'\"' => print!("\\\""),
_ => {
if ch < b' ' || ch == b'\x7F' {
if *ch < b' ' || *ch == b'\x7F' {
print!("\\x{ch:02X}");
} else if ch > b'\x7F' {
} else if *ch > b'\x7F' {
print!("\\u{{{ch:02X}}}");
} else {
print!("{}", char::from_u32(ch as u32).unwrap());
print!("{}", *ch as char);
}
}
}
}*/

if self.print_char(buffer_view, ch) {
set_buffer_dirty = true;
}
Expand All @@ -102,28 +102,13 @@ impl BufferUpdateThread {
ctx.request_repaint();
return false;
}
/*
if self.get_options().iemsi.autologin {
if let Some(adr) = self.dialing_directory_dialog.addresses.addresses.get(self.dialing_directory_dialog.cur_addr) {
if let Some(con) = &mut self.buffer_update_thread.lock().connection {
if con.is_connected() {
if let Err(err) = self.auto_login.run_autologin(con, adr) {
log::error!("{err}");
}
}
}
}
}*/
/*
*/
true
}

pub fn print_char(&self, buffer_view: &mut BufferView, c: u8) -> bool {
let result = buffer_view.print_char(c as char);
match result {
Ok(icy_engine::CallbackAction::SendString(result)) => {
println!("send string: '{}'", result.replace("\x1B", "\\x1b"));
if let Some(con) = self.connection.lock().as_mut() {
if con.is_connected() {
let r = con.send(result.as_bytes().to_vec());
Expand Down
25 changes: 8 additions & 17 deletions src/ui/connect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{Address, Terminal, TerminalResult};
use std::{collections::VecDeque, mem, sync::mpsc};
use std::{collections::VecDeque, sync::mpsc};
use web_time::{Duration, Instant};

pub trait DataConnection {
fn is_data_available(&mut self) -> TerminalResult<bool>;
fn read_buffer(&mut self) -> VecDeque<u8>;
fn read_buffer(&mut self) -> Vec<u8>;
fn read_u8(&mut self) -> TerminalResult<u8>;
fn read_exact(&mut self, size: usize) -> TerminalResult<Vec<u8>>;
fn send(&mut self, vec: Vec<u8>) -> TerminalResult<()>;
Expand All @@ -18,7 +18,7 @@ pub struct Connection {
pub rx: mpsc::Receiver<SendData>,
pub tx: mpsc::Sender<SendData>,
end_transfer: bool,
buf: VecDeque<u8>,
buf: std::collections::VecDeque<u8>,
}

impl DataConnection for Connection {
Expand All @@ -27,10 +27,8 @@ impl DataConnection for Connection {
Ok(!self.buf.is_empty())
}

fn read_buffer(&mut self) -> std::collections::VecDeque<u8> {
let mut result = VecDeque::new();
mem::swap(&mut result, &mut self.buf);
result
fn read_buffer(&mut self) -> Vec<u8> {
self.buf.drain(0..self.buf.len()).collect()
}

fn read_u8(&mut self) -> TerminalResult<u8> {
Expand Down Expand Up @@ -162,12 +160,6 @@ impl Connection {
.send(SendData::OpenConnection(OpenConnectionData::from(call_adr, timeout, window_size)))?;
Ok(())
}

pub(crate) fn push_data_back(&mut self, mut data: VecDeque<u8>) {
while !data.is_empty() {
self.buf.push_front(data.pop_back().unwrap());
}
}
}

/// A more lightweight version of `Address` that is used for the connection
Expand Down Expand Up @@ -256,10 +248,9 @@ impl DataConnection for TestConnection {
Ok(!self.get_recv_buffer().is_empty())
}

fn read_buffer(&mut self) -> VecDeque<u8> {
let mut result = VecDeque::new();
mem::swap(&mut result, self.get_recv_buffer());
result
fn read_buffer(&mut self) -> Vec<u8> {
let len = self.get_recv_buffer().len();
self.get_recv_buffer().drain(0..len).collect()
}

fn read_u8(&mut self) -> TerminalResult<u8> {
Expand Down
5 changes: 2 additions & 3 deletions src/ui/dialogs/capture_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::VecDeque;
use std::io::Write;

use eframe::egui::{self, RichText};
Expand Down Expand Up @@ -27,10 +26,10 @@ pub enum Message {
}

impl DialogState {
pub(crate) fn append_data(&mut self, data: &mut VecDeque<u8>) {
pub(crate) fn append_data(&mut self, data: &[u8]) {
if self.capture_session {
if let Ok(mut data_file) = std::fs::OpenOptions::new().create(true).append(true).open(&self.capture_filename) {
if let Err(err) = data_file.write_all(data.make_contiguous()) {
if let Err(err) = data_file.write_all(data) {
if !self.show_capture_error {
self.show_capture_error = true;
log::error!("{err}");
Expand Down
3 changes: 1 addition & 2 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use std::time::Instant;

use eframe::egui::Key;

use crate::features::{AutoFileTransfer, AutoLogin};
use crate::features::AutoLogin;
use crate::ui::connect::DataConnection;
use crate::util::SoundThread;
use crate::Options;
use crate::{protocol::FileDescriptor, TerminalResult};

Expand Down

0 comments on commit 6d873dc

Please sign in to comment.