예제 #1
0
    def run(self):
        forcePrint('start handling client', self.address)

        try:
            while self.__isRunning:
                data = self.connection.recv(1024)

                if not data:
                    self.closeConnection(1)
                    break;
                
                data = data.decode('utf-8')
                buffer = data.split('\n')
                buffer = buffer[:-1]

                print('received data', data)

                while len(buffer) > 0:
                    command = buffer.pop(0)
                    print('execute command:', command, ': from', self.address[0])
                    self.respondClient(command)
##                    threading.Thread(target=self.respondClient,args=[command]).start()
    
        except socket.error as msg:
            self.closeConnection(msg)
예제 #2
0
    def run(self):
        print('start handling gameClientConnection', self.address)

        while True:
            data = self.connection.recv(1024)
            data = data.decode('utf-8')
            print('received data from', self.address, ':', data)
            threading.Thread(target=self.respondClient, args=[data]).start()
예제 #3
0
    def run(self):
        print('start main game calculation thread')
        self.__updateTimeStamp()

        while True:
            self.deltaTime = self.__calculateDeltaTime()

            if self.gameData.gameState is GAMESTATE.READY:
                continue

            self.__updateGame(self.deltaTime)
            self.__updateTimeStamp()
예제 #4
0
    def run(self):
        print('start main game calculation thread')
        self.__updateTimeStamp()

        while True:
            self.deltaTime = self.__calculateDeltaTime()

            self.__updateGame(self.deltaTime)
            self.__updateTimeStamp()
            if currentTime() - self.lastSent >= BROADCAST_DELAY:
                self.__broadcastGameData()
                self.lastSent = currentTime()
예제 #5
0
    def __mainLoop(self):
        while True:
            try:
                self.__clearDeadClient()
                connectedNum = len(self.connectionList)
                
                if connectedNum < CLIENT_LIMIT:
                    self.__waitAndAcceptNewClient()

                delay(1)
                
            except ConnectionResetError as msg:
                print('connection reset error', msg)
예제 #6
0
    def __executeCommand(self, strDataIn):
        try:  #to call method according to header
            dataIn = self.__convertToTuple(strDataIn)  #convert to tuple
            header = self.__extractHeader(dataIn)
            values = self.__extractParameters(dataIn)

            actionMethod = self.__getCorrespondingMethod(header)
            output = self.__performAction(actionMethod, values)

            return output

        except Exception as e:
            print('error found for request:', strDataIn)
            print('with exception:', e)

            return None
예제 #7
0
    def __clearDeadClient(self):
        print("clear client")
        if len(self.connectionList) == 0:
            return

        toRemove = []
        
        for client in self.connectionList:
            exc = client.getException()
            
            if exc is not None:
                self.connectionList.remove(client)
                client.exit()
                forcePrint("remove disconnected client")

        for client in toRemove:
            self.connectionList.remove(client)
예제 #8
0
    def __broadcastGameData(self):

        gameString = str(self.gameData.gameState) + '|'

        for playerData in self.gameData.playerDataList:

            if playerData == self.gameData.playerDataList[0]:
                gameString += playerData.getPlayerString()
                #state, pos, velo, x, y, z, w
            else:
                gameString += '|' + playerData.getPlayerString()

        gameString += '\n'

        for connection in self.connectionList:
            if not connection.isClient:
                continue
            if connection.exception is not None:
                continue
            connection.send(gameString)

        print(gameString)
예제 #9
0
    def __mainLoop(self):
        while True:
            conn, addr = self.socket.accept()
            print("new client connected from : " + str(addr))

            # todo : check connection limit properly
            if len(self.connectionList) >= CLIENT_LIMIT:
                print("reject connection from : " + str(addr))
                conn.close()
                continue

            newId = len(self.connectionList)
            print('client id :', newId)
            newCommuThread = CommunicationThread(conn, addr, self.gameData,
                                                 newId)
            newCommuThread.start()

            self.connectionList.append(newCommuThread)
예제 #10
0
 def send(self, s):
     print('send result', s)
     try:
         self.connection.send(str.encode(s))
     except socket.error as msg:
         self.closeConnection(msg)
예제 #11
0
    def startConnection(self, ipAddr):
        self.socket.bind((ipAddr, PORT))
        self.socket.listen(CLIENT_LIMIT)

        print("binded at : " + str(ipAddr) + " : " + str(PORT))
        self.__mainLoop()