Commit a972e826 authored by Youness Alaoui's avatar Youness Alaoui

new NiceSocket API

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