def connectToServer(self): """ trys to connect to server with given arguments @return: returns connection status """ try: print("ssl: " + str(Config.__getSSL__())) if (Config.__getSSL__()): self.connection = ssl.wrap_socket(socket(), cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_SSLv3, ca_certs=self.serverCert) else: self.connection = socket() self.connection.connect(self.hostPort) return True except Exception as e: print 'Connection failed.' print e return False
def write(socket, data): """ Append trailing '@' to data and write this string to socket. @param socket socket where data string will be sent to @param data string to be sent to socket """ if socket is not None: try: if (Config.__getSSL__()): socket.write(data + '@') else: # plain socket socket.send(data + '@') except Exception: raise # re-rase Exception that it can be handled by Server or Client
def read(socket): """ Read from socket until the "EndOfTransmission" signal ('@') is received and then return the received string without trailing '@'. @param socket socket from which is read @return received data without trailing '@' """ data = '' while socket is not None: try: if (Config.__getSSL__()): data += socket.read() else: data += socket.recv(1024) # plain socket except Exception: raise # re-rase Exception that it can be handled by Server or Client if len(data) == 0: break if data[len(data)-1] == '@': data = data.rstrip('@') break return data
def run(self): self.running=True try: self.log.info('Starting server on port ' + str(Config.__getPort__()) + '...') self.bindsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.bindsocket.bind(('', Config.__getPort__())) self.bindsocket.listen(5) self.bindsocket.settimeout(0.1) while self.running: # initialize self.closeConnections() self.clientMap = {} self.clientIpCountMap = {} while self.running and not self.game.gameFinished: try: # prevent exception if no client wants to connect newsocket, fromaddr = self.bindsocket.accept() newsocket.settimeout(0.1) if (Config.__getSSL__()): client = ssl.wrap_socket(newsocket, server_side=True, certfile="ssl/server.crt", keyfile="ssl/server.key", ssl_version=ssl.PROTOCOL_SSLv3) else: client = newsocket self.clientMap[client] = fromaddr, None self.log.info('client ' + str(self.clientMap[client]) + ' has connected') except KeyboardInterrupt: self.log.info('\nCtrl-C was pressed, stopping server...') self.bindsocket.close() self.log.info('Server stopped.') sys.exit() except socket.timeout: pass except socket.error as e: # Spits out tons of exceptions self.log.info(str(e.errno) + str(e)) inputReady, outputReady, exceptionReady = select.select(self.clientMap.keys(), [], [], 0) for client in inputReady: # go through all clients which sent a request clientip = None #Overwritten by local variables?? TODO: check! -> see line 86 #try: request = read(client) #except Exception as e: # print e response = self.processClientCommunication(client, request) self.log.info('current clients: ' + str(self.clientMap)) self.log.info('current IP-Connections: ' + str(self.clientIpCountMap)) try: # prevent exceptions if client has disconnected write(client, response) except: self.log.info('client ' + str(self.clientMap[client]) + ' has gone') self.clientMap.pop(client) self.decreaseClientIpConnections(clientip) for client in exceptionReady and self.running: self.log.info('assumed shutdown() -> exception occurred with client: ', str(self.clientMap[client])) if self.clientMap.has_key(client): client.shutdown(socket.RDWR) client.close() self.clientMap.pop(client) self.decreaseClientIpConnections(clientip) time.sleep(0.01) finally: self.closeConnections() self.bindsocket.close()