|
from __future__ import annotations |
|
|
|
import contextlib |
|
import logging |
|
import random |
|
import socket |
|
import struct |
|
import threading |
|
import time |
|
import uuid |
|
from collections.abc import Iterable, Iterator, Mapping |
|
from types import TracebackType |
|
from typing import Any, Literal, overload |
|
|
|
from ..exceptions import ( |
|
ConcurrencyError, |
|
ConnectionClosed, |
|
ConnectionClosedOK, |
|
ProtocolError, |
|
) |
|
from ..frames import DATA_OPCODES, BytesLike, CloseCode, Frame, Opcode |
|
from ..http11 import Request, Response |
|
from ..protocol import CLOSED, OPEN, Event, Protocol, State |
|
from ..typing import Data, LoggerLike, Subprotocol |
|
from .messages import Assembler |
|
from .utils import Deadline |
|
|
|
|
|
__all__ = ["Connection"] |
|
|
|
|
|
class Connection: |
|
""" |
|
:mod:`threading` implementation of a WebSocket connection. |
|
|
|
:class:`Connection` provides APIs shared between WebSocket servers and |
|
clients. |
|
|
|
You shouldn't use it directly. Instead, use |
|
:class:`~websockets.sync.client.ClientConnection` or |
|
:class:`~websockets.sync.server.ServerConnection`. |
|
|
|
""" |
|
|
|
recv_bufsize = 65536 |
|
|
|
def __init__( |
|
self, |
|
socket: socket.socket, |
|
protocol: Protocol, |
|
*, |
|
ping_interval: float | None = 20, |
|
ping_timeout: float | None = 20, |
|
close_timeout: float | None = 10, |
|
max_queue: int | None | tuple[int | None, int | None] = 16, |
|
) -> None: |
|
self.socket = socket |
|
self.protocol = protocol |
|
self.ping_interval = ping_interval |
|
self.ping_timeout = ping_timeout |
|
self.close_timeout = close_timeout |
|
if isinstance(max_queue, int) or max_queue is None: |
|
max_queue = (max_queue, None) |
|
self.max_queue = max_queue |
|
|
|
|
|
self.protocol.logger = logging.LoggerAdapter( |
|
self.protocol.logger, |
|
{"websocket": self}, |
|
) |
|
|
|
|
|
self.id: uuid.UUID = self.protocol.id |
|
"""Unique identifier of the connection. Useful in logs.""" |
|
self.logger: LoggerLike = self.protocol.logger |
|
"""Logger for this connection.""" |
|
self.debug = self.protocol.debug |
|
|
|
|
|
self.request: Request | None = None |
|
"""Opening handshake request.""" |
|
self.response: Response | None = None |
|
"""Opening handshake response.""" |
|
|
|
|
|
self.protocol_mutex = threading.Lock() |
|
|
|
|
|
self.recv_flow_control = threading.Lock() |
|
|
|
|
|
self.recv_messages = Assembler( |
|
*self.max_queue, |
|
pause=self.recv_flow_control.acquire, |
|
resume=self.recv_flow_control.release, |
|
) |
|
|
|
|
|
self.close_deadline: Deadline | None = None |
|
|
|
|
|
self.send_in_progress = False |
|
|
|
|
|
self.pong_waiters: dict[bytes, tuple[threading.Event, float, bool]] = {} |
|
|
|
self.latency: float = 0 |
|
""" |
|
Latency of the connection, in seconds. |
|
|
|
Latency is defined as the round-trip time of the connection. It is |
|
measured by sending a Ping frame and waiting for a matching Pong frame. |
|
Before the first measurement, :attr:`latency` is ``0``. |
|
|
|
By default, websockets enables a :ref:`keepalive <keepalive>` mechanism |
|
that sends Ping frames automatically at regular intervals. You can also |
|
send Ping frames and measure latency with :meth:`ping`. |
|
""" |
|
|
|
|
|
self.keepalive_thread: threading.Thread | None = None |
|
|
|
|
|
|
|
self.recv_exc: BaseException | None = None |
|
|
|
|
|
|
|
|
|
self.recv_events_thread = threading.Thread( |
|
target=self.recv_events, |
|
daemon=True, |
|
) |
|
|
|
|
|
self.recv_events_thread.start() |
|
|
|
|
|
|
|
@property |
|
def local_address(self) -> Any: |
|
""" |
|
Local address of the connection. |
|
|
|
For IPv4 connections, this is a ``(host, port)`` tuple. |
|
|
|
The format of the address depends on the address family. |
|
See :meth:`~socket.socket.getsockname`. |
|
|
|
""" |
|
return self.socket.getsockname() |
|
|
|
@property |
|
def remote_address(self) -> Any: |
|
""" |
|
Remote address of the connection. |
|
|
|
For IPv4 connections, this is a ``(host, port)`` tuple. |
|
|
|
The format of the address depends on the address family. |
|
See :meth:`~socket.socket.getpeername`. |
|
|
|
""" |
|
return self.socket.getpeername() |
|
|
|
@property |
|
def state(self) -> State: |
|
""" |
|
State of the WebSocket connection, defined in :rfc:`6455`. |
|
|
|
This attribute is provided for completeness. Typical applications |
|
shouldn't check its value. Instead, they should call :meth:`~recv` or |
|
:meth:`send` and handle :exc:`~websockets.exceptions.ConnectionClosed` |
|
exceptions. |
|
|
|
""" |
|
return self.protocol.state |
|
|
|
@property |
|
def subprotocol(self) -> Subprotocol | None: |
|
""" |
|
Subprotocol negotiated during the opening handshake. |
|
|
|
:obj:`None` if no subprotocol was negotiated. |
|
|
|
""" |
|
return self.protocol.subprotocol |
|
|
|
@property |
|
def close_code(self) -> int | None: |
|
""" |
|
State of the WebSocket connection, defined in :rfc:`6455`. |
|
|
|
This attribute is provided for completeness. Typical applications |
|
shouldn't check its value. Instead, they should inspect attributes |
|
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions. |
|
|
|
""" |
|
return self.protocol.close_code |
|
|
|
@property |
|
def close_reason(self) -> str | None: |
|
""" |
|
State of the WebSocket connection, defined in :rfc:`6455`. |
|
|
|
This attribute is provided for completeness. Typical applications |
|
shouldn't check its value. Instead, they should inspect attributes |
|
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions. |
|
|
|
""" |
|
return self.protocol.close_reason |
|
|
|
|
|
|
|
def __enter__(self) -> Connection: |
|
return self |
|
|
|
def __exit__( |
|
self, |
|
exc_type: type[BaseException] | None, |
|
exc_value: BaseException | None, |
|
traceback: TracebackType | None, |
|
) -> None: |
|
if exc_type is None: |
|
self.close() |
|
else: |
|
self.close(CloseCode.INTERNAL_ERROR) |
|
|
|
def __iter__(self) -> Iterator[Data]: |
|
""" |
|
Iterate on incoming messages. |
|
|
|
The iterator calls :meth:`recv` and yields messages in an infinite loop. |
|
|
|
It exits when the connection is closed normally. It raises a |
|
:exc:`~websockets.exceptions.ConnectionClosedError` exception after a |
|
protocol error or a network failure. |
|
|
|
""" |
|
try: |
|
while True: |
|
yield self.recv() |
|
except ConnectionClosedOK: |
|
return |
|
|
|
|
|
|
|
|
|
@overload |
|
def recv(self, timeout: float | None, decode: Literal[True]) -> str: ... |
|
|
|
@overload |
|
def recv(self, timeout: float | None, decode: Literal[False]) -> bytes: ... |
|
|
|
@overload |
|
def recv(self, timeout: float | None = None, *, decode: Literal[True]) -> str: ... |
|
|
|
@overload |
|
def recv( |
|
self, timeout: float | None = None, *, decode: Literal[False] |
|
) -> bytes: ... |
|
|
|
@overload |
|
def recv( |
|
self, timeout: float | None = None, decode: bool | None = None |
|
) -> Data: ... |
|
|
|
def recv(self, timeout: float | None = None, decode: bool | None = None) -> Data: |
|
""" |
|
Receive the next message. |
|
|
|
When the connection is closed, :meth:`recv` raises |
|
:exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it raises |
|
:exc:`~websockets.exceptions.ConnectionClosedOK` after a normal closure |
|
and :exc:`~websockets.exceptions.ConnectionClosedError` after a protocol |
|
error or a network failure. This is how you detect the end of the |
|
message stream. |
|
|
|
If ``timeout`` is :obj:`None`, block until a message is received. If |
|
``timeout`` is set, wait up to ``timeout`` seconds for a message to be |
|
received and return it, else raise :exc:`TimeoutError`. If ``timeout`` |
|
is ``0`` or negative, check if a message has been received already and |
|
return it, else raise :exc:`TimeoutError`. |
|
|
|
If the message is fragmented, wait until all fragments are received, |
|
reassemble them, and return the whole message. |
|
|
|
Args: |
|
timeout: Timeout for receiving a message in seconds. |
|
decode: Set this flag to override the default behavior of returning |
|
:class:`str` or :class:`bytes`. See below for details. |
|
|
|
Returns: |
|
A string (:class:`str`) for a Text_ frame or a bytestring |
|
(:class:`bytes`) for a Binary_ frame. |
|
|
|
.. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
.. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
|
|
You may override this behavior with the ``decode`` argument: |
|
|
|
* Set ``decode=False`` to disable UTF-8 decoding of Text_ frames and |
|
return a bytestring (:class:`bytes`). This improves performance |
|
when decoding isn't needed, for example if the message contains |
|
JSON and you're using a JSON library that expects a bytestring. |
|
* Set ``decode=True`` to force UTF-8 decoding of Binary_ frames |
|
and return a string (:class:`str`). This may be useful for |
|
servers that send binary frames instead of text frames. |
|
|
|
Raises: |
|
ConnectionClosed: When the connection is closed. |
|
ConcurrencyError: If two threads call :meth:`recv` or |
|
:meth:`recv_streaming` concurrently. |
|
|
|
""" |
|
try: |
|
return self.recv_messages.get(timeout, decode) |
|
except EOFError: |
|
pass |
|
|
|
except ConcurrencyError: |
|
raise ConcurrencyError( |
|
"cannot call recv while another thread " |
|
"is already running recv or recv_streaming" |
|
) from None |
|
except UnicodeDecodeError as exc: |
|
with self.send_context(): |
|
self.protocol.fail( |
|
CloseCode.INVALID_DATA, |
|
f"{exc.reason} at position {exc.start}", |
|
) |
|
|
|
|
|
|
|
self.recv_events_thread.join() |
|
raise self.protocol.close_exc from self.recv_exc |
|
|
|
@overload |
|
def recv_streaming(self, decode: Literal[True]) -> Iterator[str]: ... |
|
|
|
@overload |
|
def recv_streaming(self, decode: Literal[False]) -> Iterator[bytes]: ... |
|
|
|
@overload |
|
def recv_streaming(self, decode: bool | None = None) -> Iterator[Data]: ... |
|
|
|
def recv_streaming(self, decode: bool | None = None) -> Iterator[Data]: |
|
""" |
|
Receive the next message frame by frame. |
|
|
|
This method is designed for receiving fragmented messages. It returns an |
|
iterator that yields each fragment as it is received. This iterator must |
|
be fully consumed. Else, future calls to :meth:`recv` or |
|
:meth:`recv_streaming` will raise |
|
:exc:`~websockets.exceptions.ConcurrencyError`, making the connection |
|
unusable. |
|
|
|
:meth:`recv_streaming` raises the same exceptions as :meth:`recv`. |
|
|
|
Args: |
|
decode: Set this flag to override the default behavior of returning |
|
:class:`str` or :class:`bytes`. See below for details. |
|
|
|
Returns: |
|
An iterator of strings (:class:`str`) for a Text_ frame or |
|
bytestrings (:class:`bytes`) for a Binary_ frame. |
|
|
|
.. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
.. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
|
|
You may override this behavior with the ``decode`` argument: |
|
|
|
* Set ``decode=False`` to disable UTF-8 decoding of Text_ frames |
|
and return bytestrings (:class:`bytes`). This may be useful to |
|
optimize performance when decoding isn't needed. |
|
* Set ``decode=True`` to force UTF-8 decoding of Binary_ frames |
|
and return strings (:class:`str`). This is useful for servers |
|
that send binary frames instead of text frames. |
|
|
|
Raises: |
|
ConnectionClosed: When the connection is closed. |
|
ConcurrencyError: If two threads call :meth:`recv` or |
|
:meth:`recv_streaming` concurrently. |
|
|
|
""" |
|
try: |
|
yield from self.recv_messages.get_iter(decode) |
|
return |
|
except EOFError: |
|
pass |
|
|
|
except ConcurrencyError: |
|
raise ConcurrencyError( |
|
"cannot call recv_streaming while another thread " |
|
"is already running recv or recv_streaming" |
|
) from None |
|
except UnicodeDecodeError as exc: |
|
with self.send_context(): |
|
self.protocol.fail( |
|
CloseCode.INVALID_DATA, |
|
f"{exc.reason} at position {exc.start}", |
|
) |
|
|
|
|
|
|
|
self.recv_events_thread.join() |
|
raise self.protocol.close_exc from self.recv_exc |
|
|
|
def send( |
|
self, |
|
message: Data | Iterable[Data], |
|
text: bool | None = None, |
|
) -> None: |
|
""" |
|
Send a message. |
|
|
|
A string (:class:`str`) is sent as a Text_ frame. A bytestring or |
|
bytes-like object (:class:`bytes`, :class:`bytearray`, or |
|
:class:`memoryview`) is sent as a Binary_ frame. |
|
|
|
.. _Text: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
.. _Binary: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 |
|
|
|
You may override this behavior with the ``text`` argument: |
|
|
|
* Set ``text=True`` to send a bytestring or bytes-like object |
|
(:class:`bytes`, :class:`bytearray`, or :class:`memoryview`) as a |
|
Text_ frame. This improves performance when the message is already |
|
UTF-8 encoded, for example if the message contains JSON and you're |
|
using a JSON library that produces a bytestring. |
|
* Set ``text=False`` to send a string (:class:`str`) in a Binary_ |
|
frame. This may be useful for servers that expect binary frames |
|
instead of text frames. |
|
|
|
:meth:`send` also accepts an iterable of strings, bytestrings, or |
|
bytes-like objects to enable fragmentation_. Each item is treated as a |
|
message fragment and sent in its own frame. All items must be of the |
|
same type, or else :meth:`send` will raise a :exc:`TypeError` and the |
|
connection will be closed. |
|
|
|
.. _fragmentation: https://datatracker.ietf.org/doc/html/rfc6455#section-5.4 |
|
|
|
:meth:`send` rejects dict-like objects because this is often an error. |
|
(If you really want to send the keys of a dict-like object as fragments, |
|
call its :meth:`~dict.keys` method and pass the result to :meth:`send`.) |
|
|
|
When the connection is closed, :meth:`send` raises |
|
:exc:`~websockets.exceptions.ConnectionClosed`. Specifically, it |
|
raises :exc:`~websockets.exceptions.ConnectionClosedOK` after a normal |
|
connection closure and |
|
:exc:`~websockets.exceptions.ConnectionClosedError` after a protocol |
|
error or a network failure. |
|
|
|
Args: |
|
message: Message to send. |
|
|
|
Raises: |
|
ConnectionClosed: When the connection is closed. |
|
ConcurrencyError: If the connection is sending a fragmented message. |
|
TypeError: If ``message`` doesn't have a supported type. |
|
|
|
""" |
|
|
|
|
|
|
|
if isinstance(message, str): |
|
with self.send_context(): |
|
if self.send_in_progress: |
|
raise ConcurrencyError( |
|
"cannot call send while another thread is already running send" |
|
) |
|
if text is False: |
|
self.protocol.send_binary(message.encode()) |
|
else: |
|
self.protocol.send_text(message.encode()) |
|
|
|
elif isinstance(message, BytesLike): |
|
with self.send_context(): |
|
if self.send_in_progress: |
|
raise ConcurrencyError( |
|
"cannot call send while another thread is already running send" |
|
) |
|
if text is True: |
|
self.protocol.send_text(message) |
|
else: |
|
self.protocol.send_binary(message) |
|
|
|
|
|
|
|
elif isinstance(message, Mapping): |
|
raise TypeError("data is a dict-like object") |
|
|
|
|
|
|
|
elif isinstance(message, Iterable): |
|
chunks = iter(message) |
|
try: |
|
chunk = next(chunks) |
|
except StopIteration: |
|
return |
|
|
|
try: |
|
|
|
if isinstance(chunk, str): |
|
with self.send_context(): |
|
if self.send_in_progress: |
|
raise ConcurrencyError( |
|
"cannot call send while another thread " |
|
"is already running send" |
|
) |
|
self.send_in_progress = True |
|
if text is False: |
|
self.protocol.send_binary(chunk.encode(), fin=False) |
|
else: |
|
self.protocol.send_text(chunk.encode(), fin=False) |
|
encode = True |
|
elif isinstance(chunk, BytesLike): |
|
with self.send_context(): |
|
if self.send_in_progress: |
|
raise ConcurrencyError( |
|
"cannot call send while another thread " |
|
"is already running send" |
|
) |
|
self.send_in_progress = True |
|
if text is True: |
|
self.protocol.send_text(chunk, fin=False) |
|
else: |
|
self.protocol.send_binary(chunk, fin=False) |
|
encode = False |
|
else: |
|
raise TypeError("data iterable must contain bytes or str") |
|
|
|
|
|
for chunk in chunks: |
|
if isinstance(chunk, str) and encode: |
|
with self.send_context(): |
|
assert self.send_in_progress |
|
self.protocol.send_continuation(chunk.encode(), fin=False) |
|
elif isinstance(chunk, BytesLike) and not encode: |
|
with self.send_context(): |
|
assert self.send_in_progress |
|
self.protocol.send_continuation(chunk, fin=False) |
|
else: |
|
raise TypeError("data iterable must contain uniform types") |
|
|
|
|
|
with self.send_context(): |
|
self.protocol.send_continuation(b"", fin=True) |
|
self.send_in_progress = False |
|
|
|
except ConcurrencyError: |
|
|
|
|
|
raise |
|
|
|
except Exception: |
|
|
|
|
|
with self.send_context(): |
|
self.protocol.fail( |
|
CloseCode.INTERNAL_ERROR, |
|
"error in fragmented message", |
|
) |
|
raise |
|
|
|
else: |
|
raise TypeError("data must be str, bytes, or iterable") |
|
|
|
def close(self, code: int = CloseCode.NORMAL_CLOSURE, reason: str = "") -> None: |
|
""" |
|
Perform the closing handshake. |
|
|
|
:meth:`close` waits for the other end to complete the handshake, for the |
|
TCP connection to terminate, and for all incoming messages to be read |
|
with :meth:`recv`. |
|
|
|
:meth:`close` is idempotent: it doesn't do anything once the |
|
connection is closed. |
|
|
|
Args: |
|
code: WebSocket close code. |
|
reason: WebSocket close reason. |
|
|
|
""" |
|
try: |
|
|
|
|
|
with self.send_context(): |
|
if self.send_in_progress: |
|
self.protocol.fail( |
|
CloseCode.INTERNAL_ERROR, |
|
"close during fragmented message", |
|
) |
|
else: |
|
self.protocol.send_close(code, reason) |
|
except ConnectionClosed: |
|
|
|
|
|
pass |
|
|
|
def ping( |
|
self, |
|
data: Data | None = None, |
|
ack_on_close: bool = False, |
|
) -> threading.Event: |
|
""" |
|
Send a Ping_. |
|
|
|
.. _Ping: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2 |
|
|
|
A ping may serve as a keepalive or as a check that the remote endpoint |
|
received all messages up to this point |
|
|
|
Args: |
|
data: Payload of the ping. A :class:`str` will be encoded to UTF-8. |
|
If ``data`` is :obj:`None`, the payload is four random bytes. |
|
ack_on_close: when this option is :obj:`True`, the event will also |
|
be set when the connection is closed. While this avoids getting |
|
stuck waiting for a pong that will never arrive, it requires |
|
checking that the state of the connection is still ``OPEN`` to |
|
confirm that a pong was received, rather than the connection |
|
being closed. |
|
|
|
Returns: |
|
An event that will be set when the corresponding pong is received. |
|
You can ignore it if you don't intend to wait. |
|
|
|
:: |
|
|
|
pong_event = ws.ping() |
|
pong_event.wait() # only if you want to wait for the pong |
|
|
|
Raises: |
|
ConnectionClosed: When the connection is closed. |
|
ConcurrencyError: If another ping was sent with the same data and |
|
the corresponding pong wasn't received yet. |
|
|
|
""" |
|
if isinstance(data, BytesLike): |
|
data = bytes(data) |
|
elif isinstance(data, str): |
|
data = data.encode() |
|
elif data is not None: |
|
raise TypeError("data must be str or bytes-like") |
|
|
|
with self.send_context(): |
|
|
|
if data in self.pong_waiters: |
|
raise ConcurrencyError("already waiting for a pong with the same data") |
|
|
|
|
|
while data is None or data in self.pong_waiters: |
|
data = struct.pack("!I", random.getrandbits(32)) |
|
|
|
pong_waiter = threading.Event() |
|
self.pong_waiters[data] = (pong_waiter, time.monotonic(), ack_on_close) |
|
self.protocol.send_ping(data) |
|
return pong_waiter |
|
|
|
def pong(self, data: Data = b"") -> None: |
|
""" |
|
Send a Pong_. |
|
|
|
.. _Pong: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3 |
|
|
|
An unsolicited pong may serve as a unidirectional heartbeat. |
|
|
|
Args: |
|
data: Payload of the pong. A :class:`str` will be encoded to UTF-8. |
|
|
|
Raises: |
|
ConnectionClosed: When the connection is closed. |
|
|
|
""" |
|
if isinstance(data, BytesLike): |
|
data = bytes(data) |
|
elif isinstance(data, str): |
|
data = data.encode() |
|
else: |
|
raise TypeError("data must be str or bytes-like") |
|
|
|
with self.send_context(): |
|
self.protocol.send_pong(data) |
|
|
|
|
|
|
|
def process_event(self, event: Event) -> None: |
|
""" |
|
Process one incoming event. |
|
|
|
This method is overridden in subclasses to handle the handshake. |
|
|
|
""" |
|
assert isinstance(event, Frame) |
|
if event.opcode in DATA_OPCODES: |
|
self.recv_messages.put(event) |
|
|
|
if event.opcode is Opcode.PONG: |
|
self.acknowledge_pings(bytes(event.data)) |
|
|
|
def acknowledge_pings(self, data: bytes) -> None: |
|
""" |
|
Acknowledge pings when receiving a pong. |
|
|
|
""" |
|
with self.protocol_mutex: |
|
|
|
if data not in self.pong_waiters: |
|
return |
|
|
|
pong_timestamp = time.monotonic() |
|
|
|
|
|
|
|
ping_id = None |
|
ping_ids = [] |
|
for ping_id, ( |
|
pong_waiter, |
|
ping_timestamp, |
|
_ack_on_close, |
|
) in self.pong_waiters.items(): |
|
ping_ids.append(ping_id) |
|
pong_waiter.set() |
|
if ping_id == data: |
|
self.latency = pong_timestamp - ping_timestamp |
|
break |
|
else: |
|
raise AssertionError("solicited pong not found in pings") |
|
|
|
|
|
for ping_id in ping_ids: |
|
del self.pong_waiters[ping_id] |
|
|
|
def acknowledge_pending_pings(self) -> None: |
|
""" |
|
Acknowledge pending pings when the connection is closed. |
|
|
|
""" |
|
assert self.protocol.state is CLOSED |
|
|
|
for pong_waiter, _ping_timestamp, ack_on_close in self.pong_waiters.values(): |
|
if ack_on_close: |
|
pong_waiter.set() |
|
|
|
self.pong_waiters.clear() |
|
|
|
def keepalive(self) -> None: |
|
""" |
|
Send a Ping frame and wait for a Pong frame at regular intervals. |
|
|
|
""" |
|
assert self.ping_interval is not None |
|
try: |
|
while True: |
|
|
|
|
|
|
|
self.recv_events_thread.join(self.ping_interval - self.latency) |
|
if not self.recv_events_thread.is_alive(): |
|
break |
|
|
|
try: |
|
pong_waiter = self.ping(ack_on_close=True) |
|
except ConnectionClosed: |
|
break |
|
if self.debug: |
|
self.logger.debug("% sent keepalive ping") |
|
|
|
if self.ping_timeout is not None: |
|
|
|
if pong_waiter.wait(self.ping_timeout): |
|
if self.debug: |
|
self.logger.debug("% received keepalive pong") |
|
else: |
|
if self.debug: |
|
self.logger.debug("- timed out waiting for keepalive pong") |
|
with self.send_context(): |
|
self.protocol.fail( |
|
CloseCode.INTERNAL_ERROR, |
|
"keepalive ping timeout", |
|
) |
|
break |
|
except Exception: |
|
self.logger.error("keepalive ping failed", exc_info=True) |
|
|
|
def start_keepalive(self) -> None: |
|
""" |
|
Run :meth:`keepalive` in a thread, unless keepalive is disabled. |
|
|
|
""" |
|
if self.ping_interval is not None: |
|
|
|
self.keepalive_thread = threading.Thread( |
|
target=self.keepalive, |
|
daemon=True, |
|
) |
|
self.keepalive_thread.start() |
|
|
|
def recv_events(self) -> None: |
|
""" |
|
Read incoming data from the socket and process events. |
|
|
|
Run this method in a thread as long as the connection is alive. |
|
|
|
``recv_events()`` exits immediately when the ``self.socket`` is closed. |
|
|
|
""" |
|
try: |
|
while True: |
|
try: |
|
with self.recv_flow_control: |
|
if self.close_deadline is not None: |
|
self.socket.settimeout(self.close_deadline.timeout()) |
|
data = self.socket.recv(self.recv_bufsize) |
|
except Exception as exc: |
|
if self.debug: |
|
self.logger.debug( |
|
"! error while receiving data", |
|
exc_info=True, |
|
) |
|
|
|
|
|
|
|
|
|
with self.protocol_mutex: |
|
self.set_recv_exc(exc) |
|
break |
|
|
|
if data == b"": |
|
break |
|
|
|
|
|
with self.protocol_mutex: |
|
|
|
self.protocol.receive_data(data) |
|
|
|
|
|
events = self.protocol.events_received() |
|
|
|
|
|
try: |
|
self.send_data() |
|
except Exception as exc: |
|
if self.debug: |
|
self.logger.debug( |
|
"! error while sending data", |
|
exc_info=True, |
|
) |
|
|
|
|
|
|
|
|
|
self.set_recv_exc(exc) |
|
break |
|
|
|
if self.protocol.close_expected(): |
|
|
|
|
|
if self.close_deadline is None: |
|
self.close_deadline = Deadline(self.close_timeout) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for event in events: |
|
|
|
self.process_event(event) |
|
|
|
|
|
|
|
with self.protocol_mutex: |
|
|
|
self.protocol.receive_eof() |
|
|
|
|
|
events = self.protocol.events_received() |
|
|
|
|
|
|
|
self.send_data() |
|
|
|
|
|
|
|
|
|
|
|
|
|
for event in events: |
|
|
|
self.process_event(event) |
|
|
|
except Exception as exc: |
|
|
|
self.logger.error("unexpected internal error", exc_info=True) |
|
with self.protocol_mutex: |
|
self.set_recv_exc(exc) |
|
finally: |
|
|
|
self.close_socket() |
|
|
|
@contextlib.contextmanager |
|
def send_context( |
|
self, |
|
*, |
|
expected_state: State = OPEN, |
|
) -> Iterator[None]: |
|
""" |
|
Create a context for writing to the connection from user code. |
|
|
|
On entry, :meth:`send_context` acquires the connection lock and checks |
|
that the connection is open; on exit, it writes outgoing data to the |
|
socket:: |
|
|
|
with self.send_context(): |
|
self.protocol.send_text(message.encode()) |
|
|
|
When the connection isn't open on entry, when the connection is expected |
|
to close on exit, or when an unexpected error happens, terminating the |
|
connection, :meth:`send_context` waits until the connection is closed |
|
then raises :exc:`~websockets.exceptions.ConnectionClosed`. |
|
|
|
""" |
|
|
|
wait_for_close = False |
|
|
|
raise_close_exc = False |
|
|
|
original_exc: BaseException | None = None |
|
|
|
|
|
with self.protocol_mutex: |
|
if self.protocol.state is expected_state: |
|
|
|
try: |
|
yield |
|
except (ProtocolError, ConcurrencyError): |
|
|
|
raise |
|
except Exception as exc: |
|
self.logger.error("unexpected internal error", exc_info=True) |
|
|
|
|
|
|
|
wait_for_close = False |
|
raise_close_exc = True |
|
original_exc = exc |
|
else: |
|
|
|
if self.protocol.close_expected(): |
|
wait_for_close = True |
|
|
|
|
|
|
|
|
|
|
|
assert self.close_deadline is None |
|
self.close_deadline = Deadline(self.close_timeout) |
|
|
|
try: |
|
self.send_data() |
|
except Exception as exc: |
|
if self.debug: |
|
self.logger.debug( |
|
"! error while sending data", |
|
exc_info=True, |
|
) |
|
|
|
|
|
wait_for_close = False |
|
raise_close_exc = True |
|
original_exc = exc |
|
|
|
else: |
|
|
|
|
|
wait_for_close = True |
|
raise_close_exc = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
if wait_for_close: |
|
if self.close_deadline is None: |
|
timeout = self.close_timeout |
|
else: |
|
|
|
timeout = self.close_deadline.timeout(raise_if_elapsed=False) |
|
self.recv_events_thread.join(timeout) |
|
|
|
if self.recv_events_thread.is_alive(): |
|
|
|
|
|
assert original_exc is None |
|
original_exc = TimeoutError("timed out while closing connection") |
|
|
|
|
|
raise_close_exc = True |
|
with self.protocol_mutex: |
|
self.set_recv_exc(original_exc) |
|
|
|
|
|
|
|
if raise_close_exc: |
|
self.close_socket() |
|
|
|
self.recv_events_thread.join() |
|
raise self.protocol.close_exc from original_exc |
|
|
|
def send_data(self) -> None: |
|
""" |
|
Send outgoing data. |
|
|
|
This method requires holding protocol_mutex. |
|
|
|
Raises: |
|
OSError: When a socket operations fails. |
|
|
|
""" |
|
assert self.protocol_mutex.locked() |
|
for data in self.protocol.data_to_send(): |
|
if data: |
|
if self.close_deadline is not None: |
|
self.socket.settimeout(self.close_deadline.timeout()) |
|
self.socket.sendall(data) |
|
else: |
|
try: |
|
self.socket.shutdown(socket.SHUT_WR) |
|
except OSError: |
|
pass |
|
|
|
def set_recv_exc(self, exc: BaseException | None) -> None: |
|
""" |
|
Set recv_exc, if not set yet. |
|
|
|
This method requires holding protocol_mutex. |
|
|
|
""" |
|
assert self.protocol_mutex.locked() |
|
if self.recv_exc is None: |
|
self.recv_exc = exc |
|
|
|
def close_socket(self) -> None: |
|
""" |
|
Shutdown and close socket. Close message assembler. |
|
|
|
Calling close_socket() guarantees that recv_events() terminates. Indeed, |
|
recv_events() may block only on socket.recv() or on recv_messages.put(). |
|
|
|
""" |
|
|
|
try: |
|
self.socket.shutdown(socket.SHUT_RDWR) |
|
except OSError: |
|
pass |
|
self.socket.close() |
|
|
|
|
|
|
|
with self.protocol_mutex: |
|
self.protocol.receive_eof() |
|
assert self.protocol.state is CLOSED |
|
|
|
|
|
self.recv_messages.close() |
|
|
|
|
|
self.acknowledge_pending_pings() |
|
|