Browse Source

Create a helper struct connsock for reading data

master
Con Kolivas 11 years ago
parent
commit
1ee3a8a8a5
  1. 20
      src/libckpool.c
  2. 12
      src/libckpool.h

20
src/libckpool.c

@ -416,7 +416,7 @@ out:
/* Peek in a socket, and then receive only one line at a time, allocing enough /* Peek in a socket, and then receive only one line at a time, allocing enough
* memory in *buf */ * memory in *buf */
int read_socket_line(int fd, void **buf) int read_socket_line(connsock_t *cs)
{ {
char readbuf[PAGESIZE], *eom = NULL; char readbuf[PAGESIZE], *eom = NULL;
size_t buflen = 0, bufofs = 0; size_t buflen = 0, bufofs = 0;
@ -424,11 +424,11 @@ int read_socket_line(int fd, void **buf)
int ret, bufsiz; int ret, bufsiz;
fd_set rd; fd_set rd;
*buf = NULL; cs->buf = NULL;
retry: retry:
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(fd, &rd); FD_SET(cs->fd, &rd);
ret = select(fd + 1, &rd, NULL, NULL, &timeout); ret = select(cs->fd + 1, &rd, NULL, NULL, &timeout);
if (ret < 0 && interrupted()) if (ret < 0 && interrupted())
goto retry; goto retry;
if (ret < 1) { if (ret < 1) {
@ -443,7 +443,7 @@ retry:
while (!eom) { while (!eom) {
int extralen; int extralen;
ret = recv(fd, readbuf, bufsiz - 2, MSG_PEEK); ret = recv(cs->fd, readbuf, bufsiz - 2, MSG_PEEK);
if (ret < 1) { if (ret < 1) {
LOGNOTICE("Failed to recv in read_socket_line"); LOGNOTICE("Failed to recv in read_socket_line");
goto out; goto out;
@ -455,10 +455,10 @@ retry:
extralen = ret; extralen = ret;
buflen += extralen + 1; buflen += extralen + 1;
align_len(&buflen); align_len(&buflen);
*buf = realloc(*buf, buflen); cs->buf = realloc(cs->buf, buflen);
if (unlikely(!*buf)) if (unlikely(!cs->buf))
quit(1, "Failed to alloc buf of %d bytes in read_socket_line", (int)buflen); 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) { if (ret != extralen) {
LOGNOTICE("Failed to recv %d bytes in read_socket_line", (int)buflen); LOGNOTICE("Failed to recv %d bytes in read_socket_line", (int)buflen);
ret = -1; ret = -1;
@ -466,12 +466,12 @@ retry:
} }
bufofs += ret; bufofs += ret;
} }
eom = *buf + bufofs; eom = cs->buf + bufofs;
eom[0] = '\0'; eom[0] = '\0';
ret = bufofs + 1; ret = bufofs + 1;
out: out:
if (ret < 1) if (ret < 1)
dealloc(buf); dealloc(cs->buf);
return ret; return ret;
} }

12
src/libckpool.h

@ -89,6 +89,16 @@ struct cklock {
typedef struct cklock cklock_t; 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_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_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); 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); void block_socket(int fd);
int connect_socket(char *url, char *port); int connect_socket(char *url, char *port);
int write_socket(int fd, const void *buf, size_t nbyte); 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 align_len(size_t *len);
void realloc_strcat(char **ptr, const char *s); void realloc_strcat(char **ptr, const char *s);

Loading…
Cancel
Save