and make reconnecting work properly maybe?

This commit is contained in:
Alex Janka 2024-04-02 08:49:03 +11:00
parent 7b5816bed7
commit ed811bcfdc
2 changed files with 5 additions and 29 deletions

View file

@ -280,39 +280,13 @@ impl AccessorySocket {
Ok(packet)
}
async fn reconnect(&mut self) -> Result<TcpStream, std::io::Error> {
self.socket.flush().await?;
self.socket.shutdown().await?;
TcpStream::connect(format!("{}:{}", self.ip, self.port)).await
}
async fn get_next(&mut self) -> Result<Vec<u8>, HomekitError> {
// max packet size + authtag size + associated data size
let mut buf = [0; 1024 + 16 + 2];
let mut read_num = self.socket.read(&mut buf).await?;
let mut tries = 0;
while read_num == 0 {
if tries > 20 {
log::error!("reconnect unsuccessful");
return Err(ConnectionError::Http.into());
}
tries += 1;
log::info!("read 0 bytes - about to reconnect");
std::thread::sleep(std::time::Duration::from_millis(200));
match tokio::time::timeout(Duration::from_secs(5), self.reconnect()).await {
Ok(Ok(socket)) => self.socket = socket,
_ => continue,
}
read_num =
match tokio::time::timeout(Duration::from_secs(5), self.socket.read(&mut buf)).await
{
Ok(Ok(val)) => val,
_ => continue,
}
let read_num = self.socket.read(&mut buf).await?;
if read_num == 0 {
return Err(HomekitError::NoData);
}
if let Some(encryption) = self.socket_encryption.as_mut() {

View file

@ -428,6 +428,8 @@ pub enum HomekitError {
Discovery(#[from] DiscoveryError),
#[error("timeout")]
Timeout,
#[error("no data")]
NoData,
}
#[derive(Debug, Error)]