2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:gstnettimepacket
21 * @short_description: Helper structure to construct clock packets used
23 * @see_also: #GstClock, #GstNetClientClock, #GstNetTimeProvider
25 * Various functions for receiving, sending an serializing #GstNetTimePacket
28 * Last reviewed on 2005-11-23 (0.9.5)
42 #include "gstnettimepacket.h"
46 * gst_net_time_packet_new:
47 * @buffer: a buffer from which to construct the packet, or NULL
49 * Creates a new #GstNetTimePacket from a buffer received over the network. The
50 * caller is responsible for ensuring that @buffer is at least
51 * #GST_NET_TIME_PACKET_SIZE bytes long.
53 * If @buffer is #NULL, the local and remote times will be set to
54 * #GST_CLOCK_TIME_NONE.
56 * MT safe. Caller owns return value (g_free to free).
58 * Returns: The new #GstNetTimePacket.
61 gst_net_time_packet_new (const guint8 * buffer)
63 GstNetTimePacket *ret;
65 g_assert (sizeof (GstClockTime) == 8);
67 ret = g_new0 (GstNetTimePacket, 1);
70 ret->local_time = GST_READ_UINT64_BE (buffer);
71 ret->remote_time = GST_READ_UINT64_BE (buffer + sizeof (GstClockTime));
73 ret->local_time = GST_CLOCK_TIME_NONE;
74 ret->remote_time = GST_CLOCK_TIME_NONE;
81 * gst_net_time_packet_serialize:
82 * @packet: the #GstNetTimePacket
84 * Serialized a #GstNetTimePacket into a newly-allocated sequence of
85 * #GST_NET_TIME_PACKET_SIZE bytes, in network byte order. The value returned is
86 * suitable for passing to write(2) or sendto(2) for communication over the
89 * MT safe. Caller owns return value (g_free to free).
91 * Returns: A newly allocated sequence of #GST_NET_TIME_PACKET_SIZE bytes.
94 gst_net_time_packet_serialize (const GstNetTimePacket * packet)
98 g_assert (sizeof (GstClockTime) == 8);
100 ret = g_new0 (guint8, GST_NET_TIME_PACKET_SIZE);
102 GST_WRITE_UINT64_BE (ret, packet->local_time);
103 GST_WRITE_UINT64_BE (ret + sizeof (GstClockTime), packet->remote_time);
109 * gst_net_time_packet_receive:
110 * @fd: a file descriptor created by socket(2)
111 * @addr: a pointer to a sockaddr to hold the address of the sender
112 * @len: a pointer to the size of the data pointed to by @addr
114 * Receives a #GstNetTimePacket over a socket. Handles interrupted system calls,
115 * but otherwise returns NULL on error. See recvfrom(2) for more information on
116 * how to interpret @sockaddr.
118 * MT safe. Caller owns return value (g_free to free).
120 * Returns: The new #GstNetTimePacket.
123 gst_net_time_packet_receive (gint fd, struct sockaddr * addr, socklen_t * len)
125 guint8 buffer[GST_NET_TIME_PACKET_SIZE];
130 ret = recvfrom (fd, (char *) buffer, GST_NET_TIME_PACKET_SIZE,
132 ret = recvfrom (fd, buffer, GST_NET_TIME_PACKET_SIZE,
134 0, (struct sockaddr *) addr, len);
136 if (errno != EAGAIN && errno != EINTR)
140 } else if (ret < GST_NET_TIME_PACKET_SIZE) {
143 return gst_net_time_packet_new (buffer);
149 GST_DEBUG ("receive error %d: %s (%d)", ret, g_strerror (errno), errno);
154 GST_DEBUG ("someone sent us a short packet (%d < %d)",
155 ret, GST_NET_TIME_PACKET_SIZE);
161 * gst_net_time_packet_send:
162 * @packet: the #GstNetTimePacket
163 * @fd: a file descriptor created by socket(2)
164 * @addr: a pointer to a sockaddr to hold the address of the sender
165 * @len: the size of the data pointed to by @addr
167 * Sends a #GstNetTimePacket over a socket. Essentially a thin wrapper around
168 * sendto(2) and gst_net_time_packet_serialize().
172 * Returns: The return value of sendto(2).
175 gst_net_time_packet_send (const GstNetTimePacket * packet, gint fd,
176 struct sockaddr * addr, socklen_t len)
178 #if defined __CYGWIN__
180 #elif defined G_OS_WIN32
185 gint ret, send_flags;
187 g_return_val_if_fail (packet != NULL, -EINVAL);
191 fdflags = fcntl (fd, F_GETFL);
192 fcntl (fd, F_SETFL, fdflags | O_NONBLOCK);
193 #elif defined G_OS_WIN32
197 send_flags = MSG_DONTWAIT;
200 buffer = gst_net_time_packet_serialize (packet);
203 ioctlsocket (fd, FIONBIO, &flags); /* Set nonblocking mode */
205 sendto (fd, (char *) buffer, GST_NET_TIME_PACKET_SIZE, send_flags, addr,
208 ret = sendto (fd, buffer, GST_NET_TIME_PACKET_SIZE, send_flags, addr, len);
212 fcntl (fd, F_SETFL, fdflags);