Skip to content

Commit

Permalink
Use socket.sendall to send packets
Browse files Browse the repository at this point in the history
If only some of the data was transmitted, writelines
does not attempt to send the remaining data, while
sendall continues to send data until either all data
has been sent or an error occurs.
  • Loading branch information
hieplpvip authored and quantum5 committed Jan 22, 2022
1 parent 0646647 commit f5cf158
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dmoj/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def _connect(self):

log.info('Starting handshake with: [%s]:%s', self.host, self.port)
self.input = self.conn.makefile('rb')
self.output = self.conn.makefile('wb', 0)
self.handshake(problems, versions, self.name, self.key)
log.info('Judge "%s" online: [%s]:%s', self.name, self.host, self.port)

Expand Down Expand Up @@ -238,7 +237,8 @@ def _send_packet(self, packet: dict):
raw = zlib.compress(utf8bytes(json.dumps(packet)))
with self._lock:
try:
self.output.writelines((PacketManager.SIZE_PACK.pack(len(raw)), raw))
assert self.conn is not None
self.conn.sendall(PacketManager.SIZE_PACK.pack(len(raw)) + raw)
except Exception: # connection reset by peer
log.exception('Exception while sending packet to site, will not attempt to reconnect! Quitting judge.')
os._exit(1)
Expand Down

0 comments on commit f5cf158

Please sign in to comment.