Skip to content

Commit 9782423

Browse files
committed
[fix] 修复注释错误,优化退出逻辑,解决 Linux Bin 中奇葩的 Ctrl + C bug(该问题在WSL中出现,其他未知)
1 parent bd255e0 commit 9782423

2 files changed

Lines changed: 48 additions & 22 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ __pycache__
22
config.json
33
log.txt
44
TouchFishFiles
5+
log.ndjson

LTS.py

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
- 启动阶段的成功提示
5757
蓝色 (blue):
5858
- [发给您的],[您发送的] 标签的颜色
59-
- help 指令显示的帮助消息中的第二段(可用的命令列表)
59+
- help 指令显示的帮助消息中的第三段和第六段(可用的命令列表)
6060
白色 (white):
6161
- 普通消息和加入提示的文本
6262
- 启动阶段的参数输入提示
@@ -68,7 +68,7 @@
6868
- 启动阶段中上面没有提到的所有文本
6969
青色 (cyan):
7070
- 所有除普通消息和加入提示以外的消息的文本
71-
- help 指令显示的帮助消息中的第一段和第三段(其余补充信息)
71+
- help 指令显示的帮助消息中的其余段落(其余补充信息)
7272
- 程序关闭时的「再见!」文本
7373
7474
(注:洋红色 (magenta) 目前没有使用过)
@@ -583,7 +583,7 @@ def process(message):
583583
# 防止干扰用户后续的终端使用
584584
prints("\033[0m\033[1;36m再见!\033[0m")
585585
EXIT_FLAG = True
586-
exit()
586+
return
587587
if message['type'] == "SERVER.CONFIG.CHANGE": # 服务端参数变更 (协议 3.4.2)
588588
announce(message['operator'])
589589
prints("配置项 {} 变更为:".format(message['key']) + str(message['value']), "cyan")
@@ -601,7 +601,7 @@ def process(message):
601601
prints("聊天室服务端已经关闭。", "cyan")
602602
prints("\033[0m\033[1;36m再见!\033[0m")
603603
EXIT_FLAG = True
604-
exit()
604+
return
605605

606606
# 从 my_socket 读取数据,每次 128 KiB,读完为止
607607
def read():
@@ -1357,7 +1357,7 @@ def do_exit(arg=None):
13571357
if users[i]['status'] in ["Pending", "Online", "Admin", "Root"]:
13581358
send_queue.put(json.dumps({'to': i, 'content': {'type': 'SERVER.STOP.ANNOUNCE'}})) # 协议 3.2.1
13591359
EXIT_FLAG = True
1360-
exit()
1360+
return
13611361

13621362
def do_help(arg=None):
13631363
print()
@@ -1400,7 +1400,7 @@ def thread_gate():
14001400
while True:
14011401
time.sleep(0.1)
14021402
if EXIT_FLAG:
1403-
exit()
1403+
return
14041404
break
14051405

14061406
# 尝试开启新连接
@@ -1493,7 +1493,7 @@ def thread_process():
14931493
while True:
14941494
time.sleep(0.1)
14951495
if EXIT_FLAG:
1496-
exit()
1496+
return
14971497
break
14981498
while not receive_queue.empty():
14991499
message = json.loads(receive_queue.get())
@@ -1527,7 +1527,7 @@ def thread_receive():
15271527
while True:
15281528
time.sleep(0.1)
15291529
if EXIT_FLAG:
1530-
exit()
1530+
return
15311531
break
15321532
for i in range(len(users)):
15331533
if users[i]['status'] in ["Online", "Admin", "Root"]:
@@ -1561,7 +1561,7 @@ def thread_send():
15611561
while True:
15621562
time.sleep(0.1)
15631563
if EXIT_FLAG:
1564-
exit()
1564+
return
15651565
break
15661566
while not send_queue.empty():
15671567
message = json.loads(send_queue.get())
@@ -1620,7 +1620,7 @@ def thread_log():
16201620
# 与其他线程不同,先写入日志再读取程序终止信号,
16211621
# 确保程序终止时没有日志残留在 log_queue 中
16221622
if EXIT_FLAG:
1623-
exit()
1623+
return
16241624
break
16251625

