[vhffs-dev] [1280] The slave is now also using a dynamically allocated buffer for recv. |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 1280
Author: gradator
Date: 2008-10-13 21:26:24 +0200 (Mon, 13 Oct 2008)
Log Message:
-----------
The slave is now also using a dynamically allocated buffer for recv.
Modified Paths:
--------------
trunk/vhffs-fssync/vhffsfssync_master.c
trunk/vhffs-fssync/vhffsfssync_slave.c
Modified: trunk/vhffs-fssync/vhffsfssync_master.c
===================================================================
--- trunk/vhffs-fssync/vhffsfssync_master.c 2008-10-13 18:44:48 UTC (rev 1279)
+++ trunk/vhffs-fssync/vhffsfssync_master.c 2008-10-13 19:26:24 UTC (rev 1280)
@@ -842,8 +842,8 @@
memcpy(conn->delayedevents[conn->delayedevents_end], begin, len);
conn->delayedevents_end++;
}
- begin = cur;
conn->recvbuf_begin += len;
+ begin = cur;
}
if(cur == end) {
@@ -851,10 +851,8 @@
//fprintf(stderr, "Not parsed %d\n", len);
if(len) {
- if(len > VHFFSFSSYNC_NET_RECV_OVERFLOW) {
- vhffsfssync_net_conn_disable(conn);
- break;
- }
+ if(len > VHFFSFSSYNC_NET_RECV_OVERFLOW)
+ return -1;
// copy the data that is not parsed to the begin of the buffer if the data don't overlap
if(len <= conn->recvbuf_begin) {
@@ -1748,7 +1746,8 @@
else {
//fprintf(stdout, "Read %d, buffer is %lu, begin is %lu, end is %lu\n", len, (unsigned long)conn->recvbuf, (unsigned long)conn->recvbuf_begin, (unsigned long)conn->recvbuf_end);
conn->recvbuf_end += len;
- vhffsfssync_net_parse(conn);
+ if( vhffsfssync_net_parse(conn) )
+ vhffsfssync_net_conn_disable(conn);
}
}
Modified: trunk/vhffs-fssync/vhffsfssync_slave.c
===================================================================
--- trunk/vhffs-fssync/vhffsfssync_slave.c 2008-10-13 18:44:48 UTC (rev 1279)
+++ trunk/vhffs-fssync/vhffsfssync_slave.c 2008-10-13 19:26:24 UTC (rev 1280)
@@ -26,8 +26,8 @@
/* -- network stuff -- */
// huge buffer size reduce syscalls
-/* TODO: fullview require a HUGE buffer, change that to a dynamically allocated buffer */
-#define VHFFSFSSYNC_NET_RECV_BUF_LEN 20971520
+#define VHFFSFSSYNC_NET_RECV_CHUNK 65536
+#define VHFFSFSSYNC_NET_RECV_OVERFLOW 104857600
typedef struct {
char *data_buffer;
@@ -39,9 +39,9 @@
int fd;
struct sockaddr_in sockaddr;
- char buf[VHFFSFSSYNC_NET_RECV_BUF_LEN];
- uint32_t buf_len;
- char *buf_cur;
+ char *recvbuf;
+ uint32_t recvbuf_begin;
+ uint32_t recvbuf_end;
FILE *chunk_file;
size_t chunk_stilltoread;
@@ -462,11 +462,12 @@
int vhffsfssync_parse(vhffsfssync_conn *conn) {
char *cur, *end;
- cur = conn->buf;
- end = conn->buf + conn->buf_len; //beware: end can be outside the buffer, you should NOT read *end
-
//printf("Buffer %d, stilltoread: %d\n", conn->buf_len, conn->chunk_stilltoread);
+ /* parse the buffer */
+ cur = conn->recvbuf + conn->recvbuf_begin;
+ end = conn->recvbuf + conn->recvbuf_end; //beware: end can be outside the buffer, you should NOT read *end
+
while(cur < end) {
// text mode
@@ -477,26 +478,33 @@
if( !*(cur-2) && !*(cur-1) ) {
vhffsfssync_event(conn, begin);
+ conn->recvbuf_begin += (cur - begin);
begin = cur;
}
if(cur == end) {
- register uint32_t len;
- len = end - begin;
- //printf("Not parsed %d\n", len);
+ register uint32_t len = end - begin;
+ //fprintf(stderr, "Not parsed %d, begin is %d, end id %d\n", len, conn->recvbuf_begin, conn->recvbuf_end);
- // buffer is full and we didn't manage to fetch everything
- if(len == VHFFSFSSYNC_NET_RECV_BUF_LEN) {
- fprintf(stderr, "The buffer is not large enough, throwing away the content\n");
- conn->buf_cur = conn->buf;
- conn->buf_len = 0;
- break;
+ if(len) {
+ if(len > VHFFSFSSYNC_NET_RECV_OVERFLOW)
+ return -1;
+
+ // copy the data that is not parsed to the begin of the buffer if the data don't overlap
+ if(len <= conn->recvbuf_begin) {
+ //printf("Realloc to %d bytes\n", len);
+ memcpy(conn->recvbuf, begin, len);
+ conn->recvbuf = realloc( conn->recvbuf , len );
+ conn->recvbuf_begin = 0;
+ conn->recvbuf_end = len;
+ }
}
-
- // copy the data that is not parsed to the begin of the buffer
- memcpy(conn->buf, begin, len);
- conn->buf_cur = conn->buf + len;
- conn->buf_len = len;
+ else {
+ free(conn->recvbuf);
+ conn->recvbuf = NULL;
+ conn->recvbuf_begin = 0;
+ conn->recvbuf_end = 0;
+ }
break;
}
}
@@ -528,12 +536,15 @@
// all the data have been read, resetting buffer
if(cur == end) {
- conn->buf_cur = conn->buf;
- conn->buf_len = 0;
+ free(conn->recvbuf);
+ conn->recvbuf = NULL;
+ conn->recvbuf_begin = 0;
+ conn->recvbuf_end = 0;
break;
}
}
}
+
return 0;
}
@@ -562,8 +573,9 @@
signal(SIGPIPE, SIG_IGN);
conn = malloc(sizeof(vhffsfssync_conn));
conn->fd = -1;
- conn->buf_len = 0;
- conn->buf_cur = conn->buf;
+ conn->recvbuf = NULL;
+ conn->recvbuf_begin = 0;
+ conn->recvbuf_end = 0;
conn->chunk_stilltoread = 0;
conn->chunk_file = NULL;
conn->messages = NULL;
@@ -579,10 +591,6 @@
}
conn->fd = -1;
- conn->buf_len = 0;
- conn->buf_cur = conn->buf;
- conn->chunk_stilltoread = 0;
-
if(conn->chunk_file) fclose(conn->chunk_file);
conn->chunk_file = NULL;
@@ -596,6 +604,12 @@
conn->messages_begin = 0;
conn->messages_end = 0;
+ if(conn->recvbuf) free(conn->recvbuf);
+ conn->recvbuf = NULL;
+ conn->recvbuf_begin = 0;
+ conn->recvbuf_end = 0;
+ conn->chunk_stilltoread = 0;
+
/* connect */
inet_aton(host, &conn->sockaddr.sin_addr);
conn->sockaddr.sin_family = AF_INET;
@@ -656,14 +670,16 @@
if(FD_ISSET(conn->fd, &readfs) ) {
ssize_t len;
- len = read(conn->fd, conn->buf_cur, VHFFSFSSYNC_NET_RECV_BUF_LEN - (conn->buf_cur - conn->buf) );
+ //fprintf(stdout, "Alloc %d bytes\n", conn->recvbuf_end + VHFFSFSSYNC_NET_RECV_CHUNK);
+ conn->recvbuf = realloc( conn->recvbuf , conn->recvbuf_end + VHFFSFSSYNC_NET_RECV_CHUNK );
+ len = read(conn->fd, conn->recvbuf+conn->recvbuf_end, VHFFSFSSYNC_NET_RECV_CHUNK);
if(len < 0) {
switch(errno) {
case EAGAIN:
case EINTR:
break;
default:
- fprintf(stderr, "read() failed on socket %d: %s\n", conn->fd, strerror(errno));
+ fprintf(stdout, "read() failed on socket %d: %s\n", conn->fd, strerror(errno));
goto disconnected;
}
}
@@ -671,9 +687,10 @@
goto disconnected;
}
else {
- //printf("Read %d\n", len);
- conn->buf_len += len;
- vhffsfssync_parse(conn);
+ //fprintf(stdout, "Read %d, buffer is %lu, begin is %lu, end is %lu\n", len, (unsigned long)conn->recvbuf, (unsigned long)conn->recvbuf_begin, (unsigned long)conn->recvbuf_end);
+ conn->recvbuf_end += len;
+ if( vhffsfssync_parse(conn) )
+ goto disconnected;
}
}