from twisted.internet import protocol, reactor from twisted.internet import reactor import xdrlib import pyui import constants import environment """ This class represents a PySoldier UDP server. Will listen for UDP packets at the specified port and broadcast datagrams on another. """ class Server(protocol.DatagramProtocol): """ Creates a Server using the specified port numbers for reading and writing, respectively. """ def __init__(self, world, readPort, writePort): self.world = world self.readPort = readPort self.writePort = writePort self.sendToAllIP = constants.sendToAllIP self.updateTime = pyui.readTimer() self.lastupdate = 0 self.clients = [] #client IP addresses reactor.listenMulticast(self.readPort, self) print "server loaded" print "server listening at port "+str(readPort) print "server will write from port "+str(writePort) """ Initializes this Server """ def startProtocol(self): print "starting protocol" self.transport.joinGroup(self.sendToAllIP) """ Presently improperly implemented. Read and react to network input """ def datagramReceived(self, datagram, address): #make sure address is registered if not address[0] in self.clients: if len(self.clients) < 1: """ The above if-statement ensures that only one client can join. That exact statement should be removed when more players are allowed to join """ self.clients.append(address[0]) unpacker = xdrlib.Unpacker(datagram) up = unpacker.unpack_int() down = unpacker.unpack_int() left = unpacker.unpack_int() right = unpacker.unpack_int() fire = unpacker.unpack_int() throw = unpacker.unpack_int() facing = unpacker.unpack_int() control = environment.Env.remotePlayer.controller control.up = up control.down = down control.left = left control.right = right control.fire = fire control.throw = throw environment.Env.remotePlayer.avatar.gun.facing = facing """ Helper method for writing the contents of a packer """ def writePacker(self, packer, address): buffer = packer.get_buffer() self.transport.write(buffer, address) """ Makes the packer and initiates different information to be sendt in the correct numbering """ def writeUpdate(self, address): packer = xdrlib.Packer() self.packSoldier(packer, environment.Env.localPlayer.avatar) self.packSoldier(packer, environment.Env.remotePlayer.avatar) packer.pack_int(environment.Env.localPlayer.controller.fire) packer.pack_int(environment.Env.localPlayer.controller.throw) packer.pack_int(environment.Env.localPlayer.fragCount) packer.pack_int(environment.Env.remotePlayer.fragCount) self.writePacker(packer, address) """ Packs all info from soldier """ def packSoldier(self, packer, soldier): packer.pack_float(soldier.posX) packer.pack_float(soldier.posY) packer.pack_int(soldier.gun.facing) packer.pack_float(soldier.velocityX) packer.pack_float(soldier.velocityY) packer.pack_int(soldier.health) """ Perform all instructions received since last update call. """ def update(self): currentTime = pyui.readTimer() reactor.runUntilCurrent() reactor.doSelect(0) environment.Env.localPlayer.controller.processPeripheralsServer() environment.Env.localPlayer.update() environment.Env.remotePlayer.update() if currentTime - self.lastupdate > 0.05: for clientIp in self.clients: self.writeUpdate((clientIp,self.writePort)) self.lastupdate = currentTime