简体   繁体   English

提升时间戳 UDP 数据包

[英]Boost time stamping UDP packets

I would like to retrieve the reception timestamp for UDP packets received using boost asio.我想检索使用 boost asio 接收的 UDP 数据包的接收时间戳。

I found out that the kernel provides the socket option SO_TIMESTAMP which should allow the generation of a timestamp when the packets is received by the NIC.我发现内核提供了套接字选项SO_TIMESTAMP ,它应该允许在 NIC 接收到数据包时生成时间戳。 I also found this old ticket which was proposing a patch to add support for SO_TIMESTAMP.我还发现了这张旧,它提出了一个补丁来添加对 SO_TIMESTAMP 的支持。

I'm using boost 1.60.0 and I couldn't enable this option:我正在使用 boost 1.60.0,但无法启用此选项:

ip::udp::socket sock; 
... 
sock.set_option(ip::unicast::timestamp(true));

How can I retrieve a UDP packet reception time with boost and compute the elapsed time since reception when I receive the packet with a synchronous or an asynchronous read?当我通过同步或异步读取接收数据包时,如何使用 boost 检索 UDP 数据包接收时间并计算自接收以来经过的时间?

It seems boost has not implement this option yet, you can do it with native socket if your platform supports ancillary data:似乎 boost 还没有实现这个选项,如果你的平台支持辅助数据,你可以用原生套接字来实现:

int socket = sock.native(); 
int opt = 1;
setsockopt( sock, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt));

then access control messages from the native socket:然后从本机套接字访问控制消息:

int received = recvmsg(socket, &msgh, 0);
struct msghdr msgh;
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
    if ((cmsg->cmsg_level == SOL_SOCKET ) &&(cmsg->cmsg_type == SO_TIMESTAMP ))
        // read the timestamp
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM