from twisted.internet import reactor, protocol
import struct

UDP_SERVER_RESPONSE = "\x80"
UDP_CLIENT_REQUEST = "\x00"

class HostedGame():
    pass 

"""
len     type            val

2 bytes "session-id"    random?
1 byte  "mode"          hex(00)=>request(client->server), hex(80)=>response(server->client)   
1 byte  "content-len"   hex encoded content length


game_mode=3;inet=1;maxpl=1;nation=de;numpl=1;password=0;players=§(10)←Patric
k∟§(-1)←;run=1;server_addr=62.143.7.179;server_name=tot98 (Patrick);server_port=
58282;vmaj=1;vmin=3;vtype=f

game=EM4;lang=de;mod=
"""


class CommunityServer():
    def __init__(self):
        self.games = []
    def addGame(self, client, request):
        pass
    def endGame(self, client):
        pass
    def getGames(self):
        return self.games
    def processUDP(self, data):
        session = data[:2]
        #Byte 3 should be mode \x00, ignore. Byte 4 should be content-len, ignore
        request = data[4:]
        print request
        response = "game_mode=3;inet=1;maxpl=4;nation=de;numpl=1;password=0;players=\xA7(10)\x1BnoBlubb\x1C\xA7(-1)\x1B;run=1;server_addr=88.76.57.26;server_name=Blubberium (noBlubb);server_port=58282;vmaj=1;vmin=1;vtype=f"
        responselen = struct.pack('B', len(response))
        return session + UDP_SERVER_RESPONSE + responselen + response

class TCPFactory(protocol.Protocol):   
    def __init__(self, master):
        self.server = master
    def doStart():
        pass
    def dataReceived(self, data):
        self.transport.write(data)
        print data

class UDPFactory(protocol.DatagramProtocol):
    def __init__(self, master):
        self.server = master
    def doStart():
        pass
    def dataReceived(self, data, address):
        response = self.server.processUDP(request)
        self.transport.write(response, address)


def main():
    masterserver = CommunityServer()
    #reactor.listenTCP(54321,TCPFactory(masterserver))
    reactor.listenUDP(54321,UDPFactory(masterserver))
    reactor.run()

if __name__ == '__main__':
    main()
@@ -117,38 +117,8 @@
 0%22%0A%0A
-class HostedGame():%0A    pass %0A
 %0A%22%22%22
@@ -330,443 +330,193 @@
 th%0A%0A
-%0Agame_mode=3;inet=1;maxpl=1;nation=de;numpl=1;password=0;players=%C2%A7(10)%E2%86%90Patric%0Ak%E2%88%9F%C2%A7(-1)%E2%86%90;run=1;server_addr=62.143.7.179;server_name=tot98 (Patrick);server_port=%0A58282;vmaj=1;vmin=3;vtype=f%0A%0Agame=EM4;lang=de;mod=%0A%22%22%22%0A%0A%0Aclass CommunityServer():%0A    def __init__(self):%0A        self.games = %5B%5D%0A    def addGame(self, client, request):%0A        pass%0A    def endGame(self, client):%0A        pass%0A    def getGames(self):%0A        return self.games
+%22%22%22%0Aclass HostedGame():%0A    pass %0A%0Aclass CommunityServer():%0A    def __init__(self):%0A        self.games = %5B%5D%0A    #Byte 3 should be mode %5Cx00, ignore. Byte 4 should be content-len, ignore
 %0A   
@@ -574,90 +574,10 @@
 %5B:2%5D
-%0A
   
-      #Byte 3 should be mode %5Cx00, ignore. Byte 4 should be content-len, ignore
 %0A   
@@ -1080,32 +1080,36 @@
     def doStart(
+self
 ):%0A        pass%0A
@@ -1319,16 +1319,20 @@
 doStart(
+self
 ):%0A     
@@ -1424,23 +1424,20 @@
 cessUDP(
-request
+data
 )%0A      

work in progress