50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
|
from importlib import reload
|
||
|
|
||
|
import re
|
||
|
import socket
|
||
|
from handlers import serverPingHandler, mainHandler
|
||
|
|
||
|
class Client:
|
||
|
BUFF_SIZE = 65535
|
||
|
HANDLERS = {
|
||
|
re.compile(b"SAMP"): serverPingHandler
|
||
|
}
|
||
|
|
||
|
def __init__(self, server, addr):
|
||
|
self.server = server
|
||
|
self.addr = addr
|
||
|
self.state = 0
|
||
|
self.__writebuffer = []
|
||
|
|
||
|
def handle_data(self, data):
|
||
|
found = False
|
||
|
for pattern, handler in self.HANDLERS.items():
|
||
|
if pattern.match(data):
|
||
|
queue = handler.handle(self, data)
|
||
|
if queue:
|
||
|
self.write_to_buffer(queue)
|
||
|
found = True
|
||
|
|
||
|
if not found:
|
||
|
queue = mainHandler.handle(self, data)
|
||
|
if queue:
|
||
|
self.write_to_buffer(queue)
|
||
|
#print("?[{}] -> {} / {}".format(self.addr, data, data.hex()))
|
||
|
|
||
|
def write_to_buffer(self, data):
|
||
|
if type(data) == bytes:
|
||
|
self.__writebuffer.append(data)
|
||
|
else:
|
||
|
self.__writebuffer += data
|
||
|
|
||
|
def write_buffer_size(self):
|
||
|
return len(self.__writebuffer)
|
||
|
|
||
|
def write_socket(self):
|
||
|
if len(self.__writebuffer) > 0:
|
||
|
write_chunk = self.__writebuffer.pop(0)
|
||
|
print("[{}] <- {}".format(self.addr, write_chunk))
|
||
|
self.server.socket.sendto(write_chunk, self.addr)
|
||
|
|
||
|
def restart():
|
||
|
reload(serverPingHandler)
|