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
Exemple #2
0
 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()
Exemple #4
0
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()