Commit a972e826 authored by Youness Alaoui's avatar Youness Alaoui

new NiceSocket API

parent 4459912b
...@@ -42,26 +42,60 @@ ...@@ -42,26 +42,60 @@
#include <glib.h> #include <glib.h>
#include <udp.h> #include "socket.h"
#include "udp-bsd.h"
#include "udp-turn.h"
NICEAPI_EXPORT gboolean
nice_udp_socket_factory_make ( NICEAPI_EXPORT NiceSocketFactory *
NiceUDPSocketFactory *man, nice_socket_factory_new (NiceSocketFactoryType type)
NiceUDPSocket *sock,
NiceAddress *addr)
{ {
return man->init (man, sock, addr); 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 NICEAPI_EXPORT void
nice_udp_socket_factory_close (NiceUDPSocketFactory *man) nice_socket_factory_free (NiceSocketFactory *man)
{ {
if (man) {
man->close (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_udp_socket_recv ( nice_socket_recv (
NiceUDPSocket *sock, NiceSocket *sock,
NiceAddress *from, NiceAddress *from,
guint len, guint len,
gchar *buf) gchar *buf)
...@@ -70,8 +104,8 @@ nice_udp_socket_recv ( ...@@ -70,8 +104,8 @@ nice_udp_socket_recv (
} }
NICEAPI_EXPORT void NICEAPI_EXPORT void
nice_udp_socket_send ( nice_socket_send (
NiceUDPSocket *sock, NiceSocket *sock,
const NiceAddress *to, const NiceAddress *to,
guint len, guint len,
const gchar *buf) const gchar *buf)
...@@ -80,8 +114,11 @@ nice_udp_socket_send ( ...@@ -80,8 +114,11 @@ nice_udp_socket_send (
} }
NICEAPI_EXPORT void NICEAPI_EXPORT void
nice_udp_socket_close (NiceUDPSocket *sock) nice_socket_free (NiceSocket *sock)
{ {
if (sock) {
sock->close (sock); sock->close (sock);
g_slice_free (NiceSocket,sock);
}
} }
...@@ -35,71 +35,79 @@ ...@@ -35,71 +35,79 @@
* file under either the MPL or the LGPL. * file under either the MPL or the LGPL.
*/ */
#ifndef _UDP_H #ifndef _SOCKET_H
#define _UDP_H #define _SOCKET_H
#include "address.h" #include "address.h"
G_BEGIN_DECLS G_BEGIN_DECLS
typedef struct _NiceUDPSocket NiceUDPSocket; typedef struct _NiceSocket NiceSocket;
struct _NiceUDPSocket struct _NiceSocket
{ {
NiceAddress addr; NiceAddress addr;
guint fileno; guint fileno;
gint (*recv) (NiceUDPSocket *sock, NiceAddress *from, guint len, gint (*recv) (NiceSocket *sock, NiceAddress *from, guint len,
gchar *buf); gchar *buf);
gboolean (*send) (NiceUDPSocket *sock, const NiceAddress *to, guint len, gboolean (*send) (NiceSocket *sock, const NiceAddress *to, guint len,
const gchar *buf); const gchar *buf);
void (*close) (NiceUDPSocket *sock); void (*close) (NiceSocket *sock);
void *priv; void *priv;
}; };
typedef struct _NiceUDPSocketManager NiceUDPSocketFactory; typedef struct _NiceSocketFactory NiceSocketFactory;
struct _NiceUDPSocketManager struct _NiceSocketFactory
{ {
gboolean (*init) (NiceUDPSocketFactory *man, NiceUDPSocket *sock, gboolean (*init) (NiceSocketFactory *man, NiceSocket *sock,
NiceAddress *sin); NiceAddress *sin);
void (*close) (NiceUDPSocketFactory *man); void (*close) (NiceSocketFactory *man);
void *priv; 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 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 * 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. * address bound to will be set in sock->addr.
*/ */
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
gboolean NiceSocket *
nice_udp_socket_factory_make ( nice_socket_new (
NiceUDPSocketFactory *man, NiceSocketFactory *man,
NiceUDPSocket *sock, NiceAddress *adddr);
NiceAddress *addr);
void
nice_udp_socket_factory_close (NiceUDPSocketFactory *man);
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
gint gint
nice_udp_socket_recv ( nice_socket_recv (
NiceUDPSocket *sock, NiceSocket *sock,
NiceAddress *from, NiceAddress *from,
guint len, guint len,
gchar *buf); gchar *buf);
void void
nice_udp_socket_send ( nice_socket_send (
NiceUDPSocket *sock, NiceSocket *sock,
const NiceAddress *to, const NiceAddress *to,
guint len, guint len,
const gchar *buf); const gchar *buf);
void void
nice_udp_socket_close (NiceUDPSocket *sock); nice_socket_free (NiceSocket *sock);
G_END_DECLS G_END_DECLS
#endif /* _UDP_H */ #endif /* _SOCKET_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