@@ -344,7 +344,7 @@ void udp_server::handle_receive(std::unique_ptr<uint8_t[]> buffer_cache, const a
344344{
345345 if (error)
346346 {
347- if (!connection_socket.is_open ())
347+ if (error == asio::error::operation_aborted || !connection_socket.is_open ())
348348 return ;
349349 }
350350
@@ -396,6 +396,9 @@ void udp_client::pause(bool set_as_pause)
396396
397397void udp_client::stop ()
398398{
399+ bool expect = true ;
400+ if (stopped.compare_exchange_strong (expect, true ))
401+ return ;
399402 stopped.store (true );
400403 callback = empty_udp_callback;
401404 if (connection_socket.is_open ())
@@ -544,7 +547,10 @@ void udp_client::start_receive()
544547
545548 std::unique_ptr<uint8_t []> buffer_cache = std::make_unique<uint8_t []>(BUFFER_SIZE);
546549 uint8_t *buffer_raw_ptr = buffer_cache.get ();
547- connection_socket.async_receive_from (asio::buffer (buffer_raw_ptr, BUFFER_SIZE), incoming_endpoint,
550+ auto asio_buffer = asio::buffer (buffer_raw_ptr, BUFFER_SIZE);
551+ if (!connection_socket.is_open ())
552+ return ;
553+ connection_socket.async_receive_from (asio_buffer, incoming_endpoint,
548554 [buffer = std::move (buffer_cache), this ](const asio::error_code &error, std::size_t bytes_transferred) mutable
549555 {
550556 handle_receive (std::move (buffer), error, bytes_transferred);
@@ -555,7 +561,7 @@ void udp_client::handle_receive(std::unique_ptr<uint8_t[]> buffer_cache, const a
555561{
556562 if (error)
557563 {
558- if (!stopped.load () && !paused.load () && connection_socket.is_open ())
564+ if (error != asio::error::operation_aborted && !stopped.load () && !paused.load () && connection_socket.is_open ())
559565 start_receive ();
560566 return ;
561567 }
0 commit comments