input:b0011v0001p0001eAB41-e0,1,4,11,14,k71,72,73,74,75,76,77,79,7A,7B,7C,7D,7E,7F,80,8C,8E,8F,9B,9C,9D,9E,9F,A3,A4,A5,A6,AC,AD,B7,B8,B9,C1,D9,E2,ram4,l0,1,2,sfw
  5   ˆl–ßi~” T¾R( 	Å õÆÙ¸”Å£�þB gÅÂz0_ÏÌƒh¹ËÂÉ      input:b0011v0002p0013e0006-e0,1,3,k110,111,112,ra0,1,mlsfw
  A   ˆl–Ãa¾”Ši?u8 Eq Üe¥1hß�þB@F³�ÊøÊQ�˜ÞóÍƒt.¿Ì_ŠI|¥�+Wé3ž¯ÄË     # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license

import socket
import ssl
import selectors
import struct
import threading
import time

import aioquic.quic.configuration  # type: ignore
import aioquic.quic.connection  # type: ignore
import aioquic.quic.events  # type: ignore
import dns.inet

from dns.quic._common import (
    BaseQuicStream,
    BaseQuicConnection,
    BaseQuicManager,
    QUIC_MAX_DATAGRAM,
)

# Avoid circularity with dns.query
if hasattr(selectors, "PollSelector"):
    _selector_class = selectors.PollSelector  # type: ignore
else:
    _selector_class = selectors.SelectSelector  # type: ignore


class SyncQuicStream(BaseQuicStream):
    def __init__(self, connection, stream_id):
        super().__init__(connection, stream_id)
        self._wake_up = threading.Condition()
        self._lock = threading.Lock()

    def wait_for(self, amount, expiration):
        timeout = self._timeout_from_expiration(expiration)
        while True:
            with self._lock:
                if self._buffer.have(amount):
                    return
                self._expecting = amount
            with self._wake_up:
                self._wake_up.wait(timeout)
            self._expecting = 0

    def receive(self, timeout=None):
        expiration = self._expiration_from_timeout(timeout)
        self.wait_for(2, expiration)
        with self._lock:
            (size,) = struct.unpack("!H", self._buffer.get(2))
        self.wait_for(size, expiration)
        with self._lock:
            return self._buffer.get(size)

    def send(self, datagram, is_end=False):
        data = self._encapsulate(datagram)
        self._connection.write(self._stream_id, data, is_end)

    def _add_input(self, data, is_end):
        if self._common_add_input(data, is_end):
            with self._wake_up:
                self._wake_up.notify()

    def close(self):
        with self._lock:
            self._close()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()
        with self._wake_up:
            self._wake_up.notify()
        return False


class SyncQuicConnection(BaseQuicConnection):
    def __init__(self, connection, address, port, source, source_port, manager):
        super().__init__(connection, address, port, source, source_port, manager)
        self._socket = socket.socket(self._af, socket.SOCK_DGRAM, 0)
        self._socket.connect(self._peer)
        (self._send_wakeup, self._receive_wakeup) = socket.socketpair()
        self._receive_wakeup.setblocking(False)
        self._socket.setblocking(False)
        if self._source is not None:
            try:
                self._socket.bind(
                    dns.inet.low_level_address_tuple(self._source, self._af)
                )
            except Exception:
                self._socket.close()
                raise
        self._handshake_complete = threading.Event()
        self._worker_thread = None
        self._lock = threading.Lock()

    def _read(self):
        count = 0
        while count < 10:
            count += 1
            try:
                datagram = self._socket.recv(QUIC_MAX_DATAGRAM)
            except BlockingIOError:
                return
            with self._lock:
                self._connection.receive_datagram(datagram, self._peer[0], time.time())

    def _drain_wakeup(self):
        while True:
            try:
                self._receive_wakeup.recv(32)
            except BlockingIOError:
                return

    def _worker(self):
        sel = _selector_class()
        sel.register(self._socket, selectors.EVENT_READ, self._read)
        sel.register(self._receive_wakeu