From 1ee3a8a8a53d74719edf905652b372d3fcbfa637 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 12 Apr 2014 09:28:03 +1000 Subject: [PATCH] Create a helper struct connsock for reading data --- src/libckpool.c | 20 ++++++++++---------- src/libckpool.h | 12 +++++++++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index 20566ca6..72d20b4e 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -416,7 +416,7 @@ out: /* Peek in a socket, and then receive only one line at a time, allocing enough * memory in *buf */ -int read_socket_line(int fd, void **buf) +int read_socket_line(connsock_t *cs) { char readbuf[PAGESIZE], *eom = NULL; size_t buflen = 0, bufofs = 0; @@ -424,11 +424,11 @@ int read_socket_line(int fd, void **buf) int ret, bufsiz; fd_set rd; - *buf = NULL; + cs->buf = NULL; retry: FD_ZERO(&rd); - FD_SET(fd, &rd); - ret = select(fd + 1, &rd, NULL, NULL, &timeout); + FD_SET(cs->fd, &rd); + ret = select(cs->fd + 1, &rd, NULL, NULL, &timeout); if (ret < 0 && interrupted()) goto retry; if (ret < 1) { @@ -443,7 +443,7 @@ retry: while (!eom) { int extralen; - ret = recv(fd, readbuf, bufsiz - 2, MSG_PEEK); + ret = recv(cs->fd, readbuf, bufsiz - 2, MSG_PEEK); if (ret < 1) { LOGNOTICE("Failed to recv in read_socket_line"); goto out; @@ -455,10 +455,10 @@ retry: extralen = ret; buflen += extralen + 1; align_len(&buflen); - *buf = realloc(*buf, buflen); - if (unlikely(!*buf)) + cs->buf = realloc(cs->buf, buflen); + if (unlikely(!cs->buf)) quit(1, "Failed to alloc buf of %d bytes in read_socket_line", (int)buflen); - ret = recv(fd, *buf + bufofs, extralen, 0); + ret = recv(cs->fd, cs->buf + bufofs, extralen, 0); if (ret != extralen) { LOGNOTICE("Failed to recv %d bytes in read_socket_line", (int)buflen); ret = -1; @@ -466,12 +466,12 @@ retry: } bufofs += ret; } - eom = *buf + bufofs; + eom = cs->buf + bufofs; eom[0] = '\0'; ret = bufofs + 1; out: if (ret < 1) - dealloc(buf); + dealloc(cs->buf); return ret; } diff --git a/src/libckpool.h b/src/libckpool.h index c3b2f1f8..a7c03a34 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -89,6 +89,16 @@ struct cklock { typedef struct cklock cklock_t; +struct connsock { + int fd; + char *url; + char *port; + char *auth; + char *buf; +}; + +typedef struct connsock connsock_t; + void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line); @@ -143,7 +153,7 @@ void noblock_socket(int fd); void block_socket(int fd); int connect_socket(char *url, char *port); int write_socket(int fd, const void *buf, size_t nbyte); -int read_socket_line(int fd, void **buf); +int read_socket_line(connsock_t *cs); void align_len(size_t *len); void realloc_strcat(char **ptr, const char *s);