mirror of
https://github.com/Xevion/tcp-chat.git
synced 2025-12-06 05:16:45 -06:00
Improve main thread logging, commit stop_flag
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
|
||||
from server import handler
|
||||
@@ -21,23 +22,24 @@ clients = []
|
||||
|
||||
# Receiving / Listening Function
|
||||
def receive():
|
||||
stop_flag: bool = False
|
||||
try:
|
||||
logger.debug('Waiting for connections...')
|
||||
while True:
|
||||
conn = None
|
||||
|
||||
try:
|
||||
# Accept Connection
|
||||
logger.debug('Waiting for connections...')
|
||||
conn, address = server.accept()
|
||||
logger.info(f"New connection from {address}")
|
||||
|
||||
client = handler.Client(conn, address, clients)
|
||||
client = handler.Client(conn, address, clients, lambda: stop_flag)
|
||||
clients.append(client)
|
||||
client.request_nickname()
|
||||
|
||||
# Inform all clients of new client, give new client connections list
|
||||
for client in clients:
|
||||
client.send_connections_list()
|
||||
if len(clients) > 0:
|
||||
logger.debug('Informing all connected clients of incoming connection.')
|
||||
for client in clients:
|
||||
client.send_connections_list()
|
||||
|
||||
# Start Handling Thread For Client
|
||||
thread = threading.Thread(target=client.handle, name=client.id[:8])
|
||||
@@ -50,8 +52,9 @@ def receive():
|
||||
logger.critical(e, exc_info=e)
|
||||
break
|
||||
except KeyboardInterrupt:
|
||||
logger.info('User stopped server manually.')
|
||||
return
|
||||
logger.info('User stopped server manually. Enabling stop flag.')
|
||||
stop_flag = True
|
||||
sys.exit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user