Skip to content
Closed
Show file tree
Hide file tree
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
24 changes: 18 additions & 6 deletions e3dc/_e3dc_rscp_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# Licensed under a MIT license. See LICENSE for details

import socket
import struct

from ._RSCPEncryptDecrypt import RSCPEncryptDecrypt
from ._rscpLib import RscpMessage, rscpDecode, rscpEncode, rscpFrame
from ._rscpLib import RscpMessage, rscpDecode, rscpEncode, rscpFrame, rscpFrameDecode
from ._rscpTags import RscpError, RscpTag, RscpType

DEFAULT_PORT = 5033
Expand Down Expand Up @@ -69,11 +70,22 @@ def _send(self, plainMsg: RscpMessage) -> None:
self.socket.send(encData)

def _receive(self):
data = self.socket.recv(BUFFER_SIZE)
if len(data) == 0:
raise RSCPKeyError
decData = rscpDecode(self.encdec.decrypt(data))[0]
return decData
# A single socket.recv() call is not guaranteed to return a complete
# RSCP frame (large responses can be split across multiple TCP reads),
# so keep receiving and decrypting until a full frame is available.
decData = b""
while True:
data = self.socket.recv(BUFFER_SIZE)
if len(data) == 0:
raise RSCPKeyError
decData += self.encdec.decrypt(data)
try:
rscpFrameDecode(decData)
except struct.error:
# not enough data yet for a full frame: keep receiving
continue
break
return rscpDecode(decData)[0]

def sendCommand(self, plainMsg: RscpMessage) -> None:
"""Sending RSCP command.
Expand Down
2 changes: 1 addition & 1 deletion e3dc/_rscpLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def rscpEncode(
rscptypeHex = getHexRscpType(rscptype)
rscptype = getRscpType(rscptype)

loggable_data = '<redacted>' if tag in (RscpTag.SERVER_PASSWD, RscpTag.SERVER_USER ) else data
loggable_data = '<redacted>' if tag in (RscpTag.SERVER_PASSWD, RscpTag.SERVER_USER) else data
logger.debug("> %s %s %s", tag, rscptype, loggable_data)

if isinstance(data, str):
Expand Down
Loading