Re: UDP Sockets & Clock Synchronization
Re: UDP Sockets & Clock Synchronization
- Subject: Re: UDP Sockets & Clock Synchronization
- From: Zack Morris <email@hidden>
- Date: Thu, 28 Feb 2002 09:06:49 -0700
on 2/27/02 10:15 PM, Peter Sichel at email@hidden wrote:
>
I've discovered there is a better way. Although not documented
>
in Stevens "UNIX Network Programming", I found an SO_TIMESTAMP option
>
in sys/socket.h
>
>
If the SO_TIMESTAMP option is enabled on a SOCK_DGRAM socket, the
>
recvmsg() call will return a timestamp corresponding to when the
>
datagram was received. The msg_control field in the msghdr structure
>
points to a buffer that contains a cmsghdr structure followed by a
>
struct timeval. The cmsghdr fields have the following values:
>
>
cmsg_len = sizeof(struct timeval)
>
cmsg_level = SOL_SOCKET
>
cmsg_type = SCM_TIMESTAMP
>
>
So even though you can't respond at interrupt time, you can capture
>
when UDP datagrams arrive without resorting to an NKE.
>
By including both the timestamp and send time in your response,
>
you should be able to calculate the relative time difference.
Wow, this could save my life! I've been talking to people about how I
thought sockets was flawed because without a time stamp, you can't do clock
synch for games (glad I'm wrong). Everyone's ideas about the separate
thread are good, except for OS 9. I looked into pthreads, and as far as I
can tell, they are cooperative, which I believe means they would only get
called at WaitNextEvent(), which is every 33 ms. There might be other OS
calls that yield to the thread manager, or most likely the GUSI calls like
recv do this automatically, but I would still only call them in one place in
the loop (every 33 ms). Can I get this time stamp information through GUSI?
If not, can I make a filter of some sort where I can intercept the message
in a notifier and tag a time stamp to it?
If not, my idea now is to use nonblocking sockets (blocking with select
with a timeout of 0) on OS 9 & 10 for TCP since a timestamp is not needed,
and use some queued message passing system for UDP. I already have an
asynch library for UDP which works okay (I stick the time stamp in my
message). Actually, this code might work as-is on X, although I don't know
how efficient it is to have a notifier in X. I assume the other thread is
already going in the background, so when a packet comes in, it just executes
my notifier code.
I really would like to go all-sockets, I wish there was a way to call
GUSI from an MP thread or time manager task. I am also a little concerned
that GUSI could be calling WaitNextEvent or doing a lot of threading that
could slow down a game. Has anyone experienced this? Thanx for all your
help,
Zack Morris
Z Sculpt Entertainment
email@hidden
http://www.zsculpt.com
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.