Esempio n. 1
0
    def connect(self):

        """Negotiate for connection to remote host."""

        self.logger.info("Connecting to %s:%s." % self.addr)

        try:
            self.socket = create_connection(self.addr, 5)
            self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        except IOError as e:
            self.logger.critical("Connection failed: %s" % e)
            sys.exit(1)

        net.send(self.socket, 'icanhazconnectplz?', self.username)

        servername, response = net.receive(self.socket)
        if servername == None:
            self.logger.critical("Connection failed: Unknown reason")

        if 'accepted' not in response[0]:
            self.logger.critical("Bad server response")
            self.logger.debug("Data causing error:\n%s" % response)
            sys.exit(1)

        if response[0]['accepted'] == False:
            self.logger.critical("Connection failed: %s" %
                                 response[0]['reason'])
        else:
            self.logger.info("Connected")
Esempio n. 2
0
    def get_client_events(self, socket):

        """Parse all events from a single client, return a dict with
        username: events"""

        try:
            username, messages = net.receive(socket)
            if username == None:
                return
        except IOError as e:
            if e.errno == errno.ECONNRESET:
                self.disconnect_client(socket, "Connection lost")
            else:
                self.logger.error(e.strerror)
            return None, None

        events = []
        for message in filter(lambda m:m['label'] == 'events', messages):
            events += message['events']

        #self.logger.debug("Events: %s" % events)

        event_list = []
        for event in events:
            #self.logger.debug("Event: %s" % event)
            if 'type' not in event:
                self.logger.error("Malformed event")
                self.logger.debug("Event causing error:\n%s" % event)
                raise Exception("Malformed event")
                return username, None

            elif event['type'] in (KEYDOWN, KEYUP):
                event = Event(event['type'], { 'key': event['key'] })

            elif event['type'] in (MOUSEBUTTONDOWN, MOUSEBUTTONUP):
                event = Event(event['type'], { 'button': event['button'],
                                               'pos': event['pos'] })

            elif event['type'] == MOUSEMOTION:
                event = Event(event['type'], { 'pos': event['pos'] })

            event_list.append(event)

        return username, event_list
Esempio n. 3
0
    def get_state(self):

        """Parse server state messages and return a list of it"""

        state = []

        read_list = [self.socket]
        readable, writable, in_error = select.select(read_list, [], [], 0)
        try: 
            for socket in readable:
                servername, state = net.receive(self.socket)
        except ValueError:
            self.logger.error("gameengine.get_state: ValueError")
            state = None

        if state == None:
            return []
        else:
            return state
Esempio n. 4
0
    def on_connect(self):

        """Handle client(s) awaiting connection."""

        client, address = self.server_socket.accept()

        try:
            username, messages = net.receive(client)
        except IOError as e:
            if e.errno == errno.ECONNRESET:
                self.disconnect_client(client, "Connection lost")
            else:
                self.logger.error(e.strerror)
            return

        if username in self.clients.keys():
            # The username is already taken, refuse connection
            self.logger.info("Refused connection from %s:%s, "
                    "username '%s' unavailable" %
                    (address[0], address[1], username))

            net.send(client, { 'accepted': False,
                               'reason': 'Username unavailable' })
            
            client.close()

            return False

        else:
            # The username is free, accept the client
            self.logger.info("Connection from %s:%d, username '%s' accepted" %
                    (address[0], address[1], username))

            net.send(client, { 'accepted': True })
            self.clients[username] = client

            # For now, create a vehicle:
            gameengine.add_entity(Vehicle(username, (400, 300),
            120,('bigmotor','smallmotor')))

            return True