17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 * Boston, MA 02111-1307, USA. |
18 * Boston, MA 02111-1307, USA. |
19 */ |
19 */ |
20 |
20 |
21 /** |
21 /** |
22 * SECTION:tcpserversink |
22 * SECTION:element-tcpserversink |
23 * @short_description: a sink that acts as a TCP server and sends data to |
|
24 * multiple clients |
|
25 * @see_also: #multifdsink |
23 * @see_also: #multifdsink |
|
24 * |
|
25 * <refsect2> |
|
26 * <title>Example launch line</title> |
|
27 * |[ |
|
28 * # server: |
|
29 * gst-launch fdsrc fd=1 ! tcpserversink protocol=none port=3000 |
|
30 * # client: |
|
31 * gst-launch tcpclientsrc protocol=none port=3000 ! fdsink fd=2 |
|
32 * ]| |
|
33 * </refsect2> |
26 */ |
34 */ |
27 |
35 |
28 #ifdef HAVE_CONFIG_H |
36 #ifdef HAVE_CONFIG_H |
29 #include "config.h" |
37 #include "config.h" |
30 #endif |
38 #endif |
31 #ifdef __SYMBIAN32__ |
|
32 #include "gst/gst-i18n-plugin.h" |
|
33 #else |
|
34 #include <gst/gst-i18n-plugin.h> |
39 #include <gst/gst-i18n-plugin.h> |
35 #endif |
|
36 #include <string.h> /* memset */ |
40 #include <string.h> /* memset */ |
37 |
41 |
38 #include <sys/ioctl.h> |
42 #include <sys/ioctl.h> |
39 |
43 |
40 #ifdef HAVE_FIONREAD_IN_SYS_FILIO |
44 #ifdef HAVE_FIONREAD_IN_SYS_FILIO |
103 gobject_class->get_property = gst_tcp_server_sink_get_property; |
107 gobject_class->get_property = gst_tcp_server_sink_get_property; |
104 gobject_class->finalize = gst_tcp_server_sink_finalize; |
108 gobject_class->finalize = gst_tcp_server_sink_finalize; |
105 |
109 |
106 g_object_class_install_property (gobject_class, ARG_HOST, |
110 g_object_class_install_property (gobject_class, ARG_HOST, |
107 g_param_spec_string ("host", "host", "The host/IP to send the packets to", |
111 g_param_spec_string ("host", "host", "The host/IP to send the packets to", |
108 TCP_DEFAULT_HOST, G_PARAM_READWRITE)); |
112 TCP_DEFAULT_HOST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
109 g_object_class_install_property (gobject_class, ARG_PORT, |
113 g_object_class_install_property (gobject_class, ARG_PORT, |
110 g_param_spec_int ("port", "port", "The port to send the packets to", |
114 g_param_spec_int ("port", "port", "The port to send the packets to", |
111 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, G_PARAM_READWRITE)); |
115 0, TCP_HIGHEST_PORT, TCP_DEFAULT_PORT, |
|
116 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
112 |
117 |
113 gstmultifdsink_class->init = gst_tcp_server_sink_init_send; |
118 gstmultifdsink_class->init = gst_tcp_server_sink_init_send; |
114 gstmultifdsink_class->wait = gst_tcp_server_sink_handle_wait; |
119 gstmultifdsink_class->wait = gst_tcp_server_sink_handle_wait; |
115 gstmultifdsink_class->close = gst_tcp_server_sink_close; |
120 gstmultifdsink_class->close = gst_tcp_server_sink_close; |
116 gstmultifdsink_class->removed = gst_tcp_server_sink_removed; |
121 gstmultifdsink_class->removed = gst_tcp_server_sink_removed; |
156 client_address_len = 0; |
161 client_address_len = 0; |
157 |
162 |
158 client_sock_fd = |
163 client_sock_fd = |
159 accept (sink->server_sock.fd, (struct sockaddr *) &client_address, |
164 accept (sink->server_sock.fd, (struct sockaddr *) &client_address, |
160 &client_address_len); |
165 &client_address_len); |
161 if (client_sock_fd == -1) { |
166 if (client_sock_fd == -1) |
|
167 goto accept_failed; |
|
168 |
|
169 gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd); |
|
170 |
|
171 GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d", |
|
172 inet_ntoa (client_address.sin_addr), client_sock_fd); |
|
173 |
|
174 return TRUE; |
|
175 |
|
176 /* ERRORS */ |
|
177 accept_failed: |
|
178 { |
162 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL), |
179 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL), |
163 ("Could not accept client on server socket %d: %s (%d)", |
180 ("Could not accept client on server socket %d: %s (%d)", |
164 sink->server_sock.fd, g_strerror (errno), errno)); |
181 sink->server_sock.fd, g_strerror (errno), errno)); |
165 return FALSE; |
182 return FALSE; |
166 } |
183 } |
167 |
|
168 gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd); |
|
169 |
|
170 GST_DEBUG_OBJECT (sink, "added new client ip %s with fd %d", |
|
171 inet_ntoa (client_address.sin_addr), client_sock_fd); |
|
172 |
|
173 return TRUE; |
|
174 } |
184 } |
175 |
185 |
176 static void |
186 static void |
177 gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd) |
187 gst_tcp_server_sink_removed (GstMultiFdSink * sink, int fd) |
178 { |
188 { |
192 { |
202 { |
193 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink); |
203 GstTCPServerSink *this = GST_TCP_SERVER_SINK (sink); |
194 |
204 |
195 if (gst_poll_fd_can_read (set, &this->server_sock)) { |
205 if (gst_poll_fd_can_read (set, &this->server_sock)) { |
196 /* handle new client connection on server socket */ |
206 /* handle new client connection on server socket */ |
197 if (!gst_tcp_server_sink_handle_server_read (this)) { |
207 if (!gst_tcp_server_sink_handle_server_read (this)) |
198 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL), |
208 goto connection_failed; |
199 ("client connection failed: %s", g_strerror (errno))); |
|
200 return FALSE; |
|
201 } |
|
202 } |
209 } |
203 return TRUE; |
210 return TRUE; |
|
211 |
|
212 /* ERRORS */ |
|
213 connection_failed: |
|
214 { |
|
215 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL), |
|
216 ("client connection failed: %s", g_strerror (errno))); |
|
217 return FALSE; |
|
218 } |
204 } |
219 } |
205 |
220 |
206 static void |
221 static void |
207 gst_tcp_server_sink_set_property (GObject * object, guint prop_id, |
222 gst_tcp_server_sink_set_property (GObject * object, guint prop_id, |
208 const GValue * value, GParamSpec * pspec) |
223 const GValue * value, GParamSpec * pspec) |
261 { |
276 { |
262 int ret; |
277 int ret; |
263 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent); |
278 GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent); |
264 |
279 |
265 /* create sending server socket */ |
280 /* create sending server socket */ |
266 if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) { |
281 if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) |
267 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM); |
282 goto no_socket; |
268 return FALSE; |
283 |
269 } |
|
270 GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d", |
284 GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d", |
271 this->server_sock.fd); |
285 this->server_sock.fd); |
272 |
286 |
273 /* make address reusable */ |
287 /* make address reusable */ |
274 ret = 1; |
288 ret = 1; |
275 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR, |
289 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR, |
276 (void *) &ret, sizeof (ret)) < 0) { |
290 (void *) &ret, sizeof (ret)) < 0) |
277 gst_tcp_socket_close (&this->server_sock); |
291 goto reuse_failed; |
278 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL), |
292 |
279 ("Could not setsockopt: %s", g_strerror (errno))); |
|
280 return FALSE; |
|
281 } |
|
282 /* keep connection alive; avoids SIGPIPE during write */ |
293 /* keep connection alive; avoids SIGPIPE during write */ |
283 ret = 1; |
294 ret = 1; |
284 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE, |
295 if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE, |
285 (void *) &ret, sizeof (ret)) < 0) { |
296 (void *) &ret, sizeof (ret)) < 0) |
286 gst_tcp_socket_close (&this->server_sock); |
297 goto keepalive_failed; |
287 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL), |
|
288 ("Could not setsockopt: %s", g_strerror (errno))); |
|
289 return FALSE; |
|
290 } |
|
291 |
298 |
292 /* name the socket */ |
299 /* name the socket */ |
293 memset (&this->server_sin, 0, sizeof (this->server_sin)); |
300 memset (&this->server_sin, 0, sizeof (this->server_sin)); |
294 this->server_sin.sin_family = AF_INET; /* network socket */ |
301 this->server_sin.sin_family = AF_INET; /* network socket */ |
295 this->server_sin.sin_port = htons (this->server_port); /* on port */ |
302 this->server_sin.sin_port = htons (this->server_port); /* on port */ |
297 |
304 |
298 /* bind it */ |
305 /* bind it */ |
299 GST_DEBUG_OBJECT (this, "binding server socket to address"); |
306 GST_DEBUG_OBJECT (this, "binding server socket to address"); |
300 ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin, |
307 ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin, |
301 sizeof (this->server_sin)); |
308 sizeof (this->server_sin)); |
302 |
309 if (ret) |
303 if (ret) { |
310 goto bind_failed; |
|
311 |
|
312 /* set the server socket to nonblocking */ |
|
313 fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK); |
|
314 |
|
315 GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d", |
|
316 this->server_sock.fd, TCP_BACKLOG); |
|
317 if (listen (this->server_sock.fd, TCP_BACKLOG) == -1) |
|
318 goto listen_failed; |
|
319 |
|
320 GST_DEBUG_OBJECT (this, |
|
321 "listened on server socket %d, returning from connection setup", |
|
322 this->server_sock.fd); |
|
323 |
|
324 gst_poll_add_fd (parent->fdset, &this->server_sock); |
|
325 gst_poll_fd_ctl_read (parent->fdset, &this->server_sock, TRUE); |
|
326 |
|
327 return TRUE; |
|
328 |
|
329 /* ERRORS */ |
|
330 no_socket: |
|
331 { |
|
332 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM); |
|
333 return FALSE; |
|
334 } |
|
335 reuse_failed: |
|
336 { |
|
337 gst_tcp_socket_close (&this->server_sock); |
|
338 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL), |
|
339 ("Could not setsockopt: %s", g_strerror (errno))); |
|
340 return FALSE; |
|
341 } |
|
342 keepalive_failed: |
|
343 { |
|
344 gst_tcp_socket_close (&this->server_sock); |
|
345 GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL), |
|
346 ("Could not setsockopt: %s", g_strerror (errno))); |
|
347 return FALSE; |
|
348 } |
|
349 listen_failed: |
|
350 { |
|
351 gst_tcp_socket_close (&this->server_sock); |
|
352 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), |
|
353 ("Could not listen on server socket: %s", g_strerror (errno))); |
|
354 return FALSE; |
|
355 } |
|
356 bind_failed: |
|
357 { |
304 gst_tcp_socket_close (&this->server_sock); |
358 gst_tcp_socket_close (&this->server_sock); |
305 switch (errno) { |
359 switch (errno) { |
306 default: |
360 default: |
307 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), |
361 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), |
308 ("bind on port %d failed: %s", this->server_port, |
362 ("bind on port %d failed: %s", this->server_port, |
309 g_strerror (errno))); |
363 g_strerror (errno))); |
310 return FALSE; |
|
311 break; |
364 break; |
312 } |
365 } |
313 } |
366 return FALSE; |
314 |
367 } |
315 /* set the server socket to nonblocking */ |
|
316 fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK); |
|
317 |
|
318 GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d", |
|
319 this->server_sock.fd, TCP_BACKLOG); |
|
320 if (listen (this->server_sock.fd, TCP_BACKLOG) == -1) { |
|
321 gst_tcp_socket_close (&this->server_sock); |
|
322 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL), |
|
323 ("Could not listen on server socket: %s", g_strerror (errno))); |
|
324 return FALSE; |
|
325 } |
|
326 GST_DEBUG_OBJECT (this, |
|
327 "listened on server socket %d, returning from connection setup", |
|
328 this->server_sock.fd); |
|
329 |
|
330 gst_poll_add_fd (parent->fdset, &this->server_sock); |
|
331 gst_poll_fd_ctl_read (parent->fdset, &this->server_sock, TRUE); |
|
332 |
|
333 return TRUE; |
|
334 } |
368 } |
335 |
369 |
336 static gboolean |
370 static gboolean |
337 gst_tcp_server_sink_close (GstMultiFdSink * parent) |
371 gst_tcp_server_sink_close (GstMultiFdSink * parent) |
338 { |
372 { |