109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
import json
|
|
import socket
|
|
import threading
|
|
|
|
class Server:
|
|
|
|
__address:str
|
|
__tcpPort:str
|
|
__udpPort:str
|
|
__tcpSocket:socket
|
|
__udpSocket:socket
|
|
__clientThread:threading.Thread
|
|
|
|
def __init__(self, address:str, tcpPort:str, udpPort:str):
|
|
self.__address = address
|
|
self.__tcpPort = tcpPort
|
|
self.__udpPort = tcpPort
|
|
self.__tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
self.__udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.startServer()
|
|
|
|
# handles starting the server and assigning socket values to the local reference
|
|
def startServer(self):
|
|
self.__tcpSocket.bind(self.__address, self.__tcpPort)
|
|
self.__tcpSocket.listen()
|
|
print(f"tcp server started on: {self.__address}:{self.__tcpPort}")
|
|
|
|
self.__udpSocket.bind(self.__address, self.__udpPort)
|
|
self.__udpSocket.listen()
|
|
print(f"tcp server started on: {self.__address}:{self.__udpPort}")
|
|
|
|
# server loop forwards connection to handleConnection
|
|
while True:
|
|
# accept incoming connection
|
|
# TODO: validate this connection is a valid game connection
|
|
client_tcpSocket, client_tcpAddress = self.__tcpSocket.accept()
|
|
|
|
# create network thread for connection
|
|
self.__TCPclientThread = threading.Thread(target=self.handleTCPConnection, args=(client_tcpSocket, client_tcpAddress))
|
|
self.__TCPclientThread.start()
|
|
|
|
client_udpSocket, client_udpAddress = self.__udpSocket.accept()
|
|
self.__UDPclientThread = threading.Thread(target=self.handleUDPConnection, args=(client_udpSocket, client_udpAddress))
|
|
self.__UDPclientThread.start()
|
|
|
|
|
|
# handles ticking the game loop server side converting data and passing of to the event handler
|
|
def handleTCPConnection(self, socket:socket, address):
|
|
# states that a connection has been established
|
|
print(f"Connected with {address}")
|
|
|
|
# Communication with client
|
|
while True:
|
|
data = socket.recv(1024)
|
|
if not data:
|
|
break
|
|
|
|
# decode message for handling
|
|
message = data.decode()
|
|
messageJson = json.loads(message)
|
|
|
|
if messageJson["user"] in self.__users:
|
|
self.handleTCPEvents(messageJson)
|
|
else:
|
|
break
|
|
|
|
print(f"received message from {address}: {message}")
|
|
|
|
# handles ticking the game loop server side converting data and passing of to the event handler
|
|
def handleUDPConnection(self, socket:socket, address):
|
|
# states that a connection has been established
|
|
print(f"Connected with {address}")
|
|
|
|
# Communication with client
|
|
while True:
|
|
data = socket.recv(1024)
|
|
if not data:
|
|
break
|
|
|
|
# decode message for handling
|
|
message = data.decode()
|
|
messageJson = json.loads(message)
|
|
|
|
if messageJson["user"] in self.__users:
|
|
self.handleUDPEvents(messageJson)
|
|
else:
|
|
break
|
|
|
|
print(f"received message from {address}: {message}")
|
|
|
|
# handles passing of event data to the right functions
|
|
def handleTCPEvents(self, event):
|
|
# decide which event should be performed
|
|
if event["event"] == "login":
|
|
pass
|
|
elif event["event"] == "join_queue":
|
|
# count queue
|
|
# move start game if 2 players are in the queue
|
|
# remove player from the queue once game starts
|
|
pass
|
|
elif event["event"] == "leave_queue":
|
|
# just remove player from the queue
|
|
pass
|
|
|
|
def handleUDPEvents(self, event):
|
|
if event["event"] == "gameAction":
|
|
# pass of to own handler function which handles game logic
|
|
pass |