added udp connectivity to game server
This commit is contained in:
@ -5,36 +5,48 @@ import threading
|
||||
class Server:
|
||||
|
||||
__address:str
|
||||
__port:str
|
||||
__socket:socket
|
||||
__tcpPort:str
|
||||
__udpPort:str
|
||||
__tcpSocket:socket
|
||||
__udpSocket:socket
|
||||
__clientThread:threading.Thread
|
||||
|
||||
def __init__(self, address:str, port:str):
|
||||
def __init__(self, address:str, tcpPort:str, udpPort:str):
|
||||
self.__address = address
|
||||
self.__port = port
|
||||
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
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.__socket.bind(self.__address, self.__port)
|
||||
self.__socket.listen()
|
||||
print(f"server started on: {self.__address}:{self.__port}")
|
||||
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_socket, client_address = self.__socket.accept()
|
||||
client_tcpSocket, client_tcpAddress = self.__tcpSocket.accept()
|
||||
|
||||
# create network thread for connection
|
||||
self.__clientThread = threading.Thread(target=self.handleConnection, args=(client_socket, client_address))
|
||||
self.__clientThread.start()
|
||||
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 handleConnection(self, socket:socket, address):
|
||||
def handleTCPConnection(self, socket:socket, address):
|
||||
# states that a connection has been established
|
||||
print(f"Connected with {address}")
|
||||
|
||||
@ -49,14 +61,36 @@ class Server:
|
||||
messageJson = json.loads(message)
|
||||
|
||||
if messageJson["user"] in self.__users:
|
||||
self.handleEvents(messageJson)
|
||||
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 handleEvents(self, event):
|
||||
def handleTCPEvents(self, event):
|
||||
# decide which event should be performed
|
||||
if event["event"] == "login":
|
||||
pass
|
||||
@ -68,6 +102,8 @@ class Server:
|
||||
elif event["event"] == "leave_queue":
|
||||
# just remove player from the queue
|
||||
pass
|
||||
elif event["event"] == "gameAction":
|
||||
|
||||
def handleUDPEvents(self, event):
|
||||
if event["event"] == "gameAction":
|
||||
# pass of to own handler function which handles game logic
|
||||
pass
|
Reference in New Issue
Block a user