Commit f4d139bd authored by sigiesec's avatar sigiesec

Problem: duplicated code in socket-related functions

Solution: extract as_socket_base_t function
parent efa86fe6
......@@ -247,6 +247,16 @@ int zmq_ctx_destroy (void *ctx_)
// Sockets
static zmq::socket_base_t *as_socket_base_t (void *s_)
{
zmq::socket_base_t *s = static_cast<zmq::socket_base_t *> (s_);
if (!s_ || !s->check_tag ()) {
errno = ENOTSOCK;
return NULL;
}
return s;
}
void *zmq_socket (void *ctx_, int type_)
{
if (!ctx_ || !((zmq::ctx_t *) ctx_)->check_tag ()) {
......@@ -260,109 +270,83 @@ void *zmq_socket (void *ctx_, int type_)
int zmq_close (void *s_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
((zmq::socket_base_t*) s_)->close ();
s->close ();
return 0;
}
int zmq_setsockopt (void *s_, int option_, const void *optval_,
size_t optvallen_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->setsockopt (option_, optval_, optvallen_);
return result;
return s->setsockopt (option_, optval_, optvallen_);
}
int zmq_getsockopt (void *s_, int option_, void *optval_, size_t *optvallen_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->getsockopt (option_, optval_, optvallen_);
return result;
return s->getsockopt (option_, optval_, optvallen_);
}
int zmq_socket_monitor (void *s_, const char *addr_, int events_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->monitor (addr_, events_);
return result;
return s->monitor (addr_, events_);
}
int zmq_join (void *s_, const char* group_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->join (group_);
return result;
return s->join (group_);
}
int zmq_leave (void *s_, const char* group_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->leave (group_);
return result;
return s->leave (group_);
}
int zmq_bind (void *s_, const char *addr_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->bind (addr_);
return result;
return s->bind (addr_);
}
int zmq_connect (void *s_, const char *addr_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s->connect (addr_);
return result;
return s->connect (addr_);
}
int zmq_unbind (void *s_, const char *addr_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
return s->term_endpoint (addr_);
}
int zmq_disconnect (void *s_, const char *addr_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
return s->term_endpoint (addr_);
}
......@@ -392,10 +376,9 @@ int zmq_sendmsg (void *s_, zmq_msg_t *msg_, int flags_)
int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq_msg_t msg;
if (zmq_msg_init_size (&msg, len_))
return -1;
......@@ -405,7 +388,6 @@ int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
assert (buf_);
memcpy (zmq_msg_data (&msg), buf_, len_);
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int rc = s_sendmsg (s, &msg, flags_);
if (unlikely (rc < 0)) {
int err = errno;
......@@ -421,16 +403,14 @@ int zmq_send (void *s_, const void *buf_, size_t len_, int flags_)
int zmq_send_const (void *s_, const void *buf_, size_t len_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq_msg_t msg;
int rc = zmq_msg_init_data (&msg, (void *)buf_, len_, NULL, NULL);
if (rc != 0)
return -1;
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
rc = s_sendmsg (s, &msg, flags_);
if (unlikely (rc < 0)) {
int err = errno;
......@@ -454,10 +434,9 @@ int zmq_send_const (void *s_, const void *buf_, size_t len_, int flags_)
//
int zmq_sendiov (void *s_, iovec *a_, size_t count_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
if (unlikely (count_ <= 0 || !a_)) {
errno = EINVAL;
return -1;
......@@ -465,7 +444,6 @@ int zmq_sendiov (void *s_, iovec *a_, size_t count_, int flags_)
int rc = 0;
zmq_msg_t msg;
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
for (size_t i = 0; i < count_; ++i) {
rc = zmq_msg_init_size (&msg, a_[i].iov_len);
......@@ -512,15 +490,13 @@ int zmq_recvmsg (void *s_, zmq_msg_t *msg_, int flags_)
int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq_msg_t msg;
int rc = zmq_msg_init (&msg);
errno_assert (rc == 0);
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int nbytes = s_recvmsg (s, &msg, flags_);
if (unlikely (nbytes < 0)) {
int err = errno;
......@@ -562,17 +538,14 @@ int zmq_recv (void *s_, void *buf_, size_t len_, int flags_)
//
int zmq_recviov (void *s_, iovec *a_, size_t *count_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
if (unlikely (!count_ || *count_ <= 0 || !a_)) {
errno = EINVAL;
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
size_t count = *count_;
int nread = 0;
bool recvmore = true;
......@@ -634,24 +607,18 @@ int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
int zmq_msg_send (zmq_msg_t *msg_, void *s_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s_sendmsg (s, msg_, flags_);
return result;
return s_sendmsg (s, msg_, flags_);
}
int zmq_msg_recv (zmq_msg_t *msg_, void *s_, int flags_)
{
if (!s_ || !((zmq::socket_base_t*) s_)->check_tag ()) {
errno = ENOTSOCK;
zmq::socket_base_t *s = as_socket_base_t (s_);
if (!s)
return -1;
}
zmq::socket_base_t *s = (zmq::socket_base_t *) s_;
int result = s_recvmsg (s, msg_, flags_);
return result;
return s_recvmsg (s, msg_, flags_);
}
int zmq_msg_close (zmq_msg_t *msg_)
......
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