16261626
def thread_check():
@@ -1631,7 +1631,7 @@ def thread_check():
16311631
while True:
16321632
time.sleep(1) # 该部分对整体性能影响较大,因此执行频率下调至 1 秒一次
16331633
if EXIT_FLAG:
1634-
exit()
1634+
return
16351635
break
16361636
down = []
16371637
# 先完成全部下线用户检测工作再一并广播,
@@ -1660,7 +1660,7 @@ def thread_input():
16601660
while True:
16611661
time.sleep(0.1)
16621662
if EXIT_FLAG:
1663-
exit()
1663+
return
16641664
break
16651665
# 输出模式
16661666
try:
@@ -1693,7 +1693,7 @@ def thread_output():
16931693
while True:
16941694
time.sleep(0.1)
16951695
if EXIT_FLAG:
1696-
exit()
1696+
return
16971697
break
16981698
read()
16991699
message = get_message()
@@ -1731,6 +1731,8 @@ def main():
17311731
global receive_queue
17321732
global send_queue
17331733
global print_queue
1734+
1735+
can_read_config = True
17341736

17351737
# 尝试读取配置文件 (config.json),
17361738
# 检查规则详见第一部分的相关注释;
@@ -1789,24 +1791,47 @@ def main():
17891791
if not check_ip(tmp_config['ip']):
17901792
raise
17911793
config = tmp_config
1794+
except FileNotFoundError:
1795+
config = DEFAULT_SERVER_CONFIG
17921796
except:
17931797
config = DEFAULT_SERVER_CONFIG
1798+
can_read_config = False
17941799

17951800
os.system('') # 对 Windows 尝试开启 ANSI 转义字符(带颜色文本)支持
17961801
clear_screen()
17971802
prints("欢迎使用 TouchFish 聊天室!", "yellow")
17981803
prints("当前程序版本:{}".format(VERSION), "yellow")
1799-
prints("5 秒后将会自动按上次的配置启动。", "yellow")
1800-
prints("按下 Ctrl + C 以指定启动配置。", "yellow")
1801-
auto_start = True
1802-
try:
1804+
if (can_read_config):
1805+
prints("5 秒后将会自动按上次的配置启动。", "yellow")
1806+
prints("按下 Ctrl + C 以指定启动配置。", "yellow")
1807+
auto_start = True
1808+
18031809
for i in range(5, 0, -1):
1804-
prints("剩余 " + str(i) + " 秒...", "yellow")
1805-
time.sleep(1)
1806-
except KeyboardInterrupt:
1810+
try:
1811+
prints("剩余 " + str(i) + " 秒...", "yellow")
1812+
time.sleep(1)
1813+
except KeyboardInterrupt:
1814+
auto_start = False
1815+
break
1816+
except EOFError:
1817+
auto_start = False
1818+
break
1819+
except:
1820+
pass
1821+
else:
18071822
auto_start = False
1808-
except:
1809-
pass
1823+
prints("无法读取配置文件,请确认目录内是否存在 config.json,如果存在请使用管理员模式重新运行。", "red")
1824+
prints("请手动指定配置,如需退出请按 Ctrl + C,否则请按 Enter。", "yellow")
1825+
try:
1826+
input()
1827+
except KeyboardInterrupt:
1828+
os._exit(0)
1829+
except EOFError:
1830+
os._exit(0)
1831+
except Exception as e:
1832+
pass
1833+
os._exit(1)
1834+
18101835
tmp_side = None
18111836
if not auto_start:
18121837
tmp_side = input("\033[0m\033[1;37m启动类型 (Server = 服务端, Client = 客户端) [{}]:".format(config['side']))

0 commit comments

Comments
 (0)