2024-04-16 00:45:21 +08:00
|
|
|
import multiprocessing
|
2024-04-17 01:01:17 +08:00
|
|
|
import os
|
2024-04-11 01:34:16 +08:00
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import json
|
2024-04-12 00:56:58 +08:00
|
|
|
import threading
|
2024-04-17 16:45:36 +08:00
|
|
|
from .Process_Client import ProcessClient
|
2024-04-11 01:34:16 +08:00
|
|
|
|
|
|
|
|
2024-04-17 01:01:17 +08:00
|
|
|
class Session_server(ProcessClient):
|
2024-04-12 00:56:58 +08:00
|
|
|
def __init__(self):
|
2024-04-17 01:01:17 +08:00
|
|
|
ProcessClient.__init__(self)
|
2024-04-13 01:40:25 +08:00
|
|
|
self.socker_ip = "127.0.0.1"
|
2024-04-17 16:45:36 +08:00
|
|
|
self.socker_port = 7868
|
2024-04-13 01:40:25 +08:00
|
|
|
self.server_socket = None
|
2024-04-11 01:34:16 +08:00
|
|
|
self.server_status = False # 服务器状态
|
2024-04-12 00:56:58 +08:00
|
|
|
self.link_server_Thread = threading.Thread(target=self.link_server)
|
|
|
|
self.link_server_Thread.start()
|
|
|
|
self.receive_server_Thread = threading.Thread(target=self.receive_server)
|
2024-04-11 01:34:16 +08:00
|
|
|
|
2024-04-17 01:01:17 +08:00
|
|
|
current_file_path = __file__
|
|
|
|
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
|
|
|
self.Process_client_send("Server", "Name", current_file_name)
|
2024-04-16 00:45:21 +08:00
|
|
|
|
2024-04-12 00:56:58 +08:00
|
|
|
def link_server(self):
|
2024-04-13 01:40:25 +08:00
|
|
|
while True:
|
|
|
|
if not self.server_status:
|
2024-04-12 00:56:58 +08:00
|
|
|
time.sleep(1)
|
2024-04-13 01:40:25 +08:00
|
|
|
if self.server_socket is not None:
|
|
|
|
self.server_socket = None
|
|
|
|
try:
|
|
|
|
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.server_socket.connect((self.socker_ip, self.socker_port))
|
|
|
|
self.server_status = True
|
2024-04-13 13:52:36 +08:00
|
|
|
if not self.receive_server_Thread.is_alive():
|
2024-04-13 01:40:25 +08:00
|
|
|
self.receive_server_Thread.start()
|
|
|
|
except Exception as a:
|
|
|
|
self.server_status = False
|
2024-04-13 13:52:36 +08:00
|
|
|
print("连接错误:" + str(a))
|
2024-04-16 00:45:21 +08:00
|
|
|
finally:
|
2024-04-17 01:01:17 +08:00
|
|
|
self.Process_client_send("ALL", "server_status", self.server_status)
|
|
|
|
pass
|
2024-04-12 00:56:58 +08:00
|
|
|
|
|
|
|
def receive_server(self):
|
2024-04-11 01:34:16 +08:00
|
|
|
while self.server_status:
|
2024-04-12 00:56:58 +08:00
|
|
|
try:
|
2024-04-13 13:52:36 +08:00
|
|
|
receive_content_json = self.server_socket.recv(1024).decode('utf-8')
|
2024-04-12 00:56:58 +08:00
|
|
|
receive_content = json.loads(receive_content_json)
|
2024-04-18 13:45:10 +08:00
|
|
|
self.content_pick(receive_content)
|
2024-04-13 01:40:25 +08:00
|
|
|
except Exception as a:
|
2024-04-19 00:48:24 +08:00
|
|
|
print("客户端接收错误:" + str(a))
|
2024-04-12 00:56:58 +08:00
|
|
|
self.server_status = False
|
2024-04-17 01:01:17 +08:00
|
|
|
self.Process_client_send("ALL", "server_status", self.server_status)
|
2024-04-11 01:34:16 +08:00
|
|
|
|
2024-04-12 00:56:58 +08:00
|
|
|
def send_server(self, genre, target, content):
|
2024-04-11 01:34:16 +08:00
|
|
|
if self.server_status:
|
2024-04-13 01:40:25 +08:00
|
|
|
try:
|
|
|
|
data = {"genre": genre, "target": target, "data": content,
|
|
|
|
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
|
2024-04-20 17:36:36 +08:00
|
|
|
data_json = json.dumps(data) + '\n'
|
2024-04-13 01:40:25 +08:00
|
|
|
self.server_socket.send(data_json.encode("utf-8"))
|
|
|
|
except Exception as a:
|
2024-04-13 13:52:36 +08:00
|
|
|
print("发送错误:" + str(a))
|
2024-04-13 01:40:25 +08:00
|
|
|
self.server_status = False
|
2024-04-17 01:01:17 +08:00
|
|
|
self.Process_client_send("ALL", "server_status", self.server_status)
|
|
|
|
|
|
|
|
def Process_client_pick(self, data):
|
|
|
|
if data['target'] in ['ALL', 'Session_server']:
|
|
|
|
match data['function']:
|
|
|
|
case 'server_status':
|
|
|
|
self.server_status = data['content']
|
|
|
|
case 'send_server':
|
|
|
|
self.send_server(data['content']['genre'], data['content']['target'], data['content']['content'])
|
2024-04-18 13:45:10 +08:00
|
|
|
|
2024-04-20 17:36:36 +08:00
|
|
|
def content_pick(self, data):
|
2024-04-18 13:45:10 +08:00
|
|
|
match data['genre']:
|
|
|
|
case '注册' | '登录':
|
|
|
|
self.Process_client_send("Login", "login_page_receive", data)
|
|
|
|
case '数据更新':
|
2024-04-20 01:36:15 +08:00
|
|
|
self.Process_client_send("File_operate", "save_data", data['data'])
|
2024-04-20 17:36:36 +08:00
|
|
|
case '聊天记录':
|
|
|
|
if data['data']['Type'] == 'text':
|
|
|
|
self.Process_client_send("File_operate", "save_data", {"History": data['data']})
|
|
|
|
if data['target'] == '接收':
|
|
|
|
self.Process_client_send("Chat_main", "Chat_screen_show", data['data'])
|