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

Read full json message from socket in i3ipc.aio.connection (#1) #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions i3ipc/aio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,22 @@ def _message_reader(self):
except Exception as e:
self.main_quit(_error=e)

def _read_message(self):
def _recv(self, size):
buf = bytearray(size)
mem = memoryview(buf)
sock = self._sub_socket
while len(mem):
if not (rsize := sock.recv_into(mem)):
return b''
mem = mem[rsize:]
mem.release()
return buf

def _read_message(self):
error = None
buf = b''
try:
buf = self._sub_socket.recv(_struct_header_size)
buf = self._recv(_struct_header_size)
except ConnectionError as e:
error = e

Expand All @@ -325,7 +335,7 @@ def _read_message(self):

magic, message_length, event_type = _unpack_header(buf)
assert magic == _MAGIC
raw_message = self._sub_socket.recv(message_length)
raw_message = self._recv(message_length)
message = json.loads(raw_message)

# events have the highest bit set
Expand Down