def check_pong(data, host_port): # Make sure the host and port match up assert host_port == HOST_PORT # Check if this is a valid message assert len(data) >= 16 header = struct.unpack("!IHHQ", data[:16]) assert header[0] == _server.MAGIC assert check_crc(data) assert header[2] == _server.TYPE_ACK ping_id2, _, flags = struct.unpack("!HHI", data[16:]) assert ping_id2 == ping_id assert not (flags & _server.FLAG_ACK_BAD_KEY)
def check_pong(data, host_port): # Make sure the host and port match up eq_(host_port, self.HOST_PORT) # Check if this is a valid message assert len(data) >= 16 header = struct.unpack('!IHHQ', data[:16]) eq_(header[0], server.MAGIC) ok_(check_crc(data)) eq_(header[2], server.TYPE_ACK) ping_id2, _, flags = struct.unpack('!HHI', data[16:]) eq_(ping_id2, ping_id) ok_(not (flags & server.FLAG_ACK_BAD_KEY))
def __handle(self, data, address): if len(data) < 16: return header = struct.unpack('!IHHQ', data[:16]) if header[0] != MAGIC: return if not check_crc(data): return (host, port) = address with self.app.app_context(): if header[2] == TYPE_FIX: self.fix_received(host, header[3], data[16:]) elif header[2] == TYPE_PING: self.ping_received(host, port, header[3], data[16:]) elif header[2] == TYPE_TRAFFIC_REQUEST: self.traffic_request_received(host, port, header[3], data[16:]) elif header[2] == TYPE_USER_NAME_REQUEST: self.username_request_received(host, port, header[3], data[16:])
user_id, 0, club_id, len(name), 0, 0, 0, 0, 0) response += name response = set_crc(response) self.socket.sendto(response, (host, port)) log("%s USER_NAME_REQUEST %s -> %s" % (host, unicode(pilot).encode('utf8', 'ignore'), unicode(user).encode('utf8', 'ignore'))) def handle(self, data, (host, port)): if len(data) < 16: return header = struct.unpack('!IHHQ', data[:16]) if header[0] != MAGIC: return if not check_crc(data): return with self.app.app_context(): if header[2] == TYPE_FIX: self.fixReceived(host, header[3], data[16:]) elif header[2] == TYPE_PING: self.pingReceived(host, port, header[3], data[16:]) elif header[2] == TYPE_TRAFFIC_REQUEST: self.trafficRequestReceived(host, port, header[3], data[16:]) elif header[2] == TYPE_USER_NAME_REQUEST: self.userNameRequestReceived(host, port, header[3], data[16:]) def serve_forever(self): if not self.app: raise RuntimeError('application not registered on server instance')
USER_FLAG_NOT_FOUND, 0, 0, 0, 0, 0, 0, 0) response = set_crc(response) self.transport.write(response, (host, port)) return name = user.name[:64].encode('utf8', 'ignore') club_id = user.club_id or 0 response = struct.pack('!IHHQIIIBBBBII', MAGIC, 0, TYPE_USER_NAME_RESPONSE, 0, user_id, 0, club_id, len(name), 0, 0, 0, 0, 0) response += name response = set_crc(response) self.transport.write(response, (host, port)) def datagramReceived(self, data, (host, port)): if len(data) < 16: return header = struct.unpack('!IHHQ', data[:16]) if header[0] != MAGIC: return if not check_crc(data): return if header[2] == TYPE_FIX: self.fixReceived(host, header[3], data[16:]) elif header[2] == TYPE_PING: self.pingReceived(host, port, header[3], data[16:]) elif header[2] == TYPE_TRAFFIC_REQUEST: self.trafficRequestReceived(host, port, header[3], data[16:]) elif header[2] == TYPE_USER_NAME_REQUEST: self.userNameRequestReceived(host, port, header[3], data[16:])