Refactor kick and tcp_send in Client

This commit is contained in:
Maxim Khomutov 2023-07-08 02:18:04 +03:00
parent 3a40dc34cd
commit 482615af22

View File

@ -42,26 +42,28 @@ class Client:
self.alive = False self.alive = False
return True return True
def kick(self, reason): async def kick(self, reason):
self.log.info(f"Client: \"IP: {self.addr!r}; ID: {self.cid}\" - kicked with reason: \"{reason}\"") self.log.info(f"Client: \"IP: {self.addr!r}; ID: {self.cid}\" - kicked with reason: \"{reason}\"")
self.tcp_send(b"K" + bytes(reason, "utf-8")) await self.tcp_send(b"K" + bytes(reason, "utf-8"))
self.socket.close() # self.writer.close()
# await self.writer.wait_closed()
self.alive = False self.alive = False
def tcp_send(self, data): async def tcp_send(self, data):
# TNetwork.cpp; Line: 383 # TNetwork.cpp; Line: 383
# BEAMP TCP protocol sends a header of 4 bytes, followed by the data. # BEAMP TCP protocol sends a header of 4 bytes, followed by the data.
# [][][][][][]...[] # [][][][][][]...[]
# ^------^^---...-^ # ^------^^---...-^
# size data # size data
data = data.replace(b" ", b"_") self.log.debug(f"tcp_send({data})")
if len(data) == 10: if len(data) == 10:
data += b"." data += b"."
header = len(data).to_bytes(4, "little") header = len(data).to_bytes(4, "little", signed=True)
self.log.debug(f'len(data) {len(data)}; header: {header}; send {header + data}') self.log.debug(f'len(data) {len(data)}; send {header + data}')
self.socket.send(header + data) self.writer.write(header + data)
await self.writer.drain()
class Core: class Core: