asyncio UDP 连接如何接收整个数据报?

How asyncio UDP connection receives whole datagrams?

Python 库中有一个接口 asyncio.DatagramProtocol。它提供了通过使用方法 datagram_received(self, data, addr):

来实现接收数据报的可能性
class MyDatagramProtocol(asyncio.DatagramProtocol):
    def datagram_received(self, data: bytes, addr: tuple[str, int]):
        # Here I can use data as whole datagram. 
        # If there are more pending datagrams, it will called more than once
        pass

asyncio 事件循环接收整个数据报。但是当我使用 BSD 套接字时,我应该使用全部或部分接收到的数据。总数据可以大于一个数据报。而且我不知道这些数据的范围。我只能使用我自己的算法来解析它(读取标题、body 中的字节数等)。

if (ioctlsocket(Socket, FIONREAD, (u_long*)(&PendingDataSize)) == 0)
{
    BytesRead = recv(Socket, (char*)Data, PendingDataSize, Flags); 
    // Now I can use Data and parse it here. 
    // But Data can be two or more datagrams if remote machine sent two or more packets?
}

如何仅使用 BSD 套接字重复 asyncio 的行为?

使用数据报套接字,recv 总是一次只接收一个数据报。

man page udp(7):

All receive operations return only one packet.