I am making a multiplayer platforming game, and currently there are server performance issues once I have about 7+ clients connected. I was told earlier last week that I should investigate socketserver
since previously I had been writing my own server using merely socket
.
Well, after 6 hours of implementation I've got my server working with the new code.
As it stands now, I've seen no performance increase, but that's not surprising. I'm hoping that someone here could point me in the right direction and show me how to get more performance out of this thing.
The server is currently 600 lines of code, but I believe the bottleneck is the socketserver
stuff.
So here is (hopefully) the relevant stuff. And as always... thank you very much :)
import socket
import sys
import threading
import time
import pygame
import random
import json
import levels
import socketserver
class MyUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
gameserver.listen(data, self.client_address)
class ThreadingUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
pass
class GameServer():
def __init__(self):
self.message_count = 100000
self.message_dump = None
self.text_block = []
self.client_list = []
self.client_hero_dict = {}
self.client_obj_dict = {}
self.queue = []
def listen(self, data, addr):
if addr not in self.client_list:
Client(addr, self.dataHandler(data)) #Makes a client object for future reference
print("Accepted connection: " + repr(addr))
print("List of all connections: " + repr(self.client_list))
for line in servermap.map: #This sends the map to the newly connected client
line = "TILE" + line
threading.Thread(target=self.clientHandler(line, addr))
for obj in identifier.object_dict:
threading.Thread(target=self.clientHandler(identifier.object_dict[obj]))
self.dataHandler(data, addr)
def textHandler(self, data):
if data:
data = "^^%s^^" % str(self.message_count) + data
self.message_count += 1
threading.Thread(target=self.clientHandler(data))
def dataHandler(self, data, addr=None):
data = data.decode()
if data[0:2] == "^^": # if data is a chat message
self.textHandler(data[2:])
if data[0:2] == "*^": # if data is a movement request
self.client_hero_dict[addr].actionfsm(data[2:])
if data[0:6] == "**UN**": #if data is a username
# self.client_obj_dict[addr] = data[6:]
return data[6:]
# self.client_hero_dict[addr].name = data[6:]
# print(self.client_hero_dict[addr].name + " is the Username for " + repr(str(addr)))
def clientHandler(self, message_to_send, specific_client=None):
if type(message_to_send) == dict: #I use dictionaries as simple ways to send object (player/sprite) update information
message_to_send = json.dumps(message_to_send)
if specific_client:
server.socket.sendto(message_to_send.encode(), specific_client)
else:
for client in self.client_list:
server.socket.sendto(message_to_send.encode(), client)
I'm using threads but I'm not sure about the best way to utilize them. Having said that, I've also called socketserver.ThreadingMiIn
but I not sure how to utilize it.