def handle(self): # first send the info message self.authrand = authrand = os.urandom(4) self.write(msginfo(config.FBNAME, authrand)) while True: self.unpacker.feed(self.s.recv(BUFSIZ)) for opcode, data in self.unpacker: if opcode != OP_AUTH: self.error('First message was not AUTH.') raise BadClient() ident, rhash = readauth(data) self.authkey_check(ident, rhash) break while True: self.unpacker.feed(self.recv(BUFSIZ)) for opcode, data in self.unpacker: if opcode == OP_PUBLISH: self.do_publish(*readpublish(data)) elif opcode == OP_SUBSCRIBE: self.do_subscribe(*readsubscribe(data)) elif opcode == OP_UNSUBSCRIBE: self.do_unsubscribe(*readsubscribe(data)) else: self.error( "Unknown message type.", opcode=opcode, length=len(data), ) raise BadClient()
def messageReceived(self, opcode, data): if opcode == OP_ERROR: return self.onError(readerror(data)) elif opcode == OP_INFO: return self.onInfo(*readinfo(data)) elif opcode == OP_AUTH: return self.onAuth(*readauth(data)) elif opcode == OP_PUBLISH: return self.onPublish(*readpublish(data)) elif opcode == OP_SUBSCRIBE: return self.onSubscribe(*readsubscribe(data)) elif opcode == OP_UNSUBSCRIBE: return self.onUnsubscribe(*readunsubscribe(data)) # Can't recover from an unknown opcode, so drop connection self.protocolError('Unknown message opcode: {!r}'.format(opcode)) self.transport.loseConnection()
def test_readsubscribe(self): ident, chan = readsubscribe(b'\x05identchan') assert ident == 'ident' assert chan == 'chan'