@@ -23,7 +23,7 @@ def __init__(
2323 self .on_error_received = on_error_received
2424
2525 self .expected_packets : WeakValueDictionary [
26- Tuple [int , int ], 'asyncio.Future[Tuple[int, Addr]]'
26+ Tuple [int , int ], 'asyncio.Future[Tuple[int, int, Addr]]'
2727 ] = WeakValueDictionary ()
2828
2929 self .transport : asyncio .DatagramTransport = None # type: ignore
@@ -33,7 +33,7 @@ def ping_request(
3333 client_id : int ,
3434 packet_id : int ,
3535 payload_size : int ,
36- response_future : 'asyncio.Future[Tuple[int, Addr]]' ,
36+ response_future : 'asyncio.Future[Tuple[int, int, Addr]]' ,
3737 ) -> None :
3838 self .expected_packets [(client_id , packet_id )] = response_future
3939 frame = pack_frame (client_id , packet_id , payload_size )
@@ -48,7 +48,7 @@ def connection_lost(self, exc: Optional[Exception]) -> None:
4848
4949 def datagram_received (self , data : bytes , addr : Addr ) -> None :
5050 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 ))
5252
5353 def error_received (self , exc : Exception ) -> None :
5454 print (f'Request error: { exc } ' )
@@ -99,7 +99,7 @@ async def connect(self) -> None:
9999 async def stop (self ) -> None :
100100 self .transport .close ()
101101
102- async def ping_request (self ) -> Tuple [int , int , Addr ]:
102+ async def ping_request (self ) -> Tuple [int , int , int , Addr ]:
103103 loop = self .loop or asyncio .get_running_loop ()
104104
105105 packet_id = self .packet_counter
@@ -142,15 +142,21 @@ async def start_udp_client(
142142 received = 0
143143 try :
144144 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+
145158 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 ())
154160 await asyncio .sleep (wait )
155161 except asyncio .CancelledError :
156162 await udp_client .stop ()
0 commit comments