def listen(self): while True: try: msg, b, c, d = self.playerSocket.recvmsg(4096) msg = msg.decode('UTF-8') print(msg) request = ClientRequest(msg, self.playerSocket, self.playerId) if request.command.isLogin(): self.playerSocket.sendall(bytes(badCommand('Already logged in.'), encoding)) else: self.requestQueue.put(request) time.sleep(0) except RequestParsingException: self.playerSocket.sendall(bytes(badCommand('Could not understand request'), encoding)) except (KeyboardInterrupt, SystemExit): self.playerSocket.shutdown(socket.SHUT_RDWR) self.playerSocket.close() break except (socket.error, UnicodeDecodeError): logout = ClientRequest('logout', None, self.playerId) self.requestQueue.put(logout) break except Exception: logout = ClientRequest('logout', None, self.playerId) self.requestQueue.put(logout) break
def listen(self): while True: try: msg, b, c, d = self.playerSocket.recvmsg(4096) msg = msg.decode('UTF-8') print(msg) request = ClientRequest(msg, self.playerSocket, self.playerId) if request.command.isLogin(): self.playerSocket.sendall( bytes(badCommand('Already logged in.'), encoding)) else: self.requestQueue.put(request) time.sleep(0) except RequestParsingException: self.playerSocket.sendall( bytes(badCommand('Could not understand request'), encoding)) except (KeyboardInterrupt, SystemExit): self.playerSocket.shutdown(socket.SHUT_RDWR) self.playerSocket.close() break except (socket.error, UnicodeDecodeError): logout = ClientRequest('logout', None, self.playerId) self.requestQueue.put(logout) break except Exception: logout = ClientRequest('logout', None, self.playerId) self.requestQueue.put(logout) break
def loginResponder(clientSocket): clientSocket.sendall(bytes(protocol.playerMessage('', 'Welcome. Log in to play the game.'), settings.encoding)) request, b, c, d = clientSocket.recvmsg(4096) request = request.decode('UTF-8') try: clientRequest = protocol.ClientRequest(request, clientSocket, request.split()[1]) if not clientRequest.command.isLogin(): raise protocol.RequestParsingException else: Game.instance().considerPlayerRequest(clientRequest) except (protocol.RequestParsingException, IndexError): clientSocket.sendall(bytes(protocol.badCommand("Player must log in before playing. Goodbye."), settings.encoding)) clientSocket.close()
def loginResponder(clientSocket): clientSocket.sendall( bytes(protocol.playerMessage('', 'Welcome. Log in to play the game.'), settings.encoding)) request, b, c, d = clientSocket.recvmsg(4096) request = request.decode('UTF-8') try: clientRequest = protocol.ClientRequest(request, clientSocket, request.split()[1]) if not clientRequest.command.isLogin(): raise protocol.RequestParsingException else: Game.instance().considerPlayerRequest(clientRequest) except (protocol.RequestParsingException, IndexError): clientSocket.sendall( bytes( protocol.badCommand( "Player must log in before playing. Goodbye."), settings.encoding)) clientSocket.close()