Skip to content

Commit

Permalink
add test, clean up api
Browse files Browse the repository at this point in the history
  • Loading branch information
jthomas43 committed Dec 6, 2024
1 parent dece88f commit 8816a7e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
9 changes: 2 additions & 7 deletions include/udx.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ typedef enum {
UDX_LOOKUP_FAMILY_IPV6 = 2,
} udx_lookup_flags;

typedef enum {
UDX_JOIN_GROUP,
UDX_LEAVE_GROUP
} udx_multicast_membership;

typedef struct udx_lookup_s udx_lookup_t;

typedef struct udx_interface_event_s udx_interface_event_t;
Expand Down Expand Up @@ -424,10 +419,10 @@ int
udx_socket_bind (udx_socket_t *socket, const struct sockaddr *addr, unsigned int flags);

int
udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, udx_multicast_membership membership);
udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, uv_membership membership);

int
udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, udx_multicast_membership membership);
udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, uv_membership membership);

int
udx_socket_set_multicast_loop (udx_socket_t *socket, int on);
Expand Down
10 changes: 4 additions & 6 deletions src/udx.c
Original file line number Diff line number Diff line change
Expand Up @@ -2112,15 +2112,13 @@ udx_socket_getsockname (udx_socket_t *socket, struct sockaddr *name, int *name_l
}

int
udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, udx_multicast_membership membership) {
uv_membership m = membership == UDX_JOIN_GROUP ? UV_JOIN_GROUP : UV_LEAVE_GROUP;
return uv_udp_set_membership(&socket->handle, multicast_addr, interface_addr, m);
udx_socket_set_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, uv_membership membership) {
return uv_udp_set_membership(&socket->handle, multicast_addr, interface_addr, membership);
}

int
udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, udx_multicast_membership membership) {
uv_membership m = membership == UDX_JOIN_GROUP ? UV_JOIN_GROUP : UV_LEAVE_GROUP;
return uv_udp_set_source_membership(&socket->handle, multicast_addr, interface_addr, source_addr, m);
udx_socket_set_source_membership (udx_socket_t *socket, const char *multicast_addr, const char *interface_addr, const char *source_addr, uv_membership membership) {
return uv_udp_set_source_membership(&socket->handle, multicast_addr, interface_addr, source_addr, membership);
}

int
Expand Down
88 changes: 88 additions & 0 deletions test/socket-send-recv-multicast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#include "../include/udx.h"

#define MULTICAST_ADDR "239.255.0.1"

uv_loop_t loop;
udx_t udx;

udx_socket_t asock;
udx_socket_t bsock;
udx_socket_send_t req;

int nclient_received;
int nserver_sent;

bool close_called;

void
on_send (udx_socket_send_t *r, int status) {
assert(&req == r);
assert(status == 0);

nserver_sent++;
}

void
on_recv (udx_socket_t *handle, ssize_t read_len, const uv_buf_t *buf, const struct sockaddr *from) {
assert(buf->len == 5);
assert(buf->len == read_len);
assert(memcmp(buf->base, "hello", 5) == 0);

int e = udx_socket_set_membership(&asock, MULTICAST_ADDR, NULL, UV_LEAVE_GROUP);
assert(e == 0);

nclient_received++;
uv_stop(&loop);
}

int
main () {
int e;

uv_loop_init(&loop);

e = udx_init(&loop, &udx);
assert(e == 0);

e = udx_socket_init(&udx, &asock);
assert(e == 0);

e = udx_socket_init(&udx, &bsock);
assert(e == 0);

struct sockaddr_in baddr;
uv_ip4_addr("0.0.0.0", 8082, &baddr);
e = udx_socket_bind(&bsock, (struct sockaddr *) &baddr, 0);
assert(e == 0);

struct sockaddr_in aaddr;
uv_ip4_addr("0.0.0.0", 8081, &aaddr);
e = udx_socket_bind(&asock, (struct sockaddr *) &aaddr, UV_UDP_REUSEADDR);
assert(e == 0);

// e = udx_socket_set_multicast_loop(&asock, 1);
// assert(e == 0);
e = udx_socket_set_membership(&asock, MULTICAST_ADDR, NULL, UV_JOIN_GROUP);
assert(e == 0);

udx_socket_recv_start(&asock, on_recv);

struct sockaddr_in mcast_addr;
uv_ip4_addr(MULTICAST_ADDR, 8081, &mcast_addr);

uv_buf_t buf = uv_buf_init("hello", 5);
// udx_socket_send(&req, &bsock, &buf, 1, (struct sockaddr *) &aaddr, on_send);

udx_socket_send(&req, &bsock, &buf, 1, (struct sockaddr *) &mcast_addr, on_send);

uv_run(&loop, UV_RUN_DEFAULT);

assert(nserver_sent && nclient_received);

return 0;
}

0 comments on commit 8816a7e

Please sign in to comment.