forked from PhoenixFire6934/Classic-Brawl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
100 lines (82 loc) · 2.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import logging
import socket
import time
import os
from threading import *
from Logic.Device import Device
from Logic.Player import Players
from Packets.Factory import packets
from Utils.Config import Config
logging.basicConfig(filename="errors.log", level=logging.INFO, filemode="w")
def _(*args):
print('[INFO]', end=' ')
for arg in args:
print(arg, end=' ')
print()
class Server:
def __init__(self, ip: str, port: int):
self.server = socket.socket()
self.port = port
self.ip = ip
def start(self):
if not os.path.exists('./data.db'):
open('data.db', 'w').close()
if not os.path.exists('./config.json'):
print("Creating config.json...")
Config.create_config(self)
self.server.bind((self.ip, self.port))
_(f'Server started! Ip: {self.ip}, Port: {self.port}')
while True:
self.server.listen()
client, address = self.server.accept()
_(f'New connection! Ip: {address[0]}')
ClientThread(client, address).start()
class ClientThread(Thread):
def __init__(self, client, address):
super().__init__()
self.client = client
self.address = address
self.device = Device(self.client)
self.player = Players(self.device)
def recvall(self, length: int):
data = b''
while len(data) < length:
s = self.client.recv(length)
if not s:
print("Receive Error!")
break
data += s
return data
def run(self):
last_packet = time.time()
try:
while True:
header = self.client.recv(7)
if len(header) > 0:
last_packet = time.time()
packet_id = int.from_bytes(header[:2], 'big')
length = int.from_bytes(header[2:5], 'big')
data = self.recvall(length)
if packet_id in packets:
_(f'Received packet! Id: {packet_id}')
message = packets[packet_id](self.client, self.player, data)
message.decode()
message.process()
else:
_(f'Packet not handled! Id: {packet_id}')
if time.time() - last_packet > 10:
print(f"[INFO] Ip: {self.address[0]} disconnected!")
self.client.close()
break
except ConnectionAbortedError:
print(f"[INFO] Ip: {self.address[0]} disconnected!")
self.client.close()
except ConnectionResetError:
print(f"[INFO] Ip: {self.address[0]} disconnected!")
self.client.close()
except TimeoutError:
print(f"[INFO] Ip: {self.address[0]} disconnected!")
self.client.close()
if __name__ == '__main__':
server = Server('0.0.0.0', 9339)
server.start()