Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
uael committed Nov 7, 2023
1 parent 1ca950f commit 9dbd2d4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 110 deletions.
54 changes: 21 additions & 33 deletions bumble/l2cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@
from pyee import EventEmitter
from typing import (
Dict,
Set,
Type,
List,
Optional,
Tuple,
Callable,
Any,
TypeVar,
Union,
Deque,
Iterable,
Set,
SupportsBytes,
TYPE_CHECKING,
overload,
Expand Down Expand Up @@ -1450,9 +1449,6 @@ class LeCreditBased(Any):


# -----------------------------------------------------------------------------
TPendingConnection = TypeVar('TPendingConnection', bound=PendingConnection.Any)


class IncomingConnection:
@dataclasses.dataclass
class Any:
Expand All @@ -1462,38 +1458,26 @@ class Any:
psm: int
source_cid: int

def expired(self) -> bool:
...

class Future(asyncio.Future[TPendingConnection]):
def accept(self, pend: TPendingConnection) -> bool:
"""Accept this connection request."""
try:
self.set_result(pend)
return True
except asyncio.InvalidStateError:
return False

def expired(self) -> bool:
return self.done() or self.cancelled()
def __post_init__(self) -> None:
self.future: asyncio.Future[Any] = asyncio.Future()

@dataclasses.dataclass
class Basic(Future[PendingConnection.Basic], Any):
class Basic(Any):
"""L2CAP incoming basic channel connection request."""

def __post_init__(self) -> None:
super().__init__()
future: asyncio.Future[PendingConnection.Basic] = dataclasses.field(init=False)

@dataclasses.dataclass
class LeCreditBased(Future[PendingConnection.LeCreditBased], Any):
class LeCreditBased(Any):
"""L2CAP incoming LE credit based channel connection request."""

mtu: int
mps: int
initial_credits: int

def __post_init__(self) -> None:
super().__init__()
future: asyncio.Future[PendingConnection.LeCreditBased] = dataclasses.field(
init=False
)


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1684,13 +1668,16 @@ def create_classic_server(

def listener(incoming: IncomingConnection.Basic) -> None:
if incoming.psm == spec.psm:
incoming.accept(PendingConnection.Basic(server.on_connection, spec.mtu))
incoming.future.set_result(
PendingConnection.Basic(server.on_connection, spec.mtu)
)

def close() -> None:
self.unlisten(listener)
assert spec.psm is not None
self.free_psm(spec.psm)

self.listen(listener)
server = ClassicChannelServer(close, spec.psm, handler)
return server

Expand Down Expand Up @@ -1725,7 +1712,7 @@ def create_le_credit_based_server(

def listener(incoming: IncomingConnection.LeCreditBased) -> None:
if incoming.psm == spec.psm:
incoming.accept(
incoming.future.set_result(
PendingConnection.LeCreditBased(
server.on_connection, spec.mtu, spec.mps, spec.max_credits
)
Expand All @@ -1736,6 +1723,7 @@ def close() -> None:
assert spec.psm is not None
self.free_psm(spec.psm)

self.listen(listener)
server = LeCreditBasedChannelServer(close, spec.psm, handler)
return server

Expand Down Expand Up @@ -1848,13 +1836,13 @@ async def handle_connection_request() -> None:

# Dispatch incoming connection.
for listener in self.listeners:
if not incoming.done():
if not incoming.future.done():
listener(incoming)

try:
pending = await asyncio.wait_for(incoming, timeout=3.0)
pending = await asyncio.wait_for(incoming.future, timeout=3.0)
except asyncio.TimeoutError as e:
incoming.cancel(e)
incoming.future.cancel(e)
pending = None

if pending:
Expand Down Expand Up @@ -2127,13 +2115,13 @@ async def handle_connection_request() -> None:

# Dispatch incoming connection.
for listener in self.listeners:
if not incoming.done():
if not incoming.future.done():
listener(incoming)

try:
pending = await asyncio.wait_for(incoming, timeout=3.0)
pending = await asyncio.wait_for(incoming.future, timeout=3.0)
except asyncio.TimeoutError as e:
incoming.cancel(e)
incoming.future.cancel(e)
pending = None

if pending:
Expand Down
Loading

0 comments on commit 9dbd2d4

Please sign in to comment.