Skip to content

Commit

Permalink
Merge pull request #149 from simonlindholm/line-buffering
Browse files Browse the repository at this point in the history
Do manual line buffering in Python
  • Loading branch information
kazimuth authored Jan 26, 2018
2 parents 39e166e + 0e06ed9 commit 02451fd
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions battlecode-manager/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,26 @@ def __init__(self, *args, **kwargs):
self.error = ""
self.logged_in = False
self.is_unix_stream = is_unix_stream
self.buffer_small = b''
self.buffer_large = []

super(ReceiveHandler, self).__init__(*args, **kwargs)

def read_line(self):
while True:
if self.buffer_small:
pos = self.buffer_small.find(b'\n')
if pos != -1:
ret = b''.join(self.buffer_large) + self.buffer_small[:pos]
self.buffer_small = self.buffer_small[pos+1:]
self.buffer_large = []
return ret
else:
self.buffer_large.append(self.buffer_small)
self.buffer_small = self.request.recv(4096)
if not self.buffer_small:
raise IOError("reached socket EOF before finding newline")

def get_next_message(self) -> object:
'''
Returns the json loaded object of the next string that is sent over the
Expand All @@ -361,16 +379,14 @@ def get_next_message(self) -> object:
recv_socket = self.request
game = self.game

wrapped_socket = recv_socket.makefile('rwb', 1)
logging.debug("Client %s: Waiting for next message", self.client_id)
try:
data = next(wrapped_socket)
data = self.read_line()
except (StopIteration, IOError):
print("{} has not sent message for {} seconds, assuming they're dead".format(
self.game.get_player(self.client_id)['player'],
TIMEOUT
))
wrapped_socket.close()
recv_socket.close()
if bc.Team.Red == self.game.get_player(self.client_id)['player'].team:
self.game.winner = 'player2'
Expand All @@ -386,7 +402,6 @@ def get_next_message(self) -> object:
self.game.game_over = True
raise TimeoutError()
except KeyboardInterrupt:
wrapped_socket.close()
recv_socket.close()
if bc.Team.Red == self.game.get_player(self.client_id)['player'].team:
self.game.winner = 'player2'
Expand All @@ -401,8 +416,6 @@ def get_next_message(self) -> object:
self.game.disconnected = True
self.game.game_over = True
raise KeyboardInterrupt()
finally:
wrapped_socket.close()

data = data.decode("utf-8").strip()
return data
Expand Down Expand Up @@ -432,11 +445,9 @@ def send_message(self, obj: object) -> None:
logging.debug("Client %s: Sending message %s", self.client_id,
encoded_message)

wrapped_socket = send_socket.makefile('rwb', 1)
try:
wrapped_socket.write(encoded_message)
self.request.sendall(encoded_message)
except IOError:
wrapped_socket.close()
send_socket.close()
print("{} has not accepted message for {} seconds, assuming they're dead".format(
[p for p in self.game.players if p['id'] == self.client_id][0]['player'],
Expand All @@ -456,7 +467,6 @@ def send_message(self, obj: object) -> None:
self.game.game_over = True
raise TimeoutError()
except KeyboardInterrupt:
wrapped_socket.close()
send_socket.close()
if bc.Team.Red == self.game.get_player(self.client_id)['player'].team:
self.game.winner = 'player2'
Expand All @@ -471,8 +481,6 @@ def send_message(self, obj: object) -> None:
self.game.disconnected = True
self.game.game_over = True
raise KeyboardInterrupt()
finally:
wrapped_socket.close()
return

def message(self, state_diff):
Expand Down

0 comments on commit 02451fd

Please sign in to comment.