mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Avoid recreating struct each request in legacy protocol (#628)
This commit is contained in:
parent
510aea7207
commit
460054ced7
@ -31,6 +31,7 @@ from .json import loads as json_loads
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
_NO_RETRY_ERRORS = {errno.EHOSTDOWN, errno.EHOSTUNREACH, errno.ECONNREFUSED}
|
_NO_RETRY_ERRORS = {errno.EHOSTDOWN, errno.EHOSTUNREACH, errno.ECONNREFUSED}
|
||||||
|
_UNSIGNED_INT_NETWORK_ORDER = struct.Struct(">I")
|
||||||
|
|
||||||
|
|
||||||
def md5(payload: bytes) -> bytes:
|
def md5(payload: bytes) -> bytes:
|
||||||
@ -206,7 +207,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|||||||
await self.writer.drain()
|
await self.writer.drain()
|
||||||
|
|
||||||
packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE)
|
packed_block_size = await self.reader.readexactly(self.BLOCK_SIZE)
|
||||||
length = struct.unpack(">I", packed_block_size)[0]
|
length = _UNSIGNED_INT_NETWORK_ORDER.unpack(packed_block_size)[0]
|
||||||
|
|
||||||
buffer = await self.reader.readexactly(length)
|
buffer = await self.reader.readexactly(length)
|
||||||
response = TPLinkSmartHomeProtocol.decrypt(buffer)
|
response = TPLinkSmartHomeProtocol.decrypt(buffer)
|
||||||
@ -311,7 +312,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|||||||
:return: ciphertext to be send over wire, in bytes
|
:return: ciphertext to be send over wire, in bytes
|
||||||
"""
|
"""
|
||||||
plainbytes = request.encode()
|
plainbytes = request.encode()
|
||||||
return struct.pack(">I", len(plainbytes)) + bytes(
|
return _UNSIGNED_INT_NETWORK_ORDER.pack(len(plainbytes)) + bytes(
|
||||||
TPLinkSmartHomeProtocol._xor_payload(plainbytes)
|
TPLinkSmartHomeProtocol._xor_payload(plainbytes)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user