Browse Source

Provide helpers for writing/selecting on a socket

master
Con Kolivas 11 years ago
parent
commit
e3fef890a0
  1. 68
      src/libckpool.c
  2. 2
      src/libckpool.h

68
src/libckpool.c

@ -739,51 +739,77 @@ out:
return buf; return buf;
} }
int wait_write_select(int sockd, int timeout)
{
tv_t tv_timeout;
fd_set writefds;
int ret;
tv_timeout.tv_sec = timeout;
tv_timeout.tv_usec = 0;
retry:
FD_ZERO(&writefds);
FD_SET(sockd, &writefds);
ret = select(sockd + 1, NULL, &writefds, NULL, &tv_timeout);
if (unlikely(ret < 1 &&interrupted()))
goto retry;
return ret;
}
int write_length(int sockd, const void *buf, int len)
{
int ret, ofs = 0;
if (unlikely(len < 1)) {
LOGWARNING("Invalid write length of %d requested in write_length", len);
return -1;
}
while (len) {
ret = write(sockd, buf + ofs, len);
if (unlikely(ret < 0))
return -1;
ofs += ret;
len -= ret;
}
return ofs;
}
bool send_unix_msg(int sockd, const char *buf) bool send_unix_msg(int sockd, const char *buf)
{ {
tv_t tv_timeout = {1, 0};
uint32_t msglen, len; uint32_t msglen, len;
fd_set writefds; int ret;
int ret, ofs;
if (unlikely(!buf)) {
LOGWARNING("Null message sent to send_unix_msg");
return false;
}
len = strlen(buf); len = strlen(buf);
if (unlikely(!len)) { if (unlikely(!len)) {
LOGWARNING("Zero length message sent to send_unix_msg"); LOGWARNING("Zero length message sent to send_unix_msg");
return false; return false;
} }
msglen = htole32(len); msglen = htole32(len);
FD_ZERO(&writefds); ret = wait_write_select(sockd, 60);
FD_SET(sockd, &writefds); if (unlikely(ret < 1)) {
ret = select(sockd + 1, NULL, &writefds, NULL, &tv_timeout);
if (ret < 1) {
LOGERR("Select1 failed in send_unix_msg"); LOGERR("Select1 failed in send_unix_msg");
return false; return false;
} }
ret = write(sockd, &msglen, 4); ret = write_length(sockd, &msglen, 4);
if (unlikely(ret < 4)) { if (unlikely(ret < 4)) {
LOGERR("Failed to write 4 byte length in send_unix_msg"); LOGERR("Failed to write 4 byte length in send_unix_msg");
return false; return false;
} }
ofs = 0; ret = wait_write_select(sockd, 60);
while (len) { if (unlikely(ret < 1)) {
tv_timeout.tv_sec = 1;
tv_timeout.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET(sockd, &writefds);
ret = select(sockd + 1, NULL, &writefds, NULL, &tv_timeout);
if (ret < 1) {
LOGERR("Select2 failed in send_unix_msg"); LOGERR("Select2 failed in send_unix_msg");
return false; return false;
} }
ret = write(sockd, buf + ofs, len); ret = write_length(sockd, buf, len);
if (unlikely(ret < 0)) { if (unlikely(ret < 0)) {
LOGERR("Failed to write %d bytes in send_unix_msg", len); LOGERR("Failed to write %d bytes in send_unix_msg", len);
return false; return false;
} }
ofs += ret;
len -= ret;
}
return true; return true;
} }

2
src/libckpool.h

@ -270,6 +270,8 @@ int open_unix_client(const char *server_path);
int wait_read_select(int sockd, int timeout); int wait_read_select(int sockd, int timeout);
int read_length(int sockd, void *buf, int len); int read_length(int sockd, void *buf, int len);
char *recv_unix_msg(int sockd); char *recv_unix_msg(int sockd);
int wait_write_select(int sockd, int timeout);
int write_length(int sockd, const void *buf, int len);
bool send_unix_msg(int sockd, const char *buf); bool send_unix_msg(int sockd, const char *buf);
bool send_proc(proc_instance_t *pi, const char *msg); bool send_proc(proc_instance_t *pi, const char *msg);
char *send_recv_proc(proc_instance_t *pi, const char *msg); char *send_recv_proc(proc_instance_t *pi, const char *msg);

Loading…
Cancel
Save