Sans-I/O implementation of a WebSocket connection. Args: side: :attr:`~Side.CLIENT` or :attr:`~Side.SERVER`. state: Initial state of the WebSocket connection. max_size: Maximum size of incoming messages in bytes. :obj:`None` disables the limit. You may p
| 69 | |
| 70 | |
| 71 | class Protocol: |
| 72 | """ |
| 73 | Sans-I/O implementation of a WebSocket connection. |
| 74 | |
| 75 | Args: |
| 76 | side: :attr:`~Side.CLIENT` or :attr:`~Side.SERVER`. |
| 77 | state: Initial state of the WebSocket connection. |
| 78 | max_size: Maximum size of incoming messages in bytes. |
| 79 | :obj:`None` disables the limit. You may pass a ``(max_message_size, |
| 80 | max_fragment_size)`` tuple to set different limits for messages and |
| 81 | fragments when you expect long messages sent in short fragments. |
| 82 | logger: Logger for this connection; depending on ``side``, |
| 83 | defaults to ``logging.getLogger("websockets.client")`` |
| 84 | or ``logging.getLogger("websockets.server")``; |
| 85 | see the :doc:`logging guide <../../topics/logging>` for details. |
| 86 | |
| 87 | """ |
| 88 | |
| 89 | def __init__( |
| 90 | self, |
| 91 | side: Side, |
| 92 | *, |
| 93 | state: State = OPEN, |
| 94 | max_size: tuple[int | None, int | None] | int | None = 2**20, |
| 95 | logger: LoggerLike | None = None, |
| 96 | ) -> None: |
| 97 | # Unique identifier. For logs. |
| 98 | self.id: uuid.UUID = uuid.uuid4() |
| 99 | """Unique identifier of the connection. Useful in logs.""" |
| 100 | |
| 101 | # Logger or LoggerAdapter for this connection. |
| 102 | if logger is None: |
| 103 | logger = logging.getLogger(f"websockets.{side.name.lower()}") |
| 104 | self.logger: LoggerLike = logger |
| 105 | """Logger for this connection.""" |
| 106 | |
| 107 | # Track if DEBUG is enabled. Shortcut logging calls if it isn't. |
| 108 | self.debug = logger.isEnabledFor(logging.DEBUG) |
| 109 | |
| 110 | # Connection side. CLIENT or SERVER. |
| 111 | self.side = side |
| 112 | |
| 113 | # Connection state. Initially OPEN because subclasses handle CONNECTING. |
| 114 | self.state = state |
| 115 | |
| 116 | # Maximum size of incoming messages in bytes. |
| 117 | if isinstance(max_size, int) or max_size is None: |
| 118 | self.max_message_size, self.max_fragment_size = max_size, None |
| 119 | else: |
| 120 | self.max_message_size, self.max_fragment_size = max_size |
| 121 | |
| 122 | # Current size of incoming message in bytes. Only set while reading a |
| 123 | # fragmented message i.e. a data frames with the FIN bit not set. |
| 124 | self.current_size: int | None = None |
| 125 | |
| 126 | # True while sending a fragmented message i.e. a data frames with the |
| 127 | # FIN bit not set. |
| 128 | self.expect_continuation_frame = False |
no outgoing calls
searching dependent graphs…