Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions console_backend/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use std::{
fmt::Debug,
fs, io,
net::{SocketAddr, TcpStream, ToSocketAddrs},
fs,
io::{self, ErrorKind},
net::{TcpStream, ToSocketAddrs},
ops::Drop,
path::{Path, PathBuf},
thread,
thread::JoinHandle,
time::Duration,
};

use anyhow::anyhow;
use crossbeam::channel::Sender;
use log::{error, info};

use crate::client_sender::BoxedClientSender;
use crate::constants::*;
use crate::errors::*;
use crate::process_messages::{process_messages, Messages};
use crate::shared_state::{ConnectionState, SharedState};
use crate::status_bar::StatusBar;
Expand Down Expand Up @@ -293,42 +292,51 @@ pub struct TcpConnection {
name: String,
host: String,
port: u16,
socket_addrs: SocketAddr,
}

impl TcpConnection {
fn new(host: String, port: u16) -> Result<Self> {
let name = format!("{}:{}", host, port);
let socket_addrs = TcpConnection::socket_addrs(&name)?;
Ok(Self {
name,
host,
port,
socket_addrs,
})
Ok(Self { name, host, port })
}

fn name(&self) -> String {
self.name.clone()
}

fn socket_addrs(name: &str) -> Result<SocketAddr> {
let mut socket = name.to_socket_addrs()?;
if let Some(s) = socket.next() {
Ok(s)
} else {
Err(anyhow!("{}", TCP_CONNECTION_PARSING_FAILURE))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this line will have ever been triggered, as it looks like to_socket_addr will return an error both if the name can't be resolved to an address or if it can't be parsed - so I've removed this error for now.

fn open_connection(&self) -> io::Result<TcpStream> {
let addresses = self.name.to_socket_addrs()?;

let mut errors = vec![];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This errors vec is not being used anywhere, maybe just log the errors instead? Or you could maybe return them instead of the the last error you have at the bottom

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, good catch - was meaning to add the errors to the returned error if no connection worked, which I've done now.


for address in addresses {
info!("Attempting to connect to resolved address {:?}", address);
match TcpStream::connect_timeout(
&address,
Duration::from_millis(SERIALPORT_READ_TIMEOUT_MS),
) {
Ok(stream) => return Ok(stream),
Err(err) => {
info!(
"Error connecting to resolved address {:?}: {:?}",
address, err
);
errors.push(err);
}
}
}

Err(io::Error::new(
ErrorKind::ConnectionRefused,
format!("Could not connect to {:?}: {:#?}", self.name, errors),
))
}

fn try_connect(
self,
shared_state: Option<&SharedState>,
) -> io::Result<(Box<dyn io::Read + Send>, Box<dyn io::Write + Send>)> {
let rdr = TcpStream::connect_timeout(
&self.socket_addrs,
Duration::from_millis(SERIALPORT_READ_TIMEOUT_MS),
)?;
let rdr = self.open_connection()?;
rdr.set_read_timeout(Some(Duration::from_millis(SERIALPORT_READ_TIMEOUT_MS)))?;
let writer = rdr.try_clone()?;
info!("Connected to tcp stream!");
Expand Down
2 changes: 0 additions & 2 deletions console_backend/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ pub const CONVERT_TO_STR_FAILURE: &str = "error converting to str";
pub(crate) const UNABLE_TO_STOP_TIMER_THREAD_FAILURE: &str = "unable to kill running timer thread";
pub(crate) const UNABLE_TO_SEND_INS_UPDATE_FAILURE: &str = "unable to send an ins status update";
pub(crate) const THREAD_JOIN_FAILURE: &str = "thread join failure";
pub(crate) const TCP_CONNECTION_PARSING_FAILURE: &str =
"unable to parse the provided string for ip string";
pub(crate) const CONSOLE_LOG_JSON_TO_STRING_FAILURE: &str = "unable to convert json to string";
pub(crate) const CROSSBEAM_SCOPE_UNWRAP_FAILURE: &str = "unable to unwrap crossbeam scope";
pub(crate) const UNABLE_TO_CLONE_UPDATE_SHARED: &str = "unable to clone update shared";
Expand Down