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 @@
#include <glib.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
nice_socket_recv (
NiceSocket *sock,
......
......@@ -56,39 +56,6 @@ struct _NiceSocket
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
gint
nice_socket_recv (
......@@ -107,6 +74,9 @@ nice_socket_send (
void
nice_socket_free (NiceSocket *sock);
#include "udp-bsd.h"
#include "udp-turn.h"
G_END_DECLS
#endif /* _SOCKET_H */
......
......@@ -123,18 +123,17 @@ socket_close (NiceSocket *sock)
/*** NiceSocketFactory ***/
static gboolean
socket_factory_init_socket (
G_GNUC_UNUSED
NiceSocketFactory *man,
NiceSocket *sock,
NiceAddress *addr)
NiceSocket *
nice_udp_bsd_socket_new (NiceAddress *addr)
{
int sockfd = -1;
struct sockaddr_storage name;
guint name_len = sizeof (name);
NiceSocket *sock = g_slice_new0 (NiceSocket);
(void)man;
if (!sock) {
return NULL;
}
if (addr != NULL)
{
......@@ -187,8 +186,11 @@ socket_factory_init_socket (
#endif
}
if (sockfd == -1)
return FALSE;
if (sockfd == -1) {
g_slice_free (NiceSocket, sock);
return NULL;
}
#ifdef IP_RECVERR
else
{
......@@ -204,17 +206,17 @@ socket_factory_init_socket (
fcntl (sockfd, F_SETFL, fcntl (sockfd, F_GETFL) | O_NONBLOCK);
#endif
if(bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0)
{
close (sockfd);
return FALSE;
}
if(bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0) {
g_slice_free (NiceSocket, sock);
close (sockfd);
return NULL;
}
if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0)
{
close (sockfd);
return FALSE;
}
if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0) {
g_slice_free (NiceSocket, sock);
close (sockfd);
return NULL;
}
nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
......@@ -222,23 +224,5 @@ socket_factory_init_socket (
sock->send = socket_send;
sock->recv = socket_recv;
sock->close = socket_close;
return TRUE;
}
static void
socket_factory_close (
G_GNUC_UNUSED
NiceSocketFactory *man)
{
(void)man;
return sock;
}
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 @@
G_BEGIN_DECLS
void
nice_udp_bsd_socket_factory_init (NiceSocketFactory *man);
NiceSocket *
nice_udp_bsd_socket_new (NiceAddress *addr);
G_END_DECLS
......
......@@ -488,27 +488,22 @@ socket_close (NiceSocket *sock)
/*** NiceSocketFactory ***/
static gboolean
socket_factory_init_socket (
NiceSocketFactory *man,
NiceSocket *sock,
NiceAddress *addr)
{
return FALSE;
}
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)
NICEAPI_EXPORT NiceSocket *
nice_udp_turn_socket_new (
NiceAddress *addr,
NiceSocket *base_socket,
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility,
GStaticRecMutex *mutex)
{
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) {
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
......@@ -552,24 +547,5 @@ nice_udp_turn_create_socket_full (
sock->recv = socket_recv;
sock->close = socket_close;
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 (
gboolean
nice_udp_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer);
gboolean
nice_udp_turn_create_socket_full (
NiceSocketFactory *man,
NiceSocket *sock,
NiceAddress *addr,
NiceSocket *udp_socket,
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility);
NiceSocket *
nice_udp_turn_socket_new (
NiceAddress *addr,
NiceSocket *udp_socket,
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility,
GStaticRecMutex *mutex);
void
nice_udp_turn_socket_factory_init (NiceSocketFactory *man);
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