Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure the command length header data be 4 bytes #153

Merged
merged 1 commit into from
Aug 29, 2019
Merged
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
6 changes: 6 additions & 0 deletions naz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,12 @@ async def receive_data(self, TESTING: bool = False) -> typing.Union[None, bytes]
# `client.reader` and `client.writer` should not have timeouts since they are non-blocking
# https://github.com/komuw/naz/issues/116
command_length_header_data = await self.reader.read(4)

# make sure the header data be 4 bytes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jwenjian I wonder if using await self.reader.readexactly(4)[1] would be better?
although that raises IncompleteReadError[2] which we would have to handle somehow.

  1. https://docs.python.org/3.6/library/asyncio-stream.html#asyncio.StreamReader.readexactly
  2. https://docs.python.org/3.6/library/asyncio-stream.html#asyncio.IncompleteReadError

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also relevant[3].

I do not know which is better. Let me know your thoughts

  1. https://stackoverflow.com/questions/28452724/method-asyncio-streams-streamreader-readbyte-count-reads-too-few-bytes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readexactly method is better if we have a proper way to handle the IncompleteReadError

log an error and ignore it or raise it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is no timeout in the read*() functions?

So if the readexactly(4) raises an error, it means that there is no more data and the read partial bytes(length must be less than 4) is not part of an valid SMPP PDU, so I suggest to abandon it then continue the while loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, like the way you handle the message body in Line 1900, it should works fine to the headers.

also don't know which is better, so maybe we can use the way I do in this pr util we find out a more efficient way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your input. I think we can start with this and improve if/when it becomes neccessary

I'll think about it some more.

while len(command_length_header_data) != 4:
more_bytes = await self.reader.read(4 - len(command_length_header_data))
command_length_header_data = command_length_header_data + more_bytes

except (
ConnectionError,
TimeoutError,
Expand Down