Skip to content

Commit

Permalink
Solve transport close() as not inherited method. (#2098)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen authored Mar 8, 2024
1 parent 27b2915 commit 374ca73
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ def register(self, custom_response_class: ModbusResponse) -> None:
"""
self.framer.decoder.register(custom_response_class)

def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override]
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
if reconnect:
self.connection_lost(asyncio.TimeoutError("Server not responding"))
else:
super().close(intern)
super().close()

def idle_time(self) -> float:
"""Time before initiating next transaction (call **sync**).
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ async def connect(self) -> bool:
Log.debug("Connecting to {}.", self.comm_params.host)
return await self.base_connect()

def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override]
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
super().close(reconnect=reconnect, intern=intern)
super().close(reconnect=reconnect)


class ModbusSerialClient(ModbusBaseSyncClient):
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ async def connect(self) -> bool:
)
return await self.base_connect()

def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override]
def close(self, reconnect: bool = False) -> None:
"""Close connection."""
super().close(reconnect=reconnect, intern=intern)
super().close(reconnect=reconnect)


class ModbusTcpClient(ModbusBaseSyncClient):
Expand Down
23 changes: 14 additions & 9 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async def listen(self) -> bool:
self.transport = self.transport[0]
except OSError as exc:
Log.warning("Failed to start server {}", exc)
# self.close(intern=True)
self.__close()
return False
return True

Expand All @@ -285,7 +285,7 @@ def connection_lost(self, reason: Exception | None) -> None:
if not self.transport or self.is_closing:
return
Log.debug("Connection lost {} due to {}", self.comm_params.comm_name, reason)
self.close(intern=True)
self.__close()
if (
not self.is_server
and not self.listener
Expand Down Expand Up @@ -390,16 +390,11 @@ def send(self, data: bytes, addr: tuple | None = None) -> None:
else:
self.transport.write(data) # type: ignore[attr-defined]

def close(self, intern: bool = False, reconnect: bool = False) -> None:
"""Close connection.
def __close(self, reconnect: bool = False) -> None:
"""Close connection (internal).
:param intern: (default false), True if called internally (temporary close)
:param reconnect: (default false), try to reconnect
"""
if self.is_closing:
return
if not intern:
self.is_closing = True
if self.transport:
self.transport.close()
self.transport = None # type: ignore[assignment]
Expand All @@ -418,6 +413,16 @@ def close(self, intern: bool = False, reconnect: bool = False) -> None:
if self.listener:
self.listener.active_connections.pop(self.unique_id)

def close(self, reconnect: bool = False) -> None:
"""Close connection (external).
:param reconnect: (default false), try to reconnect
"""
if self.is_closing:
return
self.is_closing = True
self.__close(reconnect=reconnect)

def reset_delay(self) -> None:
"""Reset wait time before next reconnect to minimal period."""
self.reconnect_delay_current = self.comm_params.reconnect_delay or 0.0
Expand Down

0 comments on commit 374ca73

Please sign in to comment.