Commit 7f8ff6c2 authored by Youness Alaoui's avatar Youness Alaoui

Much better NiceSocket API now, no more need for a socket factory

parent 8b6568da
...@@ -43,56 +43,8 @@ ...@@ -43,56 +43,8 @@
#include <glib.h> #include <glib.h>
#include "socket.h" #include "socket.h"
#include "udp-bsd.h"
#include "udp-turn.h"
NICEAPI_EXPORT NiceSocketFactory *
nice_socket_factory_new (NiceSocketFactoryType type)
{
NiceSocketFactory *man = g_new0 (NiceSocketFactory, 1);
if (man) {
switch(type) {
case NICE_SOCKET_FACTORY_UDP_BSD:
nice_udp_bsd_socket_factory_init (man);
break;
case NICE_SOCKET_FACTORY_UDP_RELAY:
nice_udp_turn_socket_factory_init (man);
break;
default:
g_free (man);
man = NULL;
}
}
return man;
}
NICEAPI_EXPORT void
nice_socket_factory_free (NiceSocketFactory *man)
{
if (man) {
man->close (man);
g_free (man);
}
}
NICEAPI_EXPORT NiceSocket *
nice_socket_new (
NiceSocketFactory *man,
NiceAddress *addr)
{
NiceSocket *sock = g_slice_new0 (NiceSocket);
if (!man || !sock || !man->init (man, sock, addr)) {
g_free (sock);
sock = NULL;
}
return sock;
}
NICEAPI_EXPORT gint NICEAPI_EXPORT gint
nice_socket_recv ( nice_socket_recv (
NiceSocket *sock, NiceSocket *sock,
......
...@@ -56,39 +56,6 @@ struct _NiceSocket ...@@ -56,39 +56,6 @@ struct _NiceSocket
void *priv; void *priv;
}; };
typedef struct _NiceSocketFactory NiceSocketFactory;
struct _NiceSocketFactory
{
gboolean (*init) (NiceSocketFactory *man, NiceSocket *sock,
NiceAddress *sin);
void (*close) (NiceSocketFactory *man);
void *priv;
};
typedef enum {
NICE_SOCKET_FACTORY_UDP_BSD,
NICE_SOCKET_FACTORY_UDP_RELAY,
} NiceSocketFactoryType;
G_GNUC_WARN_UNUSED_RESULT
NiceSocketFactory *
nice_socket_factory_new (NiceSocketFactoryType type);
void
nice_socket_factory_free (NiceSocketFactory *man);
/**
* If sin is not NULL, the new socket will be bound to that IP address/port.
* If sin->sin_port is 0, a port will be assigned at random. In all cases, the
* address bound to will be set in sock->addr.
*/
G_GNUC_WARN_UNUSED_RESULT
NiceSocket *
nice_socket_new (
NiceSocketFactory *man,
NiceAddress *adddr);
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
gint gint
nice_socket_recv ( nice_socket_recv (
...@@ -107,6 +74,9 @@ nice_socket_send ( ...@@ -107,6 +74,9 @@ nice_socket_send (
void void
nice_socket_free (NiceSocket *sock); nice_socket_free (NiceSocket *sock);
#include "udp-bsd.h"
#include "udp-turn.h"
G_END_DECLS G_END_DECLS
#endif /* _SOCKET_H */ #endif /* _SOCKET_H */
......
...@@ -123,18 +123,17 @@ socket_close (NiceSocket *sock) ...@@ -123,18 +123,17 @@ socket_close (NiceSocket *sock)
/*** NiceSocketFactory ***/ /*** NiceSocketFactory ***/
static gboolean NiceSocket *
socket_factory_init_socket ( nice_udp_bsd_socket_new (NiceAddress *addr)
G_GNUC_UNUSED
NiceSocketFactory *man,
NiceSocket *sock,
NiceAddress *addr)
{ {
int sockfd = -1; int sockfd = -1;
struct sockaddr_storage name; struct sockaddr_storage name;
guint name_len = sizeof (name); guint name_len = sizeof (name);
NiceSocket *sock = g_slice_new0 (NiceSocket);
(void)man; if (!sock) {
return NULL;
}
if (addr != NULL) if (addr != NULL)
{ {
...@@ -187,8 +186,11 @@ socket_factory_init_socket ( ...@@ -187,8 +186,11 @@ socket_factory_init_socket (
#endif #endif
} }
if (sockfd == -1) if (sockfd == -1) {
return FALSE; g_slice_free (NiceSocket, sock);
return NULL;
}
#ifdef IP_RECVERR #ifdef IP_RECVERR
else else
{ {
...@@ -204,17 +206,17 @@ socket_factory_init_socket ( ...@@ -204,17 +206,17 @@ socket_factory_init_socket (
fcntl (sockfd, F_SETFL, fcntl (sockfd, F_GETFL) | O_NONBLOCK); fcntl (sockfd, F_SETFL, fcntl (sockfd, F_GETFL) | O_NONBLOCK);
#endif #endif
if(bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0) if(bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0) {
{ g_slice_free (NiceSocket, sock);
close (sockfd); close (sockfd);
return FALSE; return NULL;
} }
if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0) if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0) {
{ g_slice_free (NiceSocket, sock);
close (sockfd); close (sockfd);
return FALSE; return NULL;
} }
nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name); nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
...@@ -222,23 +224,5 @@ socket_factory_init_socket ( ...@@ -222,23 +224,5 @@ socket_factory_init_socket (
sock->send = socket_send; sock->send = socket_send;
sock->recv = socket_recv; sock->recv = socket_recv;
sock->close = socket_close; sock->close = socket_close;
return TRUE; return sock;
}
static void
socket_factory_close (
G_GNUC_UNUSED
NiceSocketFactory *man)
{
(void)man;
} }
NICEAPI_EXPORT void
nice_udp_bsd_socket_factory_init (
G_GNUC_UNUSED
NiceSocketFactory *man)
{
man->init = socket_factory_init_socket;
man->close = socket_factory_close;
}
...@@ -42,8 +42,8 @@ ...@@ -42,8 +42,8 @@
G_BEGIN_DECLS G_BEGIN_DECLS
void NiceSocket *
nice_udp_bsd_socket_factory_init (NiceSocketFactory *man); nice_udp_bsd_socket_new (NiceAddress *addr);
G_END_DECLS G_END_DECLS
......
...@@ -488,27 +488,22 @@ socket_close (NiceSocket *sock) ...@@ -488,27 +488,22 @@ socket_close (NiceSocket *sock)
/*** NiceSocketFactory ***/ /*** NiceSocketFactory ***/
static gboolean NICEAPI_EXPORT NiceSocket *
socket_factory_init_socket ( nice_udp_turn_socket_new (
NiceSocketFactory *man, NiceAddress *addr,
NiceSocket *sock, NiceSocket *base_socket,
NiceAddress *addr) NiceAddress *server_addr,
{ gchar *username,
return FALSE; gchar *password,
} NiceUdpTurnSocketCompatibility compatibility,
GStaticRecMutex *mutex)
NICEAPI_EXPORT gboolean
nice_udp_turn_create_socket_full (
NiceSocketFactory *man,
NiceSocket *sock,
NiceAddress *addr,
NiceSocket *base_socket,
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility)
{ {
turn_priv *priv = g_new0 (turn_priv, 1); turn_priv *priv = g_new0 (turn_priv, 1);
NiceSocket *sock = g_slice_new0 (NiceSocket);
if (!sock) {
return NULL;
}
if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES, stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
...@@ -552,24 +547,5 @@ nice_udp_turn_create_socket_full ( ...@@ -552,24 +547,5 @@ nice_udp_turn_create_socket_full (
sock->recv = socket_recv; sock->recv = socket_recv;
sock->close = socket_close; sock->close = socket_close;
sock->priv = (void *) priv; sock->priv = (void *) priv;
return TRUE; return sock;
} }
static void
socket_factory_close (
G_GNUC_UNUSED
NiceSocketFactory *man)
{
}
NICEAPI_EXPORT void
nice_udp_turn_socket_factory_init (
G_GNUC_UNUSED
NiceSocketFactory *man)
{
man->init = socket_factory_init_socket;
man->close = socket_factory_close;
}
...@@ -61,20 +61,16 @@ nice_udp_turn_socket_parse_recv ( ...@@ -61,20 +61,16 @@ nice_udp_turn_socket_parse_recv (
gboolean gboolean
nice_udp_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer); nice_udp_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer);
gboolean NiceSocket *
nice_udp_turn_create_socket_full ( nice_udp_turn_socket_new (
NiceSocketFactory *man, NiceAddress *addr,
NiceSocket *sock, NiceSocket *udp_socket,
NiceAddress *addr, NiceAddress *server_addr,
NiceSocket *udp_socket, gchar *username,
NiceAddress *server_addr, gchar *password,
gchar *username, NiceUdpTurnSocketCompatibility compatibility,
gchar *password, GStaticRecMutex *mutex);
NiceUdpTurnSocketCompatibility compatibility);
void
nice_udp_turn_socket_factory_init (NiceSocketFactory *man);
G_END_DECLS G_END_DECLS
......
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