class Network(): def __init__(self, _base): # Game base self.game = _base # Login state self.LOGGED = False ### SETUP NETWORKING ### ## TCP Connection ## self.tcpConnection = None ## TCP NETWORKING ## # TCP Manager self.tcpManager = QueuedConnectionManager() # TCP Listener self.tcpListener = QueuedConnectionListener(self.tcpManager, 0) # TCP Reader self.tcpReader = QueuedConnectionReader(self.tcpManager, 0) # TCP Writer self.tcpWriter = ConnectionWriter(self.tcpManager, 0) ## UDP NETWORKING ## # UDP Manager self.udpManager = QueuedConnectionManager() # UDP Reader self.udpReader = QueuedConnectionReader(self.udpManager, 0) # UDP Writer self.udpWriter = ConnectionWriter(self.udpManager, 0) ### SETUP NETWORKING TASKS ### # TCP ReaderTask taskMgr.add(self.tcpReaderTask, "ClientTCPReaderTask", -40) # UDP ReaderTask taskMgr.add(self.udpReaderTask, "ClientUDPReaderTask", -39) #----------------------------------------------------------------------# # NETWORK TASKS TCP MAIN #----------------------------------------------------------------------# def tcpReaderTask(self, task): """ Handle data from server. """ while 1: (datagram, data, opcode) = self.tcpNonBlockingRead(self.tcpReader) if opcode is MSG_NONE: # Do nothing break else: self.tcpHandleDatagram(data, opcode) return Task.cont def tcpNonBlockingRead(self, qcr): """ Return a datagram collection and type if data is available on the queued tcp connection reader. @param qcr: self.tcpReader """ if self.tcpReader.dataAvailable(): datagram = NetDatagram() if self.tcpReader.getData(datagram): data = PyDatagramIterator(datagram) opcode = data.getUint16() else: data = None opcode = MSG_NONE else: datagram = None data = None opcode = MSG_NONE return (datagram, data, opcode) def tcpHandleDatagram(self, data, opcode): """ Check for the handle assigned to the opcode. """ if opcode in self.HandlerDict.keys(): self.HandlerDict[opcode](opcode, data) else: logging.info("Unknown opcode received: %d", opcode) print "Unknown opcode: %d" % opcode print data return #----------------------------------------------------------------------# # NETWORK TASKS TCP END #----------------------------------------------------------------------# #----------------------------------------------------------------------# # NETWORK TASKS UDP MAIN #----------------------------------------------------------------------# def udpReaderTask(self, task): """ Handle any data from server on udp port. """ while 1: (datagram, data, opcode) = self.udpNonBlockingRead(self.udpReader) if opcode is MSG_NONE: # Do nothing break else: # Got some data handle it self.udpHandleDatagram(data, opcode) return Task.cont def udpNonBlockingRead(self, qcr): """ Return a datagram collection and type if data is available on the queued connection udpReader """ if self.udpReader.dataAvailable(): datagram = NetDatagram() if self.udpReader.getData(datagram): data = PyDatagramIterator(datagram) opcode = data.getUint16() else: data = None opcode = MSG_NONE else: datagram = None data = None opcode = MSG_NONE # Return the datagram to keep a handle on the data return (datagram, data, opcode) def udpHandleDatagram(self, data, opcode): """ Check for the handler assigned to the opcode. """ if opcode in self.HandlerDict.keys(): self.HandlerDict[opcode](opcode, data) else: logging.info("Unknown opcode received: %d", opcode) print "Unknown opcode received: %d" % opcode print data return #----------------------------------------------------------------------# # NETWORK TASKS UDP END #----------------------------------------------------------------------# def makeHandlers(self): ### SETUP HANDLER INSTANCE ### self.handler = Handler(self) self.gameHandler = GameHandler(self) ### SETUP HANDLERS DICT ### self.HandlerDict = { SMSG_AUTH_RESPONSE: self.handler.handleAuth_Response, SMSG_DISCONNECT_ACK: self.handler.handleDisconnect_ACK } def makeConnection(self, user, passw): """ Handle the login state for the client when the player enters the user/pass info """ if self.LOGGED == True: pass else: try: print "Connecting..." self.tcpConnection = self.tcpManager.openTCPClientConnection( IP, tcpPORT, TIMEOUT) self.tcpReader.addConnection(self.tcpConnection) # Start the Handlers self.makeHandlers() self.handler.auth_REQ(user, passw) self.LOGGED = True except: print "Connection to server failed, Server offline..." self.game.gui.login_gui.popError3() self.LOGGED = False
class Network(): def __init__(self, _base): # Game base self.game = _base # Login state self.LOGGED = False ### SETUP NETWORKING ### ## TCP Connection ## self.tcpConnection = None ## TCP NETWORKING ## # TCP Manager self.tcpManager = QueuedConnectionManager() # TCP Listener self.tcpListener = QueuedConnectionListener(self.tcpManager, 0) # TCP Reader self.tcpReader = QueuedConnectionReader(self.tcpManager, 0) # TCP Writer self.tcpWriter = ConnectionWriter(self.tcpManager, 0) ## UDP NETWORKING ## # UDP Manager self.udpManager = QueuedConnectionManager() # UDP Reader self.udpReader = QueuedConnectionReader(self.udpManager, 0) # UDP Writer self.udpWriter = ConnectionWriter(self.udpManager, 0) ### SETUP NETWORKING TASKS ### # TCP ReaderTask taskMgr.add(self.tcpReaderTask, "ClientTCPReaderTask", -40) # UDP ReaderTask taskMgr.add(self.udpReaderTask, "ClientUDPReaderTask", -39) #----------------------------------------------------------------------# # NETWORK TASKS TCP MAIN #----------------------------------------------------------------------# def tcpReaderTask(self, task): """ Handle data from server. """ while 1: (datagram, data, opcode) = self.tcpNonBlockingRead(self.tcpReader) if opcode is MSG_NONE: # Do nothing break else: self.tcpHandleDatagram(data, opcode) return Task.cont def tcpNonBlockingRead(self, qcr): """ Return a datagram collection and type if data is available on the queued tcp connection reader. @param qcr: self.tcpReader """ if self.tcpReader.dataAvailable(): datagram = NetDatagram() if self.tcpReader.getData(datagram): data = PyDatagramIterator(datagram) opcode = data.getUint16() else: data = None opcode = MSG_NONE else: datagram = None data = None opcode = MSG_NONE return (datagram, data, opcode) def tcpHandleDatagram(self, data, opcode): """ Check for the handle assigned to the opcode. """ if opcode in self.HandlerDict.keys(): self.HandlerDict[opcode](opcode, data) else: logging.info("Unknown opcode received: %d", opcode) print "Unknown opcode: %d" % opcode print data return #----------------------------------------------------------------------# # NETWORK TASKS TCP END #----------------------------------------------------------------------# #----------------------------------------------------------------------# # NETWORK TASKS UDP MAIN #----------------------------------------------------------------------# def udpReaderTask(self, task): """ Handle any data from server on udp port. """ while 1: (datagram, data, opcode) = self.udpNonBlockingRead(self.udpReader) if opcode is MSG_NONE: # Do nothing break else: # Got some data handle it self.udpHandleDatagram(data, opcode) return Task.cont def udpNonBlockingRead(self, qcr): """ Return a datagram collection and type if data is available on the queued connection udpReader """ if self.udpReader.dataAvailable(): datagram = NetDatagram() if self.udpReader.getData(datagram): data = PyDatagramIterator(datagram) opcode = data.getUint16() else: data = None opcode = MSG_NONE else: datagram = None data = None opcode = MSG_NONE # Return the datagram to keep a handle on the data return (datagram, data, opcode) def udpHandleDatagram(self, data, opcode): """ Check for the handler assigned to the opcode. """ if opcode in self.HandlerDict.keys(): self.HandlerDict[opcode](opcode, data) else: logging.info("Unknown opcode received: %d", opcode) print "Unknown opcode received: %d" % opcode print data return #----------------------------------------------------------------------# # NETWORK TASKS UDP END #----------------------------------------------------------------------# def makeHandlers(self): ### SETUP HANDLER INSTANCE ### self.handler = Handler(self) self.gameHandler = GameHandler(self) ### SETUP HANDLERS DICT ### self.HandlerDict = { SMSG_AUTH_RESPONSE : self.handler.handleAuth_Response, SMSG_DISCONNECT_ACK : self.handler.handleDisconnect_ACK } def makeConnection(self, user, passw): """ Handle the login state for the client when the player enters the user/pass info """ if self.LOGGED == True: pass else: try: print "Connecting..." self.tcpConnection = self.tcpManager.openTCPClientConnection(IP, tcpPORT, TIMEOUT) self.tcpReader.addConnection(self.tcpConnection) # Start the Handlers self.makeHandlers() self.handler.auth_REQ(user, passw) self.LOGGED = True except: print "Connection to server failed, Server offline..." self.game.gui.login_gui.popError3() self.LOGGED = False