| 1 |
#include "R_zmq.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
SEXP R_zmq_socket(SEXP R_context, SEXP R_type){ |
| 8 |
SEXP R_socket = R_NilValue; |
| 9 |
int C_type = INTEGER(R_type)[0]; |
| 10 |
void *C_context = R_ExternalPtrAddr(R_context); |
| 11 |
void *C_socket; |
| 12 |
|
| 13 |
if(C_context != NULL){ |
| 14 |
C_socket = zmq_socket(C_context, C_type); |
| 15 |
|
| 16 |
if(C_socket != NULL){ |
| 17 |
PROTECT(R_socket = R_MakeExternalPtr(C_socket, R_NilValue, R_NilValue)); |
| 18 |
|
| 19 |
UNPROTECT(1); |
| 20 |
} else{ |
| 21 |
warning("R_zmq_socket: R_socket is not available.\n"); |
| 22 |
} |
| 23 |
} else{ |
| 24 |
warning("R_zmq_socket: C_context is not available.\n"); |
| 25 |
} |
| 26 |
return(R_socket); |
| 27 |
} |
| 28 |
|
| 29 |
SEXP R_zmq_close(SEXP R_socket){ |
| 30 |
int C_ret = -1, C_errno; |
| 31 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 32 |
|
| 33 |
if(C_socket == NULL){ |
| 34 |
return(R_NilValue); |
| 35 |
} |
| 36 |
|
| 37 |
C_ret = zmq_close(C_socket); |
| 38 |
if(C_ret == -1){ |
| 39 |
C_errno = zmq_errno(); |
| 40 |
Rprintf("R_zmq_socket_close errno: %d strerror: %s\n", |
| 41 |
C_errno, zmq_strerror(C_errno)); |
| 42 |
} |
| 43 |
return(AsInt(C_ret)); |
| 44 |
} |
| 45 |
|
| 46 |
SEXP R_zmq_bind(SEXP R_socket, SEXP R_endpoint){ |
| 47 |
int C_ret = -1, C_errno; |
| 48 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 49 |
const char *C_endpoint = CHARPT(R_endpoint, 0); |
| 50 |
|
| 51 |
if(C_socket != NULL){ |
| 52 |
C_ret = zmq_bind(C_socket, C_endpoint); |
| 53 |
if(C_ret == -1){ |
| 54 |
C_errno = zmq_errno(); |
| 55 |
Rprintf("R_zmq_bind errno: %d strerror: %s\n", |
| 56 |
C_errno, zmq_strerror(C_errno)); |
| 57 |
} |
| 58 |
} else{ |
| 59 |
warning("R_zmq_bind: C_socket is not available.\n"); |
| 60 |
} |
| 61 |
return(AsInt(C_ret)); |
| 62 |
} |
| 63 |
|
| 64 |
SEXP R_zmq_connect(SEXP R_socket, SEXP R_endpoint){ |
| 65 |
int C_ret = -1, C_errno; |
| 66 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 67 |
const char *C_endpoint = CHARPT(R_endpoint, 0); |
| 68 |
|
| 69 |
if(C_socket != NULL){ |
| 70 |
C_ret = zmq_connect(C_socket, C_endpoint); |
| 71 |
if(C_ret == -1){ |
| 72 |
C_errno = zmq_errno(); |
| 73 |
Rprintf("R_zmq_connect errno: %d strerror: %s\n", |
| 74 |
C_errno, zmq_strerror(C_errno)); |
| 75 |
} |
| 76 |
} else{ |
| 77 |
warning("R_zmq_connect: C_socket is not available.\n"); |
| 78 |
} |
| 79 |
return(AsInt(C_ret)); |
| 80 |
} |
| 81 |
|
| 82 |
SEXP R_zmq_disconnect(SEXP R_socket, SEXP R_endpoint){ |
| 83 |
int C_ret = -1, C_errno; |
| 84 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 85 |
const char *C_endpoint = CHARPT(R_endpoint, 0); |
| 86 |
|
| 87 |
if(C_socket != NULL){ |
| 88 |
C_ret = zmq_disconnect(C_socket, C_endpoint); |
| 89 |
if(C_ret == -1){ |
| 90 |
C_errno = zmq_errno(); |
| 91 |
Rprintf("R_zmq_disconnect errno: %d strerror: %s\n", |
| 92 |
C_errno, zmq_strerror(C_errno)); |
| 93 |
} |
| 94 |
} else{ |
| 95 |
warning("R_zmq_disconnect: C_socket is not available.\n"); |
| 96 |
} |
| 97 |
return(AsInt(C_ret)); |
| 98 |
} |
| 99 |
|
| 100 |
SEXP R_zmq_setsockopt(SEXP R_socket, SEXP R_option_name, SEXP R_option_value, |
| 101 |
SEXP R_option_type){ |
| 102 |
int C_ret = -1, C_errno; |
| 103 |
int C_option_name = INTEGER(R_option_name)[0]; |
| 104 |
int C_option_type = INTEGER(R_option_type)[0]; |
| 105 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 106 |
void *C_option_value; |
| 107 |
size_t C_option_len; |
| 108 |
|
| 109 |
if(C_socket != NULL){ |
| 110 |
switch(C_option_type){ |
| 111 |
case 0: |
| 112 |
C_option_value = (void *) CHARPT(R_option_value, 0); |
| 113 |
C_option_len = strlen(C_option_value); |
| 114 |
break; |
| 115 |
case 1: |
| 116 |
C_option_value = (void *) INTEGER(R_option_value); |
| 117 |
C_option_len = sizeof(int); |
| 118 |
break; |
| 119 |
default: |
| 120 |
error("C_option_type: %d is not implemented.\n", |
| 121 |
C_option_type); |
| 122 |
} |
| 123 |
|
| 124 |
C_ret = zmq_setsockopt(C_socket, C_option_name, |
| 125 |
C_option_value, C_option_len); |
| 126 |
if(C_ret == -1){ |
| 127 |
C_errno = zmq_errno(); |
| 128 |
Rprintf("R_zmq_setsockopt errno: %d strerror: %s\n", |
| 129 |
C_errno, zmq_strerror(C_errno)); |
| 130 |
} |
| 131 |
} else{ |
| 132 |
warning("R_zmq_setsockopt: C_socket is not available.\n"); |
| 133 |
} |
| 134 |
return(AsInt(C_ret)); |
| 135 |
} |
| 136 |
|
| 137 |
SEXP R_zmq_getsockopt(SEXP R_socket, SEXP R_option_name, SEXP R_option_value, |
| 138 |
SEXP R_option_type){ |
| 139 |
int C_ret = -1, C_errno; |
| 140 |
int C_option_name = INTEGER(R_option_name)[0]; |
| 141 |
int C_option_type = INTEGER(R_option_type)[0]; |
| 142 |
void *C_socket = R_ExternalPtrAddr(R_socket); |
| 143 |
void *C_option_value; |
| 144 |
size_t C_option_len; |
| 145 |
|
| 146 |
if(C_socket != NULL){ |
| 147 |
switch(C_option_type){ |
| 148 |
case 0: |
| 149 |
C_option_value = (void *) CHARPT(R_option_value, 0); |
| 150 |
C_option_len = strlen(C_option_value); |
| 151 |
break; |
| 152 |
case 1: |
| 153 |
C_option_value = (void *) INTEGER(R_option_value); |
| 154 |
C_option_len = sizeof(int); |
| 155 |
break; |
| 156 |
default: |
| 157 |
error("C_option_type: %d is not implemented.\n", |
| 158 |
C_option_type); |
| 159 |
} |
| 160 |
|
| 161 |
C_ret = zmq_getsockopt(C_socket, C_option_name, |
| 162 |
C_option_value, &C_option_len); |
| 163 |
if(C_ret == -1){ |
| 164 |
C_errno = zmq_errno(); |
| 165 |
Rprintf("R_zmq_getsockopt errno: %d strerror: %s\n", |
| 166 |
C_errno, zmq_strerror(C_errno)); |
| 167 |
} |
| 168 |
} else{ |
| 169 |
warning("R_zmq_getsockopt: C_socket is not available.\n"); |
| 170 |
} |
| 171 |
return(AsInt(C_ret)); |
| 172 |
} |