Commit a9ce3510 authored by Youness Alaoui's avatar Youness Alaoui Committed by Olivier Crête

Refactor tcp-bsd to ease integration of tcp-act/tcp-passive

parent ee3c8082
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
#endif #endif
typedef struct { typedef struct {
NiceAddress server_addr; NiceAddress remote_addr;
GQueue send_queue; GQueue send_queue;
GMainContext *context; GMainContext *context;
GSource *io_source; GSource *io_source;
...@@ -77,6 +77,37 @@ static gboolean socket_is_reliable (NiceSocket *sock); ...@@ -77,6 +77,37 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition, static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition,
gpointer data); gpointer data);
NiceSocket *
nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock,
NiceAddress *local_addr, NiceAddress *remote_addr, gboolean reliable)
{
NiceSocket *sock;
TcpPriv *priv;
g_return_val_if_fail (G_IS_SOCKET (gsock), NULL);
sock = g_slice_new0 (NiceSocket);
sock->priv = priv = g_slice_new0 (TcpPriv);
if (ctx == NULL)
ctx = g_main_context_default ();
priv->context = g_main_context_ref (ctx);
priv->remote_addr = *remote_addr;
priv->error = FALSE;
priv->reliable = reliable;
sock->type = NICE_SOCKET_TYPE_TCP_BSD;
sock->fileno = g_object_ref (gsock);
sock->addr = *local_addr;
sock->send_messages = socket_send_messages;
sock->send_messages_reliable = socket_send_messages_reliable;
sock->recv_messages = socket_recv_messages;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
return sock;
}
NiceSocket * NiceSocket *
nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable) nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable)
{ {
...@@ -85,19 +116,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable ...@@ -85,19 +116,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable
struct sockaddr addr; struct sockaddr addr;
} name; } name;
NiceSocket *sock; NiceSocket *sock;
TcpPriv *priv;
GSocket *gsock = NULL; GSocket *gsock = NULL;
GError *gerr = NULL; GError *gerr = NULL;
gboolean gret = FALSE; gboolean gret = FALSE;
GSocketAddress *gaddr; GSocketAddress *gaddr;
NiceAddress local_addr;
if (addr == NULL) { if (addr == NULL) {
/* We can't connect a tcp socket with no destination address */ /* We can't connect a tcp socket with no destination address */
return NULL; return NULL;
} }
sock = g_slice_new0 (NiceSocket);
nice_address_copy_to_sockaddr (addr, &name.addr); nice_address_copy_to_sockaddr (addr, &name.addr);
if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) { if (name.storage.ss_family == AF_UNSPEC || name.storage.ss_family == AF_INET) {
...@@ -118,14 +147,12 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable ...@@ -118,14 +147,12 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable
} }
if (gsock == NULL) { if (gsock == NULL) {
g_slice_free (NiceSocket, sock);
return NULL; return NULL;
} }
gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name));
if (gaddr == NULL) { if (gaddr == NULL) {
g_object_unref (gsock); g_object_unref (gsock);
g_slice_free (NiceSocket, sock);
return NULL; return NULL;
} }
...@@ -140,7 +167,6 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable ...@@ -140,7 +167,6 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable
g_error_free (gerr); g_error_free (gerr);
g_socket_close (gsock, NULL); g_socket_close (gsock, NULL);
g_object_unref (gsock); g_object_unref (gsock);
g_slice_free (NiceSocket, sock);
return NULL; return NULL;
} }
g_error_free (gerr); g_error_free (gerr);
...@@ -149,31 +175,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable ...@@ -149,31 +175,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable
gaddr = g_socket_get_local_address (gsock, NULL); gaddr = g_socket_get_local_address (gsock, NULL);
if (gaddr == NULL || if (gaddr == NULL ||
!g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) { !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) {
g_slice_free (NiceSocket, sock);
g_socket_close (gsock, NULL); g_socket_close (gsock, NULL);
g_object_unref (gsock); g_object_unref (gsock);
return NULL; return NULL;
} }
g_object_unref (gaddr); g_object_unref (gaddr);
nice_address_set_from_sockaddr (&sock->addr, &name.addr); nice_address_set_from_sockaddr (&local_addr, &name.addr);
sock->priv = priv = g_slice_new0 (TcpPriv);
if (ctx == NULL) sock = nice_tcp_bsd_socket_new_from_gsock (ctx, gsock, &local_addr, addr,
ctx = g_main_context_default (); reliable);
priv->context = g_main_context_ref (ctx); g_object_unref (gsock);
priv->server_addr = *addr;
priv->error = FALSE;
priv->reliable = reliable;
sock->type = NICE_SOCKET_TYPE_TCP_BSD;
sock->fileno = gsock;
sock->send_messages = socket_send_messages;
sock->send_messages_reliable = socket_send_messages_reliable;
sock->recv_messages = socket_recv_messages;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
return sock; return sock;
} }
...@@ -240,7 +252,7 @@ socket_recv_messages (NiceSocket *sock, ...@@ -240,7 +252,7 @@ socket_recv_messages (NiceSocket *sock,
} }
if (recv_messages[i].from) if (recv_messages[i].from)
*recv_messages[i].from = priv->server_addr; *recv_messages[i].from = priv->remote_addr;
if (len <= 0) if (len <= 0)
break; break;
......
...@@ -45,6 +45,10 @@ G_BEGIN_DECLS ...@@ -45,6 +45,10 @@ G_BEGIN_DECLS
NiceSocket * NiceSocket *
nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable); nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable);
NiceSocket *
nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock,
NiceAddress *local_addr, NiceAddress *remote_addr, gboolean reliable);
G_END_DECLS G_END_DECLS
#endif /* _TCP_BSD_H */ #endif /* _TCP_BSD_H */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment