9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
14 #if defined(__linux__) || defined(__APPLE__)
42 int msgNQCreate(
char *host,
int port,
int msglen)
44 struct sockaddr_in me ;
46 struct hostent *hostent ;
50 size =
sizeof(
struct sockaddr_in) ;
51 memset((
char *)&me,0,size) ;
54 me.sin_family = AF_INET ;
55 me.sin_port = htons(port) ;
58 hostent = gethostbyname(host) ;
60 LOG(CRIT,
"Unknown host %s (%s)",host,strerror(errno),0,0,0) ;
65 memcpy(&me.sin_addr.s_addr,*(hostent->h_addr_list),
sizeof(me.sin_addr.s_addr)) ;
68 dsc = socket(AF_INET, SOCK_STREAM, 0) ;
70 LOG(CRIT,
"socket() failed [%s]",strerror(errno),0,0,0,0) ;
75 setsockopt(dsc,SOL_SOCKET, SO_KEEPALIVE, (
char *)&optval,
sizeof(optval)) ;
76 setsockopt(dsc,SOL_SOCKET, SO_REUSEADDR, (
char *)&optval,
sizeof(optval)) ;
79 if(connect(dsc,(
struct sockaddr *)&me,size) < 0) {
80 LOG(CRIT,
"connect() to %s, port %d failed [%s]",host,port,strerror(errno),0,0) ;
86 #if defined(__linux__) || defined(__APPLE__)
87 LOG(DBG,
"Before fcntl") ;
89 ret = fcntl(dsc,F_SETFL, O_NONBLOCK) ;
91 LOG(CRIT,
"fcntl() failed [%s]",strerror(errno),0,0,0,0) ;
98 LOG(DBG,
"Before fcntl...",0,0,0,0,0) ;
101 ret = fcntl(dsc,F_GETFL,&modes) ;
103 LOG(CRIT,
"fcntl() failed [%s]",strerror(errno),0,0,0,0) ;
108 LOG(DBG,
"Before fcntl 0x%X",modes,0,0,0,0) ;
110 ret = fcntl(dsc,F_SETFL, modes|O_NONBLOCK) ;
112 LOG(CRIT,
"fcntl() failed [%s]",strerror(errno),0,0,0,0) ;
132 int msgNQSend(
int dsc,
char *what,
int size,
int timeout,
int prio)
135 struct pollfd pollstruct ;
138 if(timeout == WAIT_FOREVER) timeout = 100000000 ;
140 pollstruct.fd = dsc ;
141 pollstruct.events = POLLOUT ;
148 if(msgNQCheck(dsc)==0) {
149 LOG(ERR,
"Task %d not there...",dsc,0,0,0,0) ;
150 return MSG_Q_NOTASK ;
154 ret = write(dsc,what,size) ;
156 if(errno == EAGAIN) {
160 ret = poll(&pollstruct,1,1000) ;
162 if((timeout % 10) == 0) {
163 LOG(DBG,
"Unable to send to task %d in 10 seconds...",dsc,0,0,0,0) ;
175 LOG(ERR,
"poll() returned (%s)",strerror(errno),0,0,0,0) ;
182 LOG(ERR,
"Can't write to task (%s)",strerror(errno),0,0,0,0) ;
189 if(ret < 0)
return MSG_Q_TIMEOUT ;
192 LOG(ERR,
"Bad size (%d != ret %d) in task %d (%s)",120,ret,dsc,strerror(errno),prio) ;
202 int msgNQReceive(
int dsc,
char *where,
int size,
int timeout)
205 struct pollfd pollstruct ;
210 if(timeout < 0) timeout = 100000000 ;
212 pollstruct.fd = dsc ;
213 #if defined(__linux__) || defined(__APPLE__)
214 pollstruct.events = POLLIN | POLLPRI ;
216 pollstruct.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI ;
222 if(!msgNQCheck(dsc)) {
223 LOG(ERR,
"Task %d not there ...",dsc,0,0,0,0) ;
224 return MSG_Q_NOTASK ;
227 LOG(DBG,
"Before read %d",size,0,0,0,0) ;
229 ret = read(dsc,where,size) ;
230 LOG(DBG,
"After read %d, %d",ret,errno,0,0,0);
233 if(errno == EAGAIN) {
237 ret = poll(&pollstruct,1,1000) ;
239 if((timeout % 10) == 0) {
240 LOG(DBG,
"Unable to rcv. from task %d in 10 seconds...",dsc,0,0,0,0) ;
249 LOG(DBG,
"Signal caught while in rcv. poll() from task %d...",dsc,0,0,0,0) ;
252 LOG(ERR,
"poll() returned (%s)",strerror(errno),0,0,0,0) ;
260 LOG(ERR,
"Can't read from task %d (%s)",dsc,strerror(errno),0,0,0) ;
268 if(ret < 0)
return MSG_Q_TIMEOUT ;
271 LOG(ERR,
"Read returned %d instead of %d - task %d",ret,size,dsc,0,0) ;
283 int msgNQDelete(
int desc)
297 int msgNQCheck(
int dsc)
301 #if defined(__linux__) || defined(__APPLE__)
308 LOG(WARN,
"No such NQueue %d",dsc,0,0,0,0) ;
312 size =
sizeof(optval) ;
315 ret = getsockopt(dsc, SOL_SOCKET, SO_KEEPALIVE, (
char *)&optval, &size) ;
317 LOG(ERR,
"getsockopt() returned error for dsc %d [%s]",dsc,strerror(errno),0,0,0) ;