-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBlazeClient.py
80 lines (63 loc) · 2.42 KB
/
BlazeClient.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
import random
from utils.bf1.blaze.BlazeSocket import BlazeServerREQ, BlazeSocket
class BlazeClient:
def __init__(self):
self.socket: BlazeSocket = None
async def connect(
self, host: str = "diceprodblapp-08.ea.com", port: int = 10539, callback=None
) -> BlazeSocket:
if not self.socket:
self.socket = await BlazeSocket.create(host, port, callback)
return self.socket
async def close(self):
if self.socket:
await self.socket.close()
self.socket = None
class BlazeClientManager:
def __init__(self):
self.clients_by_pid = {}
async def get_socket_for_pid(self, pid=None) -> BlazeSocket | None:
if not pid:
connected_clients = [
client for client in self.clients_by_pid.values() if client.connect
]
try:
return random.choice(connected_clients)
except IndexError:
return None
if pid in self.clients_by_pid:
client = self.clients_by_pid[pid]
if client.connect:
return client
try:
await client.close()
except Exception as e:
from loguru import logger
logger.error(f"关闭客户端连接时出错: {e}")
del self.clients_by_pid[pid]
new_client = BlazeClient()
host, port = await BlazeServerREQ.get_server_address()
await new_client.connect(host, port, callback=None)
if new_client.socket and new_client.socket.connect:
self.clients_by_pid[pid] = new_client.socket
return self.clients_by_pid[pid]
else:
return None
async def close_all(self):
from loguru import logger
for client in self.clients_by_pid.values():
try:
await client.close()
except Exception as e:
logger.error(f"关闭客户端连接时出错: {e}")
self.clients_by_pid.clear()
async def remove_client(self, pid: str):
if pid in self.clients_by_pid:
client = self.clients_by_pid[pid]
try:
await client.close()
except Exception as e:
from loguru import logger
logger.error(f"关闭客户端连接时出错: {e}")
del self.clients_by_pid[pid]
BlazeClientManagerInstance = BlazeClientManager()