def run(self): serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) hostname = socket.gethostname() #hack to get my host name try: tokenized = hostname.split('.') hostname = '{}.{}.{}.{}'.format(tokenized[3], tokenized[2], tokenized[1], tokenized[0]) except: hostname = 'localhost' print socket.gethostname() print hostname serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind((hostname, 8888)) serversocket.listen(5) while True: clientsocket = serversocket.accept()[0] clientsocket.setblocking(False) clientsocket.send('\n\rWelcome! Enter your name:\n\r') playerInput = '' while playerInput == '': try: playerInput = clientsocket.recv(1024) except: playerInput = '' if len(playerInput) > 0: playerInput = playerInput.strip() if ActorEngine.playerExists(playerInput) == True: player = ActorEngine.loadPlayer(playerInput) connection = Connection(clientsocket, player) loginEvent = Event() player.attributes['connection'] = connection loginEvent.attributes['signature'] = 'player_login' loginEvent.attributes['data']['player'] = player ActorEngine.receiveEvent(loginEvent) ConnectionEngine.receiveEvent(loginEvent) else: clientsocket.send('\n\rPlayer not found.\n\rEnter your name:') playerInput = '' else: playerInput = '' sleep(2)
def run(self): while True: ConnectionEngine.lock('openConnectionsSemaphore') for connection in ConnectionEngine.attribute('connectionList'): playerInput = connection.pollInput() parsedInput = playerInput.lower().strip() player = connection.attributes['player'] if len(playerInput) > 0: self.processInput(player, playerInput) ConnectionEngine.release('openConnectionsSemaphore') sleep(0.005)
def run(self): while True: ConnectionEngine.lock("openConnectionsSemaphore") for connection in ConnectionEngine.attribute("connectionList"): try: playerInput = connection.attributes["socket"].recv(1024) playerInput = playerInput.strip() if len(playerInput) > 0: connection.attributes["inputBuffer"].append(playerInput) except: pass # socket unavailable for now, we'll get it next time ConnectionEngine.release("openConnectionsSemaphore") sleep(0.005)
def run(self): while True: ConnectionEngine.lock('openConnectionsSemaphore') for connection in ConnectionEngine.attribute('connectionList'): try: for line in connection.attributes['outputBuffer']: connection.attributes['socket'].send(line) connection.attributes['outputBuffer'] = [] except: #socket unavailable for now, we'll get it next time pass ConnectionEngine.release('openConnectionsSemaphore') sleep(0.005)
def run(self): while True: ConnectionEngine.lock('openConnectionsSemaphore') for connection in ConnectionEngine.attribute('connectionList'): try: playerInput = connection.attributes['socket'].recv(1024) playerInput = playerInput.strip() if len(playerInput) > 0: connection.attributes['inputBuffer'].append( playerInput) except: pass #socket unavailable for now, we'll get it next time ConnectionEngine.release('openConnectionsSemaphore') sleep(0.005)
def removeClosedConnections(self): ConnectionEngine.lock('closedConnectionSemaphore') for connection in ConnectionEngine.attribute('closedConnections'): ConnectionEngine.attribute('connectionList').remove(connection) connection.attributes['socket'].close() player = connection.attributes['player'] playerName = player.attributes['name'] logoutNotificationEvent = Event() logoutNotificationEvent.attributes['signature'] = 'broadcast_to_all_players' logoutNotificationEvent.attributes['data']['message'] = '{} logged off.'.format(playerName) ActorEngine.receiveEvent(logoutNotificationEvent) ConnectionEngine.setAttribute('closedConnections', []) ConnectionEngine.release('closedConnectionSemaphore')
def addNewConnections(self): ConnectionEngine.lock('newConnectionSemaphore') for connection in ConnectionEngine.attribute('newConnections'): player = connection.attributes['player'] loginEvent = Event() loginEvent.attributes['signature'] = 'player_login' loginEvent.attributes['data']['player'] = player RoomEngine.receiveEvent(loginEvent) ConnectionEngine.attribute('connectionList').append(connection) playerName = player.attributes['name'] loginNotificationEvent = Event() loginNotificationEvent.attributes['signature'] = 'broadcast_to_all_players' loginNotificationEvent.attributes['data']['message'] = '{} just logged in.'.format(playerName) ActorEngine.receiveEvent(loginNotificationEvent) ConnectionEngine.setAttribute('newConnections', []) ConnectionEngine.release('newConnectionSemaphore')
def run(self): while True: ConnectionEngine.lock('openConnectionsSemaphore') ConnectionEngine.lock('openConnectionsSemaphore') ConnectionEngine.lock('openConnectionsSemaphore') self.removeClosedConnections() self.addNewConnections() ConnectionEngine.release('openConnectionsSemaphore') ConnectionEngine.release('openConnectionsSemaphore') ConnectionEngine.release('openConnectionsSemaphore') sleep(0.5)