GST_DEBUG_OBJECT (src, "connecting to server");
ret = connect (src->sock_fd.fd, (struct sockaddr *) &src->server_sin,
sizeof (src->server_sin));
+ if (ret)
+ goto connect_failed;
- if (ret) {
- gst_tcp_client_src_stop (GST_BASE_SRC (src));
- switch (errno) {
- case ECONNREFUSED:
- GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
- (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
- return FALSE;
- break;
- default:
- GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
- ("connect to %s:%d failed: %s", src->host, src->port,
- g_strerror (errno)));
- return FALSE;
- break;
- }
- }
+ /* add the socket to the poll */
+ gst_poll_add_fd (src->fdset, &src->sock_fd);
+ gst_poll_fd_ctl_read (src->fdset, &src->sock_fd, TRUE);
return TRUE;
gst_tcp_client_src_stop (GST_BASE_SRC (src));
return FALSE;
}
+connect_failed:
+ {
+ gst_tcp_client_src_stop (GST_BASE_SRC (src));
+ switch (errno) {
+ case ECONNREFUSED:
+ GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
+ (_("Connection to %s:%d refused."), src->host, src->port), (NULL));
+ break;
+ default:
+ GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
+ ("connect to %s:%d failed: %s", src->host, src->port,
+ g_strerror (errno)));
+ break;
+ }
+ return FALSE;
+ }
}
/* close the socket and associated resources
client_sock_fd =
accept (sink->server_sock.fd, (struct sockaddr *) &client_address,
&client_address_len);
- if (client_sock_fd == -1) {
- GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
- ("Could not accept client on server socket %d: %s (%d)",
- sink->server_sock.fd, g_strerror (errno), errno));
- return FALSE;
- }
+ if (client_sock_fd == -1)
+ goto accept_failed;
gst_multi_fd_sink_add (GST_MULTI_FD_SINK (sink), client_sock_fd);
inet_ntoa (client_address.sin_addr), client_sock_fd);
return TRUE;
+
+ /* ERRORS */
+accept_failed:
+ {
+ GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
+ ("Could not accept client on server socket %d: %s (%d)",
+ sink->server_sock.fd, g_strerror (errno), errno));
+ return FALSE;
+ }
}
static void
if (gst_poll_fd_can_read (set, &this->server_sock)) {
/* handle new client connection on server socket */
- if (!gst_tcp_server_sink_handle_server_read (this)) {
- GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
- ("client connection failed: %s", g_strerror (errno)));
- return FALSE;
- }
+ if (!gst_tcp_server_sink_handle_server_read (this))
+ goto connection_failed;
}
return TRUE;
+
+ /* ERRORS */
+connection_failed:
+ {
+ GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL),
+ ("client connection failed: %s", g_strerror (errno)));
+ return FALSE;
+ }
}
static void
GstTCPServerSink *this = GST_TCP_SERVER_SINK (parent);
/* create sending server socket */
- if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) {
- GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
- return FALSE;
- }
+ if ((this->server_sock.fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
+ goto no_socket;
+
GST_DEBUG_OBJECT (this, "opened sending server socket with fd %d",
this->server_sock.fd);
/* make address reusable */
ret = 1;
if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_REUSEADDR,
- (void *) &ret, sizeof (ret)) < 0) {
- gst_tcp_socket_close (&this->server_sock);
- GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
- ("Could not setsockopt: %s", g_strerror (errno)));
- return FALSE;
- }
+ (void *) &ret, sizeof (ret)) < 0)
+ goto reuse_failed;
+
/* keep connection alive; avoids SIGPIPE during write */
ret = 1;
if (setsockopt (this->server_sock.fd, SOL_SOCKET, SO_KEEPALIVE,
- (void *) &ret, sizeof (ret)) < 0) {
- gst_tcp_socket_close (&this->server_sock);
- GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
- ("Could not setsockopt: %s", g_strerror (errno)));
- return FALSE;
- }
+ (void *) &ret, sizeof (ret)) < 0)
+ goto keepalive_failed;
/* name the socket */
memset (&this->server_sin, 0, sizeof (this->server_sin));
GST_DEBUG_OBJECT (this, "binding server socket to address");
ret = bind (this->server_sock.fd, (struct sockaddr *) &this->server_sin,
sizeof (this->server_sin));
-
- if (ret) {
- gst_tcp_socket_close (&this->server_sock);
- switch (errno) {
- default:
- GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
- ("bind on port %d failed: %s", this->server_port,
- g_strerror (errno)));
- return FALSE;
- break;
- }
- }
+ if (ret)
+ goto bind_failed;
/* set the server socket to nonblocking */
fcntl (this->server_sock.fd, F_SETFL, O_NONBLOCK);
GST_DEBUG_OBJECT (this, "listening on server socket %d with queue of %d",
this->server_sock.fd, TCP_BACKLOG);
- if (listen (this->server_sock.fd, TCP_BACKLOG) == -1) {
- gst_tcp_socket_close (&this->server_sock);
- GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
- ("Could not listen on server socket: %s", g_strerror (errno)));
- return FALSE;
- }
+ if (listen (this->server_sock.fd, TCP_BACKLOG) == -1)
+ goto listen_failed;
+
GST_DEBUG_OBJECT (this,
"listened on server socket %d, returning from connection setup",
this->server_sock.fd);
gst_poll_fd_ctl_read (parent->fdset, &this->server_sock, TRUE);
return TRUE;
+
+ /* ERRORS */
+no_socket:
+ {
+ GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE, (NULL), GST_ERROR_SYSTEM);
+ return FALSE;
+ }
+reuse_failed:
+ {
+ gst_tcp_socket_close (&this->server_sock);
+ GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
+ ("Could not setsockopt: %s", g_strerror (errno)));
+ return FALSE;
+ }
+keepalive_failed:
+ {
+ gst_tcp_socket_close (&this->server_sock);
+ GST_ELEMENT_ERROR (this, RESOURCE, SETTINGS, (NULL),
+ ("Could not setsockopt: %s", g_strerror (errno)));
+ return FALSE;
+ }
+listen_failed:
+ {
+ gst_tcp_socket_close (&this->server_sock);
+ GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
+ ("Could not listen on server socket: %s", g_strerror (errno)));
+ return FALSE;
+ }
+bind_failed:
+ {
+ gst_tcp_socket_close (&this->server_sock);
+ switch (errno) {
+ default:
+ GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ, (NULL),
+ ("bind on port %d failed: %s", this->server_port,
+ g_strerror (errno)));
+ break;
+ }
+ return FALSE;
+ }
}
static gboolean