Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/airtouch2/common/NetClient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import errno
import logging
from socket import gaierror
import socket
#from socket import gaierror
from typing import Callable
from airtouch2.common.interfaces import CoroCallback, Serializable, TaskCreator

Expand Down Expand Up @@ -33,8 +34,14 @@ def __init__(self, host: str, port: int, on_connect: CoroCallback, handle_messag
async def connect(self) -> bool:
"""Opens connection to the server, returns True/False if successful/unsuccessful"""
_LOGGER.debug(f"Connecting to {self._host_ip} on port {self._host_port}")

try:
self._reader, self._writer = await asyncio.open_connection(self._host_ip, self._host_port)
sock = self._writer.get_extra_info('socket')
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Enable keep-alive
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 60) # Idle time before sending probes (seconds)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10) # Interval between probes (seconds)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5) # Number of probes before considering connection dead
except OSError as e:
_LOGGER.warning(f"Could not connect to host {self._host_ip}")
if isinstance(e, gaierror):
Expand Down Expand Up @@ -117,4 +124,4 @@ async def _try_reconnect(self) -> None:
retries += 1
if not retries % 60 or retries == 4:
_LOGGER.info("Server is not responding, will continue trying to reconnect every 10s")
_LOGGER.info("Reconnected")
_LOGGER.info("Reconnected")