Skip to content

Fix Build Without Assert #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 96 additions & 15 deletions src/interfaces/csp_if_zmqhub.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,47 @@ int csp_zmqhub_init_w_endpoints(uint16_t addr,
return_interface);
}

static void csp_zmqhub_driver_destroy(zmq_driver_t ** _drv, const char * const publish_endpoint, const char * const subscribe_endpoint) {

if (*_drv == NULL) {
return;
}

zmq_driver_t * const drv = *_drv;

int ret;
(void)ret; /* Silence unused variable warning (promoted to an error if -Werr) issued when building with NDEBUG (release with asserts turned off) */

if (subscribe_endpoint != NULL) {
ret = zmq_disconnect(drv->subscriber, subscribe_endpoint);
assert(ret == 0);
}

if (publish_endpoint != NULL) {
ret = zmq_disconnect(drv->publisher, publish_endpoint);
assert(ret == 0);
}

if (drv->subscriber) {
ret = zmq_close(drv->subscriber);
assert(ret == 0);
}

if (drv->publisher) {
ret = zmq_close(drv->publisher);
assert(ret == 0);
}

if (drv->context != NULL) {
ret = zmq_ctx_destroy(drv->context);
assert(ret == 0);
}

free(drv);
*_drv = NULL;

}

int csp_zmqhub_init_w_name_endpoints_rxfilter(const char * ifname, uint16_t addr,
const uint16_t rxfilter[], unsigned int rxfilter_count,
const char * publish_endpoint,
Expand All @@ -165,7 +206,9 @@ int csp_zmqhub_init_w_name_endpoints_rxfilter(const char * ifname, uint16_t addr
int ret;
pthread_attr_t attributes;
zmq_driver_t * drv = calloc(1, sizeof(*drv));
assert(drv != NULL);
if (drv == NULL) {
return CSP_ERR_NOMEM;
}

if (ifname == NULL) {
ifname = CSP_ZMQHUB_IF_NAME;
Expand All @@ -178,35 +221,53 @@ int csp_zmqhub_init_w_name_endpoints_rxfilter(const char * ifname, uint16_t addr
drv->iface.addr = addr;

drv->context = zmq_ctx_new();
assert(drv->context != NULL);
if (drv->context == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

//csp_print("INIT %s: pub(tx): [%s], sub(rx): [%s], rx filters: %u", drv->iface.name, publish_endpoint, subscribe_endpoint, rxfilter_count);

/* Publisher (TX) */
drv->publisher = zmq_socket(drv->context, ZMQ_PUB);
assert(drv->publisher != NULL);
if (drv->publisher == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

/* Subscriber (RX) */
drv->subscriber = zmq_socket(drv->context, ZMQ_SUB);
assert(drv->subscriber != NULL);
if (drv->subscriber == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

// subscribe to all packets - no filter
ret = zmq_setsockopt(drv->subscriber, ZMQ_SUBSCRIBE, NULL, 0);
assert(ret == 0);

/* Connect to server */
ret = zmq_connect(drv->publisher, publish_endpoint);
assert(ret == 0);
zmq_connect(drv->subscriber, subscribe_endpoint);
assert(ret == 0);
if (ret) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_TIMEDOUT;
}
ret = zmq_connect(drv->subscriber, subscribe_endpoint);
if (ret) {
csp_zmqhub_driver_destroy(&drv, publish_endpoint, NULL);
return CSP_ERR_TIMEDOUT;
}

/* Start RX thread */
ret = pthread_attr_init(&attributes);
assert(ret == 0);
ret = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
assert(ret == 0);
ret = pthread_create(&drv->rx_thread, &attributes, csp_zmqhub_task, drv);
assert(ret == 0);
if (ret) {
csp_zmqhub_driver_destroy(&drv, publish_endpoint, subscribe_endpoint);
return CSP_ERR_DRIVER;
}

/* Register interface */
csp_iflist_add(&drv->iface);
Expand All @@ -229,7 +290,9 @@ int csp_zmqhub_init_filter2(const char * ifname, const char * host, uint16_t add
int ret;
pthread_attr_t attributes;
zmq_driver_t * drv = calloc(1, sizeof(*drv));
assert(drv != NULL);
if (drv == NULL) {
return CSP_ERR_NOMEM;
}

if (ifname == NULL) {
ifname = CSP_ZMQHUB_IF_NAME;
Expand All @@ -241,17 +304,26 @@ int csp_zmqhub_init_filter2(const char * ifname, const char * host, uint16_t add
drv->iface.nexthop = csp_zmqhub_tx;

drv->context = zmq_ctx_new();
assert(drv->context != NULL);
if (drv->context == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

csp_print(" ZMQ init %s: addr: %u, pub(tx): [%s], sub(rx): [%s]\n", drv->iface.name, addr, pub, sub);

/* Publisher (TX) */
drv->publisher = zmq_socket(drv->context, ZMQ_PUB);
assert(drv->publisher != NULL);
if (drv->publisher == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

/* Subscriber (RX) */
drv->subscriber = zmq_socket(drv->context, ZMQ_SUB);
assert(drv->subscriber != NULL);
if (drv->subscriber == NULL) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_DRIVER;
}

/* If shared secret key provided */
if (sec_key) {
Expand Down Expand Up @@ -290,9 +362,15 @@ int csp_zmqhub_init_filter2(const char * ifname, const char * host, uint16_t add

/* Connect to server */
ret = zmq_connect(drv->publisher, pub);
assert(ret == 0);
if (ret) {
csp_zmqhub_driver_destroy(&drv, NULL, NULL);
return CSP_ERR_TIMEDOUT;
}
ret = zmq_connect(drv->subscriber, sub);
assert(ret == 0);
if (ret) {
csp_zmqhub_driver_destroy(&drv, pub, NULL);
return CSP_ERR_TIMEDOUT;
}


if (promisc) {
Expand Down Expand Up @@ -326,7 +404,10 @@ int csp_zmqhub_init_filter2(const char * ifname, const char * host, uint16_t add
ret = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
assert(ret == 0);
ret = pthread_create(&drv->rx_thread, &attributes, csp_zmqhub_task, drv);
assert(ret == 0);
if (ret) {
csp_zmqhub_driver_destroy(&drv, pub, sub);
return CSP_ERR_DRIVER;
}

/* Register interface */
csp_iflist_add(&drv->iface);
Expand Down
Loading