def getData(self): data = self.passedData self.passedData = [] while self.cReader.dataAvailable(): datagram = NetDatagram() if self.cReader.getData(datagram): data.append( (datagram.getConnection(), self.processData(datagram))) return data
def begin(self): if self.cReader.dataAvailable(): datagram = NetDatagram() # catch the incoming data in this instance # Check the return value; if we were threaded, someone else could have # snagged this data before we did if self.cReader.getData(datagram): myIterator = PyDatagramIterator(datagram) msgID = myIterator.getUint8() #If not in our protocol range then we just reject if msgID < 0 or msgID > 200: return #Order of these will need to be optimized later #We now pull out the rest of our headers remotePacketCount = myIterator.getUint8() ack = myIterator.getUint8() acks = myIterator.getUint16() hashID = myIterator.getUint16() sourceOfMessage = datagram.getConnection() if msgID == protocol.NEW_SHIP: log.info("New ship") playerPilotID = myIterator.getUint16() shipID = myIterator.getUint16() shipName = myIterator.getString() health = myIterator.getUint8() position = Point3(myIterator.getFloat32(), myIterator.getFloat32(), myIterator.getFloat32()) linearVelocity = Vec3(myIterator.getFloat32(), myIterator.getFloat32(), myIterator.getFloat32()) rotiation = VBase3(myIterator.getFloat32(), myIterator.getFloat32(), myIterator.getFloat32()) angularVelocity = Vec3(myIterator.getFloat32(), myIterator.getFloat32(), myIterator.getFloat32()) ship = sandbox.addEntity(shipID) component = ships.PilotComponent() component.accountEntityID = playerPilotID ship.addComponent(component) component = ships.BulletPhysicsComponent() messenger.send("addSpaceShip", [component, shipName, position, linearVelocity]) ship.addComponent(component) component = ships.ThrustComponent() ship.addComponent(component) component = ships.InfoComponent() component.health = health component.name = shipName ship.addComponent(component) elif msgID == protocol.PLAYER_MOVED_SHIP: log.debug("Player moved ship") accountID = myIterator.getUint16() shipID = myIterator.getUint16() print sandbox.components[shipID] universals.shipNode = sandbox.components[shipID][ships.BulletPhysicsComponent].nodePath elif msgID == protocol.LOGIN_ACCEPTED: log.info("Login accepted") entityID = myIterator.getUint8() universals.day = myIterator.getFloat32() elif msgID == protocol.LOGIN_DENIED: log.info("Login failed")
def begin(self): if self.cReader.dataAvailable(): datagram = NetDatagram() # catch the incoming data in this instance # Check the return value; if we were threaded, someone else could have # snagged this data before we did if self.cReader.getData(datagram): myIterator = PyDatagramIterator(datagram) msgID = myIterator.getUint8() #If not in our protocol range then we just reject if msgID < 0 or msgID > 200: return self.lastAck[datagram.getAddress()] = datetime.datetime.now() #TODO Switch to ip address and port #Order of these will need to be optimized later #We now pull out the rest of our headers remotePacketCount = myIterator.getUint8() ack = myIterator.getUint8() acks = myIterator.getUint16() hashID = myIterator.getUint16() if msgID == protocol.LOGIN: username = myIterator.getString() password = myIterator.getString() if username not in accountEntities: entity = sandbox.createEntity() component = AccountComponent() component.name = username component.passwordHash = password if not accountEntities: component.owner = True component.address = datagram.getAddress() entity.addComponent(component) accountEntities[username] = entity.id log.info("New player " + username + " logged in.") # self.activePlayers.append(component) self.activeConnections[component.address] = component ackDatagram = protocol.loginAccepted(entity.id) self.sendData(ackDatagram, datagram.getAddress()) #TODO: Send initial states? messenger.send("newPlayerShip", [component, entity]) else: component = sandbox.entities[accountEntities[username]].get_component(AccountComponent) if component.passwordHash != password: log.info("Player " + username + " has the wrong password.") else: component.connection = datagram.getConnection() log.info("Player " + username + " logged in.")
def listen_for_new_data(self): """ Listens for new data from active connections """ while True: if self.reader.data_available(): datagram = NetDatagram() if self.reader.get_data(datagram): connection = datagram.getConnection() session = self.session_manager.for_connection(connection) self.handler.handle_data( datagram, connection, session, ) time.sleep(0.01)
def check_for_message(self, taskdata): """ Called repeatedly to check if there's any new messages """ if self.cReader.dataAvailable(): dg = NetDatagram() if self.cReader.getData(dg): iterator = PyDatagramIterator(dg) connection = dg.getConnection() try: msg_id = iterator.getUint8() except AssertionError: self.notify.warning("[check_for_message] No message ID") return Task.cont # check if real msg_id if msg_id in self.mapping: self.mapping[msg_id](self, iterator) else: self.notify.warning( "[check_for_message] Invalid msg_id: {}".format( msg_id)) return Task.cont