@@ -23,7 +23,7 @@ def __init__(
23
23
self .on_error_received = on_error_received
24
24
25
25
self .expected_packets : WeakValueDictionary [
26
- Tuple [int , int ], 'asyncio.Future[Tuple[int, Addr]]'
26
+ Tuple [int , int ], 'asyncio.Future[Tuple[int, int, Addr]]'
27
27
] = WeakValueDictionary ()
28
28
29
29
self .transport : asyncio .DatagramTransport = None # type: ignore
@@ -33,7 +33,7 @@ def ping_request(
33
33
client_id : int ,
34
34
packet_id : int ,
35
35
payload_size : int ,
36
- response_future : 'asyncio.Future[Tuple[int, Addr]]' ,
36
+ response_future : 'asyncio.Future[Tuple[int, int, Addr]]' ,
37
37
) -> None :
38
38
self .expected_packets [(client_id , packet_id )] = response_future
39
39
frame = pack_frame (client_id , packet_id , payload_size )
@@ -48,7 +48,7 @@ def connection_lost(self, exc: Optional[Exception]) -> None:
48
48
49
49
def datagram_received (self , data : bytes , addr : Addr ) -> None :
50
50
client_id , packet_id , payload_size = unpack_frame (data )
51
- self .expected_packets [(client_id , packet_id )].set_result ((payload_size , addr ))
51
+ self .expected_packets [(client_id , packet_id )].set_result ((packet_id , payload_size , addr ))
52
52
53
53
def error_received (self , exc : Exception ) -> None :
54
54
print (f'Request error: { exc } ' )
@@ -99,7 +99,7 @@ async def connect(self) -> None:
99
99
async def stop (self ) -> None :
100
100
self .transport .close ()
101
101
102
- async def ping_request (self ) -> Tuple [int , int , Addr ]:
102
+ async def ping_request (self ) -> Tuple [int , int , int , Addr ]:
103
103
loop = self .loop or asyncio .get_running_loop ()
104
104
105
105
packet_id = self .packet_counter
@@ -142,15 +142,21 @@ async def start_udp_client(
142
142
received = 0
143
143
try :
144
144
while run_infite or transmitted < count :
145
+ async def proc () -> None :
146
+ nonlocal received
147
+ try :
148
+ round_trip , packet_id , ret_payload_size , addr = await asyncio .wait_for (
149
+ udp_client .ping_request (), timeout )
150
+ except asyncio .TimeoutError :
151
+ print ('Request timeout' )
152
+ else :
153
+ received += 1
154
+ round_trips .append (round_trip )
155
+ print (f'{ ret_payload_size + HEADER_SIZE } bytes from { addr [0 ]} :{ addr [1 ]} : '
156
+ f'seq={ packet_id } time={ round_trip :.3f} ms' )
157
+
145
158
transmitted += 1
146
- try :
147
- round_trip , ret_payload_size , addr = await asyncio .wait_for (udp_client .ping_request (), timeout )
148
- except asyncio .TimeoutError :
149
- print ('Request timeout' )
150
- else :
151
- received += 1
152
- round_trips .append (round_trip )
153
- print (f'{ ret_payload_size + HEADER_SIZE } bytes from { addr [0 ]} :{ addr [1 ]} : time={ round_trip :.3f} ms' )
159
+ asyncio .ensure_future (proc ())
154
160
await asyncio .sleep (wait )
155
161
except asyncio .CancelledError :
156
162
await udp_client .stop ()
0 commit comments