Example #1
0
    def loop(self):
        def selectExcept(exceptsock):
            print "Exception with socket", exceptsock
            exceptsock.close()
            if exceptsock is self.sock:
                raise IOError
            else:
                self.handle_disconnecting_client(exceptsock)

        def selectRead(readsock):
            if readsock is self.sock:
                self.do_accept()
            else:
                self.handle_incoming_client_data(readsock)

        while True:
            message.doSelect([self.sock] + self.clientsockets.keys(), selectRead, selectExcept)
Example #2
0
    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('127.0.0.1', self.port))
        self.sock.setblocking(0)
        self.connected = True
        self.inLobby = True
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        def selectExcept(exceptsock):
            print "Exception with socket", exceptsock
            exceptsock.close()
            if exceptsock is self.sock:
                raise IOError
            else:
                self.handle_disconnecting_client(exceptsock)

        def selectRead(readsock):
            self.server_is_silent = False
            msgs = message.recvMessage(readsock)
            if not msgs:
                self.sock.close()
                self.connected = False
                print "Disconnected from server"
            else:
                for msg in msgs:
                    print "Incoming server message:", msg
                    msg.handleMessageForClient(self)

        while self.connected:
            while True:
                self.server_is_silent = True
                message.doSelect([self.sock], selectRead, selectExcept, 0.1)
                if self.server_is_silent:
                    break;
            if not self.inLobby:
                if self.gameState.activeTeamID == self.ownTeamID:
                    msg = self.ai.decide(self.gameState)
                    self.send_to_server(msg)
Example #3
0
    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect(('127.0.0.1', self.port))
        self.sock.setblocking(0)
        self.connected = True
        self.inLobby = True
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        def selectExcept(exceptsock):
            print "Exception with socket", exceptsock
            exceptsock.close()
            if exceptsock is self.sock:
                raise IOError
            else:
                self.handle_disconnecting_client(exceptsock)

        def selectRead(readsock):
            self.server_is_silent = False
            msgs = message.recvMessage(readsock)
            if not msgs:
                self.sock.close()
                self.connected = False
                print "Disconnected from server"
            else:
                for msg in msgs:
                    print "Incoming server message:", msg
                    msg.handleMessageForClient(self)

        while self.connected:
            while True:
                self.server_is_silent = True
                message.doSelect([self.sock], selectRead, selectExcept, 0.1)
                if self.server_is_silent:
                    break
            if not self.inLobby:
                if self.gameState.activeTeamID == self.ownTeamID:
                    msg = self.ai.decide(self.gameState)
                    self.send_to_server(msg)