2024-04-17 01:01:17 +08:00
|
|
|
import json
|
|
|
|
import multiprocessing
|
|
|
|
from multiprocessing.connection import Listener
|
|
|
|
import threading
|
2024-04-16 00:45:21 +08:00
|
|
|
|
|
|
|
|
2024-04-17 01:01:17 +08:00
|
|
|
class ProcessServer:
|
2024-04-16 00:45:21 +08:00
|
|
|
def __init__(self):
|
2024-04-17 16:45:36 +08:00
|
|
|
try:
|
|
|
|
self.Process_port = 3244
|
|
|
|
self.Process_server = 'localhost'
|
|
|
|
self.Process_server_listener = Listener((self.Process_server, self.Process_port))
|
|
|
|
self.Process_client_link_dick = {}
|
|
|
|
Process_client_link_Thread = threading.Thread(target=self.Process_client_link)
|
|
|
|
Process_client_link_Thread.start()
|
|
|
|
except:
|
|
|
|
print("进程通信端口绑定失败")
|
|
|
|
|
2024-04-17 01:01:17 +08:00
|
|
|
|
|
|
|
def Process_client_link(self):
|
|
|
|
while True:
|
|
|
|
client_connect = self.Process_server_listener.accept()
|
2024-04-17 16:45:36 +08:00
|
|
|
client_Thread_recv = threading.Thread(target=self.Process_client_recv, args=(client_connect,))
|
|
|
|
client_Thread_recv.start()
|
2024-04-17 01:01:17 +08:00
|
|
|
|
2024-04-17 16:45:36 +08:00
|
|
|
def Process_client_recv(self, client_Thread_recv):
|
2024-04-17 01:01:17 +08:00
|
|
|
while True:
|
2024-04-17 16:45:36 +08:00
|
|
|
data_json = client_Thread_recv.recv()
|
2024-04-17 01:01:17 +08:00
|
|
|
data = json.loads(data_json)
|
2024-04-17 16:45:36 +08:00
|
|
|
if data['target'] == 'Server':
|
|
|
|
if data['function'] == 'Name':
|
|
|
|
self.Process_client_link_dick[data['content']] = client_Thread_recv
|
|
|
|
else:
|
|
|
|
self.Process_client_pick(data)
|
2024-04-17 01:01:17 +08:00
|
|
|
|
|
|
|
def Process_client_send(self, target, function, content):
|
|
|
|
connect = self.Process_client_link_dick[target]
|
|
|
|
data = {"target": target, "function": function, "content": content}
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
connect.send(data_json)
|
|
|
|
|
2024-04-17 16:45:36 +08:00
|
|
|
def Process_client_pick(self, data):
|
|
|
|
if data['target'] == 'ALL':
|
|
|
|
for value in self.Process_client_link_dick.values():
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
value.send(data_json)
|
|
|
|
else:
|
|
|
|
if data['target'] in self.Process_client_link_dick.keys():
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
self.Process_client_link_dick[data['target']].send(data_json)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
ProcessServer()
|