Skip to content

Refactor socket read/write #196

Description

@emnoor

I have a few propositions regarding socket usage in send_pdu() and read_pdu() methods.

Here, instead of manually tracking how much is sent in a loop, using socket.sendall() method should make the code more concise and readable.

sent = 0
while sent < len(generated):
try:
sent_last = self._socket.send(generated[sent:])
except socket.error as e:
self.logger.warning(e)
raise exceptions.ConnectionError()
if sent_last == 0:
raise exceptions.ConnectionError()
sent += sent_last

In read_pdu, we don't check len(raw_len) == 4. For less than 4 bytes we raise PDUError. But we could possibly get raw_len in multiple with recv, just like when we are reading the rest of the PDU.

I propose we refactor the "keep recving until we got all bytes" into a separate method (i.e. _recv_exact(n)), and then read raw_len and raw_pdu with this method.

If you are interested, I will send pull requests for these changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions