|
1 | | -# Copyright 2020, 2022 Bruce Merry |
| 1 | +# Copyright 2020, 2022, 2024 Bruce Merry |
2 | 2 | # |
3 | 3 | # This file is part of async-solipsism. |
4 | 4 | # |
|
24 | 24 | from .exceptions import SolipsismError |
25 | 25 |
|
26 | 26 |
|
27 | | -__all__ = ('EventLoop', 'EventLoopPolicy', 'stream_pairs') |
| 27 | +__all__ = ('EventLoop', 'EventLoopPolicy', 'aiohappyeyeballs_start_connection', 'stream_pairs') |
28 | 28 |
|
29 | 29 |
|
30 | 30 | class EventLoop(asyncio.selector_events.BaseSelectorEventLoop): |
@@ -133,6 +133,27 @@ async def create_server( |
133 | 133 | **kwargs |
134 | 134 | ) |
135 | 135 |
|
| 136 | + async def aiohappyeyeballs_start_connection(self, |
| 137 | + addr_infos, |
| 138 | + *, |
| 139 | + local_addr_infos=None, |
| 140 | + happy_eyeballs_delay=None, |
| 141 | + interleave=None): |
| 142 | + for addr_info in addr_infos: |
| 143 | + addr = addr_info[4] |
| 144 | + addr = _socket._normalise_ipv6_sockaddr(addr) |
| 145 | + if addr in self.__listening_sockets: |
| 146 | + listener = self.__listening_sockets[addr] |
| 147 | + if local_addr_infos: |
| 148 | + local_addr_info = local_addr_infos[0] |
| 149 | + else: |
| 150 | + port = self.__next_port |
| 151 | + self.__next_port += 1 |
| 152 | + local_addr_info = ('::1', port, 0, 0) |
| 153 | + sock = await listener.make_connection(local_addr_info) |
| 154 | + return sock |
| 155 | + raise ConnectionRefusedError('No socket listening on requested addresses') |
| 156 | + |
136 | 157 | async def connect_read_pipe(self, protocol_factory, pipe): |
137 | 158 | raise SolipsismError("connect_read_pipe is not supported") |
138 | 159 |
|
@@ -207,3 +228,17 @@ async def stream_pairs(capacity=None): |
207 | 228 | streams1 = await asyncio.open_connection(sock=sock1) |
208 | 229 | streams2 = await asyncio.open_connection(sock=sock2) |
209 | 230 | return streams1, streams2 |
| 231 | + |
| 232 | + |
| 233 | +async def aiohappyeyeballs_start_connection(addr_infos, |
| 234 | + *, |
| 235 | + local_addr_infos=None, |
| 236 | + happy_eyeballs_delay=None, |
| 237 | + interleave=None, |
| 238 | + loop=None): |
| 239 | + if loop is None: |
| 240 | + loop = asyncio.get_running_loop() |
| 241 | + return await loop.aiohappyeyeballs_start_connection(addr_infos, |
| 242 | + local_addr_infos=local_addr_infos, |
| 243 | + happy_eyeballs_delay=happy_eyeballs_delay, |
| 244 | + interleave=interleave) |
0 commit comments