| 1 |
""" |
| 2 |
AMQP Header Class Definitions |
| 3 |
|
| 4 |
For encoding AMQP Header frames into binary AMQP stream data and decoding AMQP |
| 5 |
binary data into AMQP Header frames. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import struct |
| 10 |
import typing |
| 11 |
|
| 12 |
from pamqp import commands, constants, decode |
| 13 |
|
| 14 |
BasicProperties = commands.Basic.Properties | None |
| 15 |
|
| 16 |
|
| 17 |
class ProtocolHeader: |
| 18 |
"""Class that represents the AMQP Protocol Header""" |
| 19 |
|
| 20 |
name: typing.ClassVar[str] = 'ProtocolHeader' |
| 21 |
|
| 22 |
def __init__( |
| 23 |
self, |
| 24 |
major_version: int = constants.VERSION[0], |
| 25 |
minor_version: int = constants.VERSION[1], |
| 26 |
revision: int = constants.VERSION[2], |
| 27 |
) -> None: |
| 28 |
"""Construct a Protocol Header frame object for the specified AMQP |
| 29 |
version. |
| 30 |
|
| 31 |
:param major_version: The AMQP major version (``0``) |
| 32 |
:param minor_version: The AMQP major version (``9``) |
| 33 |
:param revision: The AMQP major version (``1``) |
| 34 |
|
| 35 |
""" |
| 36 |
self.major_version = major_version |
| 37 |
self.minor_version = minor_version |
| 38 |
self.revision = revision |
| 39 |
|
| 40 |
def marshal(self) -> bytes: |
| 41 |
"""Return the full AMQP wire protocol frame data representation of the |
| 42 |
ProtocolHeader frame. |
| 43 |
|
| 44 |
""" |
| 45 |
return constants.AMQP + struct.pack( |
| 46 |
'BBBB', 0, self.major_version, self.minor_version, self.revision |
| 47 |
) |
| 48 |
|
| 49 |
def unmarshal(self, data: bytes) -> int: |
| 50 |
"""Dynamically decode the frame data applying the values to the method |
| 51 |
object by iterating through the attributes in order and decoding them. |
| 52 |
|
| 53 |
:param data: The frame value to unpack |
| 54 |
:raises: ValueError |
| 55 |
|
| 56 |
""" |
| 57 |
try: |
| 58 |
(self.major_version, self.minor_version, self.revision) = ( |
| 59 |
struct.unpack('BBB', data[5:8]) |
| 60 |
) |
| 61 |
except struct.error as err: |
| 62 |
raise ValueError( |
| 63 |
f'Could not unpack protocol header from {data!r}' |
| 64 |
) from err |
| 65 |
return 8 |
| 66 |
|
| 67 |
|
| 68 |
class ContentHeader: |
| 69 |
"""Represent a content header frame |
| 70 |
|
| 71 |
A Content Header frame is received after a Basic.Deliver or Basic.GetOk |
| 72 |
frame and has the data and properties for the Content Body frames that |
| 73 |
follow. |
| 74 |
|
| 75 |
""" |
| 76 |
|
| 77 |
name: typing.ClassVar[str] = 'ContentHeader' |
| 78 |
|
| 79 |
def __init__( |
| 80 |
self, |
| 81 |
weight: int = 0, |
| 82 |
body_size: int = 0, |
| 83 |
properties: commands.Basic.Properties | None = None, |
| 84 |
) -> None: |
| 85 |
"""Initialize the Exchange.DeleteOk class |
| 86 |
|
| 87 |
Weight is unused and must be `0` |
| 88 |
|
| 89 |
:param weight: The unused content weight field |
| 90 |
:param body_size: The size in bytes of the message body |
| 91 |
:param properties: The message properties |
| 92 |
|
| 93 |
""" |
| 94 |
self.class_id = None |
| 95 |
self.weight = weight |
| 96 |
self.body_size = body_size |
| 97 |
self.properties = properties or commands.Basic.Properties() |
| 98 |
|
| 99 |
def marshal(self) -> bytes: |
| 100 |
"""Return the AMQP binary encoded value of the frame""" |
| 101 |
return ( |
| 102 |
struct.pack('>HxxQ', commands.Basic.frame_id, self.body_size) |
| 103 |
+ self.properties.marshal() |
| 104 |
) |
| 105 |
|
| 106 |
def unmarshal(self, data: bytes) -> None: |
| 107 |
"""Dynamically decode the frame data applying the values to the method |
| 108 |
object by iterating through the attributes in order and decoding them. |
| 109 |
|
| 110 |
:param data: The raw frame data to unmarshal |
| 111 |
|
| 112 |
""" |
| 113 |
self.class_id, self.weight, self.body_size = struct.unpack( |
| 114 |
'>HHQ', data[0:12] |
| 115 |
) |
| 116 |
offset, flags = self._get_flags(data[12:]) |
| 117 |
self.properties.unmarshal(flags, data[12 + offset :]) |
| 118 |
|
| 119 |
@staticmethod |
| 120 |
def _get_flags(data: bytes) -> tuple[int, int]: |
| 121 |
"""Decode the flags from the data returning the bytes consumed and |
| 122 |
flags. |
| 123 |
|
| 124 |
""" |
| 125 |
bytes_consumed, flags, flagword_index = 0, 0, 0 |
| 126 |
while True: |
| 127 |
consumed, partial_flags = decode.short_int(data) |
| 128 |
bytes_consumed += consumed |
| 129 |
flags |= partial_flags << (flagword_index * 16) |
| 130 |
if not partial_flags & 1: # pragma: nocover |
| 131 |
break |
| 132 |
flagword_index += 1 # pragma: nocover |
| 133 |
return bytes_consumed, flags |