diff options
author | Jean-Baptiste Maillet <jean-baptiste.maillet@parrot.com> | 2014-11-27 11:10:27 +0100 |
---|---|---|
committer | Jean-Baptiste Maillet <jean-baptiste.maillet@parrot.com> | 2014-11-27 11:10:27 +0100 |
commit | 3be37c4a5390312c3f1ede00d0dcea3501491630 (patch) | |
tree | 47e4aaf91fae6e8fc542342ae6a8680ab75e811b /examples/mrp_client | |
parent | 1201e3b82907254dcf4d4e4a63368008e3d0835e (diff) | |
download | Open-AVB-3be37c4a5390312c3f1ede00d0dcea3501491630.tar.gz |
mrpdclient: API change, un-globify the socket connection to mrpd
mrpdclient_init returns the socket:
- No need to have the socket global in mrpdclient lib, and allow its un-globification
in applications.
- Applications can use the socket to build a poll/select event loop around it.
- Subsequent calls to mrpdclinet_* take the socket as additional parameter.
- Socket is closed and uninit in mrpdclient_close.
API change applied to mrpctl as the simplest sample code for it.
Note: this commit breaks the build of mrpq and mrpValidate.
Diffstat (limited to 'examples/mrp_client')
-rw-r--r-- | examples/mrp_client/mrpdclient.c | 55 | ||||
-rw-r--r-- | examples/mrp_client/mrpdclient.h | 8 |
2 files changed, 34 insertions, 29 deletions
diff --git a/examples/mrp_client/mrpdclient.c b/examples/mrp_client/mrpdclient.c index 5e20ab84..15fea147 100644 --- a/examples/mrp_client/mrpdclient.c +++ b/examples/mrp_client/mrpdclient.c @@ -60,23 +60,21 @@ static struct timeval rcv_timeout = { #include "mrpd.h" #include "mrpdclient.h" -/* global variables */ -static SOCKET control_socket = SOCKET_ERROR; - int mrpdclient_init(void) { + SOCKET mrpd_sock = SOCKET_ERROR; struct sockaddr_in addr; int rc; - control_socket = socket(AF_INET, SOCK_DGRAM, 0); - if (control_socket == INVALID_SOCKET) + mrpd_sock = socket(AF_INET, SOCK_DGRAM, 0); + if (mrpd_sock == INVALID_SOCKET) goto out; - rc = setsockopt(control_socket, SOL_SOCKET, SO_RCVTIMEO, + rc = setsockopt(mrpd_sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&rcv_timeout, sizeof(rcv_timeout)); if (rc != 0) goto out; - rc = setsockopt(control_socket, SOL_SOCKET, SO_SNDTIMEO, + rc = setsockopt(mrpd_sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&rcv_timeout, sizeof(rcv_timeout)); if (rc != 0) goto out; @@ -85,38 +83,46 @@ int mrpdclient_init(void) addr.sin_family = AF_INET; addr.sin_port = htons(0); - rc = bind(control_socket, (struct sockaddr *)&addr, + rc = bind(mrpd_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)); if (rc <= SOCKET_ERROR) goto out; - return (0); + return mrpd_sock; out: - if (control_socket != SOCKET_ERROR) - closesocket(control_socket); - control_socket = SOCKET_ERROR; - return (-1); + if (mrpd_sock != SOCKET_ERROR) + closesocket(mrpd_sock); + + return SOCKET_ERROR; } -int mrpdclient_close(void) +int mrpdclient_close(SOCKET *mrpd_sock) { - if (control_socket != SOCKET_ERROR) - closesocket(control_socket); + if (NULL == mrpd_sock) + return -1; + + if (SOCKET_ERROR != *mrpd_sock) + closesocket(*mrpd_sock); + *mrpd_sock = SOCKET_ERROR; + return 0; } -int mrpdclient_recv(ptr_process_msg fn) +int mrpdclient_recv(SOCKET mrpd_sock, ptr_process_mrpd_msg fn) { char *msgbuf; int bytes = 0; + if (SOCKET_ERROR == mrpd_sock) + return -1; + msgbuf = (char *)malloc(MRPDCLIENT_MAX_FRAME_SIZE); if (NULL == msgbuf) return -1; memset(msgbuf, 0, MRPDCLIENT_MAX_FRAME_SIZE); - bytes = recv(control_socket, msgbuf, MRPDCLIENT_MAX_FRAME_SIZE, 0); + bytes = recv(mrpd_sock, msgbuf, MRPDCLIENT_MAX_FRAME_SIZE, 0); if (bytes <= SOCKET_ERROR) { goto out; } @@ -127,21 +133,20 @@ int mrpdclient_recv(ptr_process_msg fn) return (-1); } -int mrpdclient_sendto(char *notify_data, int notify_len) +int mrpdclient_sendto(SOCKET mrpd_sock, char *notify_data, int notify_len) { struct sockaddr_in addr; socklen_t addr_len; + if (SOCKET_ERROR == mrpd_sock) + return -1; + memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(MRPD_PORT_DEFAULT); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr_len = sizeof(addr); - if (control_socket != SOCKET_ERROR) - return (sendto - (control_socket, notify_data, notify_len, 0, - (struct sockaddr *)&addr, addr_len)); - else - return (0); + return sendto(mrpd_sock, notify_data, notify_len, 0, + (struct sockaddr *)&addr, addr_len); } diff --git a/examples/mrp_client/mrpdclient.h b/examples/mrp_client/mrpdclient.h index 0c472ca0..8259cbf8 100644 --- a/examples/mrp_client/mrpdclient.h +++ b/examples/mrp_client/mrpdclient.h @@ -37,11 +37,11 @@ #define MRPDCLIENT_MAX_FRAME_SIZE 2000 -typedef int (*ptr_process_msg) (char *buf, int buflen); +typedef int (*ptr_process_mrpd_msg) (char *buf, int buflen); int mrpdclient_init(void); -int mrpdclient_recv(ptr_process_msg fn); -int mrpdclient_sendto(char *notify_data, int notify_len); -int mrpdclient_close(void); +int mrpdclient_recv(SOCKET mrpd_sock, ptr_process_mrpd_msg fn); +int mrpdclient_sendto(SOCKET mrpd_sock, char *notify_data, int notify_len); +int mrpdclient_close(SOCKET *mrpd_sock); #endif |