def __handleDownloadMap(data, msgID, client): ''' Prepare and send requested map to client data (PyDatagramIterator): the list of data sent with this datagram msgID (Int): the message ID client (Connection): the connection that tendNehis datagram came from''' # Unpack message data id = data.getUint32() resp = 0 filename = self._mapStore.getMap(id=id)['filename'] mapString = self._mapStore.loadMapAsString(filename) if (mapString == None): LOG.debug('No such map') resp = 0 mapString = '' else: LOG.debug('Sending map') resp = 1 # Send response pkg = NetDatagram() pkg.addUint16(MSG_JOINGAME_RES) pkg.addUint32(resp) pkg.addString(mapString) self._cWriter.send(pkg, client)
def __handleAuth(self, data, msgID, client): ''' Try to authorize the connecting user, send the result. data (PyDatagramIterator): the list of data sent with this datagram msgID (Int): the message ID client (Connection): the connection that this datagram came from''' # Unpack message data username = data.getString() password = data.getString() auth = 0 # Look for the username in the list of registered users for u in self.registeredUsers: if u.username == username: if u.connected: auth = 1 self._console.printNotice('%s: User %s already connected'%(client.getAddress().getIpString(),username)) break elif u.password != password: auth = 2 self._console.printNotice('%s: User %s gave invalid password'%(client.getAddress().getIpString(),username)) break else: auth = 3 u.connect(client) self.connectedUsers.append(u) self._console.printNotice('%s: User %s connected with pass %s' %(client.getAddress().getIpString(),username,password)) # Send response pkg = NetDatagram() pkg.addUint16(MSG_AUTH_RES) pkg.addUint32(auth) self._cWriter.send(pkg, client)
def __sendJoinGameReq(self, id): ''' Join a game on the server. id (int): the id of the game to join''' LOG.debug('Sending join game request') # Send Request if (self._connected and self._authorized): pkg = NetDatagram() pkg.addUint16(MSG_JOINGAME_REQ) pkg.addUint32(id) self._cWriter.send(pkg, self._tcpSocket)
def _sendDownloadReq(type, id): ''' Download a file from the server.''' LOG.debug("Downloading type=%s, id=%s"%(type, id)) if (self._connected and self._authorized): pkg = NetDatagram() if (type == "MAP"): pkg.addUint16(MSG_DOWNLOADMAP_REQ) pkg.addUint32(id) elif (type == "UPDATE"): pkg.addUint16(MSG_DOWNLOADUPD_REQ) pkg.addUint32(id) self._cWriter.send(pkg, self._tcpSocket)
def __sendNewGameReq(self, gamename, mapID, numplayers): ''' Create a new game on the server. name (String): the name of the game mapID (String): the MD5 ID of the map maxplayers (Int): the max players allowed''' LOG.debug('Sending new game request %s'%map) # Send Request if (self._connected and self._authorized): pkg = NetDatagram() pkg.addUint16(MSG_NEWGAME_REQ) pkg.addString(gamename) pkg.addString(mapID) pkg.addUint32(numplayers) self._cWriter.send(pkg, self._tcpSocket)
def __handleGameList(self, data, msgID, client): ''' Assemble a list of active games and send it to the requesting client. data (PyDatagramIterator): the list of data sent with this datagram msgID (Int): the message ID client (Connection): the connection that this datagram came from''' # Send response pkg = NetDatagram() pkg.addUint16(MSG_GAMELIST_RES) if (len(self.games) == 0): pkg.addString('EOT') # Nothing to transmit else: pkg.addString('SOT') # Start Of Transmission for i,g in enumerate(self.games): pkg.addInt32(g.id) pkg.addString(g.name) pkg.addUint32(g.numPlayers) pkg.addString(g.map.name) pkg.addString(g.mapFile) pkg.addUint32(g.startTime) pkg.addUint32(g.turnNumber) if i < len(self.games)-1: pkg.addString('T') # Still tranmitting pkg.addString('EOT') # End Of Transmission self._cWriter.send(pkg, client) self._console.printNotice('%s: Request for game list.' %(client.getAddress().getIpString()))
def __handleJoinGame(self, data, msgID, client): ''' Add client to the requested game. data (PyDatagramIterator): the list of data sent with this datagram msgID (Int): the message ID client (Connection): the connection that tendNehis datagram came from''' # Unpack message data id = data.getUint32() resp = 0 # Find the game game = None mapMD5 = "00000000000000000000000000000000" print self.games for g in self.games: if g.id == id: game = g mapMD5 = g.mapID if game == None: LOG.debug('No such game') resp = 0 elif len(game.connections) >= game.numPlayers: LOG.debug('Game full') resp = 1 else: game.addPlayer(client) LOG.debug('Ok, joining game') resp = 2 # Send response pkg = NetDatagram() pkg.addUint16(MSG_JOINGAME_RES) pkg.addUint32(resp) pkg.addString(mapMD5) self._cWriter.send(pkg, client) self._console.printNotice('%s: Request to join game id %d, gave %d.'%(client.getAddress().getIpString(),id,resp))