Skip to content

Commit 8a0850f

Browse files
authored
Merge pull request #43 from SentientPlatypus/fix-nonblocking-read-desync
2 parents cc246ed + f25807c commit 8a0850f

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎rlbot/interface.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ def __del__(self):
7070
def _int_to_bytes(val: int) -> bytes:
7171
return val.to_bytes(2, byteorder="big")
7272

73-
def _read_int(self) -> int:
74-
return int.from_bytes(self._read_exact(2), "big")
75-
7673
def _read_exact(self, n: int) -> bytes:
7774
assert self.socket is not None, "Socket has not been established"
7875

@@ -88,7 +85,19 @@ def _read_exact(self, n: int) -> bytes:
8885
return bytes(buff)
8986

9087
def read_message(self) -> bytes:
91-
size = self._read_int()
88+
assert self.socket is not None, "Socket has not been established"
89+
90+
# Only this first recv may raise BlockingIOError when the socket is non-blocking.
91+
# Once any byte of a message has been consumed, the rest must be read blocking,
92+
# otherwise the consumed bytes are lost and the stream loses its framing.
93+
header = self.socket.recv(2)
94+
if len(header) == 0:
95+
raise EOFError
96+
self.socket.setblocking(True)
97+
98+
if len(header) == 1:
99+
header += self._read_exact(1)
100+
size = int.from_bytes(header, "big")
92101
return self._read_exact(size)
93102

94103
def send_bytes(self, data: bytes):

‎rlbot/version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.0.0-beta.55"
1+
__version__ = "2.0.0-beta.56"

0 commit comments

Comments
 (0)