-
Notifications
You must be signed in to change notification settings - Fork 2
connection: Try all resolved TCP addresses [CPP-470] #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -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)) | ||
| fn open_connection(&self) -> io::Result<TcpStream> { | ||
| let addresses = self.name.to_socket_addrs()?; | ||
|
|
||
| let mut errors = vec![]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
|
|
||
There was a problem hiding this comment.
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_addrwill 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.