diff options
author | Youness Alaoui <youness.alaoui@collabora.co.uk> | 2014-04-10 23:47:59 -0400 |
---|---|---|
committer | Olivier CrĂȘte <olivier.crete@ocrete.ca> | 2014-05-15 09:44:00 -0400 |
commit | a9ce3510732275d0cd554d8daffe2e3fe4d83fe5 (patch) | |
tree | bfcbad1af568f42ea7c6c97249347f082889a19c /socket | |
parent | ee3c80820e56d883f8f5c62baffa9cf90223c652 (diff) |
Refactor tcp-bsd to ease integration of tcp-act/tcp-passive
Diffstat (limited to 'socket')
-rw-r--r-- | socket/tcp-bsd.c | 64 | ||||
-rw-r--r-- | socket/tcp-bsd.h | 4 |
2 files changed, 42 insertions, 26 deletions
diff --git a/socket/tcp-bsd.c b/socket/tcp-bsd.c index 903cce4..83c962f 100644 --- a/socket/tcp-bsd.c +++ b/socket/tcp-bsd.c @@ -55,7 +55,7 @@ #endif typedef struct { - NiceAddress server_addr; + NiceAddress remote_addr; GQueue send_queue; GMainContext *context; GSource *io_source; @@ -78,6 +78,37 @@ static gboolean socket_send_more (GSocket *gsocket, GIOCondition condition, 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 * nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable) { union { @@ -85,19 +116,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable struct sockaddr addr; } name; NiceSocket *sock; - TcpPriv *priv; GSocket *gsock = NULL; GError *gerr = NULL; gboolean gret = FALSE; GSocketAddress *gaddr; + NiceAddress local_addr; if (addr == NULL) { /* We can't connect a tcp socket with no destination address */ return NULL; } - sock = g_slice_new0 (NiceSocket); - nice_address_copy_to_sockaddr (addr, &name.addr); 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 } if (gsock == NULL) { - g_slice_free (NiceSocket, sock); return NULL; } gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); if (gaddr == NULL) { g_object_unref (gsock); - g_slice_free (NiceSocket, sock); return NULL; } @@ -140,7 +167,6 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable g_error_free (gerr); g_socket_close (gsock, NULL); g_object_unref (gsock); - g_slice_free (NiceSocket, sock); return NULL; } g_error_free (gerr); @@ -149,31 +175,17 @@ nice_tcp_bsd_socket_new (GMainContext *ctx, NiceAddress *addr, gboolean reliable gaddr = g_socket_get_local_address (gsock, NULL); if (gaddr == NULL || !g_socket_address_to_native (gaddr, &name.addr, sizeof (name), NULL)) { - g_slice_free (NiceSocket, sock); g_socket_close (gsock, NULL); g_object_unref (gsock); return NULL; } g_object_unref (gaddr); - nice_address_set_from_sockaddr (&sock->addr, &name.addr); - - sock->priv = priv = g_slice_new0 (TcpPriv); + nice_address_set_from_sockaddr (&local_addr, &name.addr); - if (ctx == NULL) - ctx = g_main_context_default (); - priv->context = g_main_context_ref (ctx); - 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; + sock = nice_tcp_bsd_socket_new_from_gsock (ctx, gsock, &local_addr, addr, + reliable); + g_object_unref (gsock); return sock; } @@ -240,7 +252,7 @@ socket_recv_messages (NiceSocket *sock, } if (recv_messages[i].from) - *recv_messages[i].from = priv->server_addr; + *recv_messages[i].from = priv->remote_addr; if (len <= 0) break; diff --git a/socket/tcp-bsd.h b/socket/tcp-bsd.h index 83681d3..21ffc09 100644 --- a/socket/tcp-bsd.h +++ b/socket/tcp-bsd.h @@ -45,6 +45,10 @@ G_BEGIN_DECLS NiceSocket * 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 #endif /* _TCP_BSD_H */ |