def sendRequest(self, *command): """ Send a request to the Frostbite server. """ if command is None: return None if self._serverSocket is None: self.console.info("sendRequest: reconnecting...") self._connect() self._auth() if len(command) == 1 and type(command[0]) == tuple: words = command[0] else: words = command request = protocol.EncodeClientRequest(words) self.printPacket(protocol.DecodePacket(request)) try: self._serverSocket.sendall(request) [response, self._receiveBuffer ] = protocol.receivePacket(self._serverSocket, self._receiveBuffer) except socket.error, detail: raise FrostbiteNetworkException(detail)
def readEvent(self): """ Wait event from the server. """ packet = None timeout_counter = 0 while packet is None: try: if self._serverSocket is None: self.console.info("readEvent: reconnecting...") self._connect() self._auth() self.subscribeToEvents() [tmppacket, self._receiveBuffer] = protocol.receivePacket(self._serverSocket, self._receiveBuffer) [isFromServer, isResponse, sequence, words] = protocol.DecodePacket(tmppacket) if isFromServer and not isResponse: packet = tmppacket else: self.console.verbose2('received a packet which is not an event: %s' % [isFromServer, isResponse, sequence, words,]) except socket.timeout: timeout_counter += 1 self.console.verbose2('timeout %s' % timeout_counter) if timeout_counter >= 5: self.console.verbose2('checking connection...') request = protocol.EncodeClientRequest(['eventsEnabled','true']) self.printPacket(protocol.DecodePacket(request)) self._serverSocket.sendall(request) timeout_counter = 0 except socket.error, detail: raise FrostbiteNetworkException('readEvent: %r'% detail)
def readEvent(self): """ Wait event from the server. """ packet = None timeout_counter = 0 while packet is None: try: if self._serverSocket is None: self.console.info("readEvent: reconnecting...") self._connect() self._auth() self.subscribeToEvents() [tmppacket, self._receiveBuffer ] = protocol.receivePacket(self._serverSocket, self._receiveBuffer) [isFromServer, isResponse, sequence, words] = protocol.DecodePacket(tmppacket) if isFromServer and not isResponse: packet = tmppacket else: self.console.verbose2( 'received a packet which is not an event: %s' % [ isFromServer, isResponse, sequence, words, ]) except socket.timeout: timeout_counter += 1 self.console.verbose2('timeout %s' % timeout_counter) if timeout_counter >= 5: self.console.verbose2('checking connection...') request = protocol.EncodeClientRequest( ['eventsEnabled', 'true']) self.printPacket(protocol.DecodePacket(request)) self._serverSocket.sendall(request) timeout_counter = 0 except socket.error as detail: raise FrostbiteNetworkException('readEvent: %r' % detail) try: [isFromServer, isResponse, sequence, words] = protocol.DecodePacket(packet) self.printPacket(protocol.DecodePacket(packet)) except: raise FrostbiteException('readEvent: failed to decodePacket {%s}' % packet) # If this was a command from the server, we should respond to it # For now, we always respond with an "OK" if isResponse: self.console.debug( 'received an unexpected response packet from server, ignoring: %r' % packet) return self.readEvent() else: response = protocol.EncodePacket(True, True, sequence, ["OK"]) self.printPacket(protocol.DecodePacket(response)) try: self._serverSocket.sendall(response) except socket.error as detail: self.console.warning( "in readEvent while sending response OK to server : %s" % detail) return words