def unpack(cls, msg, client, server, request_id):
        """Parse message and return an `OpMsg`.

        Takes the client message as bytes, the client and server socket objects,
        and the client request id.
        """
        payload_document = OrderedDict()
        flags, = _UNPACK_UINT(msg[:4])
        pos = 4
        if flags != 0 and flags != 2:
            raise ValueError('OP_MSG flag must be 0 or 2 not %r' % (flags,))

        while pos < len(msg):
            payload_type, = _UNPACK_BYTE(msg[pos:pos + 1])
            pos += 1
            payload_size, = _UNPACK_INT(msg[pos:pos + 4])
            if payload_type == 0:
                doc = bson.decode_all(msg[pos:pos + payload_size],
                                      CODEC_OPTIONS)[0]
                payload_document.update(doc)
                pos += payload_size
            elif payload_type == 1:
                section_size, = _UNPACK_INT(msg[pos:pos + 4])
                pos += 4
                identifier, pos = _get_c_string(msg, pos)
                # Section starts w/ 4-byte size prefix, identifier ends w/ nil.
                documents_len = section_size - len(identifier) - 1 - 4
                documents = bson.decode_all(msg[pos:pos + documents_len],
                                            CODEC_OPTIONS)
                payload_document[identifier] = documents
                pos += documents_len

        database = payload_document['$db']
        return OpMsg(payload_document, namespace=database, flags=flags,
                     _client=client, request_id=request_id,
                     _server=server)
def unpack ( cls , msg , client , server , request_id ) : payload_document = OrderedDict ( ) flags , = _UNPACK_UINT ( msg [ : 4 ] ) pos = 4 if flags != 0 and flags != 2 : raise ValueError ( 'OP_MSG flag must be 0 or 2 not %r' % ( flags , ) ) while pos < len ( msg ) : payload_type , = _UNPACK_BYTE ( msg [ pos : pos + 1 ] ) pos += 1 payload_size , = _UNPACK_INT ( msg [ pos : pos + 4 ] ) if payload_type == 0 : doc = bson . decode_all ( msg [ pos : pos + payload_size ] , CODEC_OPTIONS ) [ 0 ] payload_document . update ( doc ) pos += payload_size elif payload_type == 1 : section_size , = _UNPACK_INT ( msg [ pos : pos + 4 ] ) pos += 4 identifier , pos = _get_c_string ( msg , pos ) documents_len = section_size - len ( identifier ) - 1 - 4 documents = bson . decode_all ( msg [ pos : pos + documents_len ] , CODEC_OPTIONS ) payload_document [ identifier ] = documents pos += documents_len database = payload_document [ '$db' ] return OpMsg ( payload_document , namespace = database , flags = flags , _client = client , request_id = request_id , _server = server )
Parse message and return an `OpMsg`.

        Takes the client message as bytes, the client and server socket objects,
        and the client request id.