and make reconnecting work properly maybe?
This commit is contained in:
parent
7b5816bed7
commit
ed811bcfdc
|
@ -280,39 +280,13 @@ impl AccessorySocket {
|
||||||
Ok(packet)
|
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> {
|
async fn get_next(&mut self) -> Result<Vec<u8>, HomekitError> {
|
||||||
// max packet size + authtag size + associated data size
|
// max packet size + authtag size + associated data size
|
||||||
let mut buf = [0; 1024 + 16 + 2];
|
let mut buf = [0; 1024 + 16 + 2];
|
||||||
|
|
||||||
let mut read_num = self.socket.read(&mut buf).await?;
|
let read_num = self.socket.read(&mut buf).await?;
|
||||||
let mut tries = 0;
|
if read_num == 0 {
|
||||||
|
return Err(HomekitError::NoData);
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(encryption) = self.socket_encryption.as_mut() {
|
if let Some(encryption) = self.socket_encryption.as_mut() {
|
||||||
|
|
|
@ -428,6 +428,8 @@ pub enum HomekitError {
|
||||||
Discovery(#[from] DiscoveryError),
|
Discovery(#[from] DiscoveryError),
|
||||||
#[error("timeout")]
|
#[error("timeout")]
|
||||||
Timeout,
|
Timeout,
|
||||||
|
#[error("no data")]
|
||||||
|
NoData,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
|
|
Loading…
Reference in a new issue