tRead struct from server in chunks - synk - synchronize files between hosts
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 91fe72f11a4c218d94a2694ad150ea1ed46edaae
(DIR) parent 726cfff486a2c923d14397e15c8b64f95c7a96a3
(HTM) Author: Willy <willyatmailoodotorg>
Date: Fri, 2 Sep 2016 00:38:15 +0200
Read struct from server in chunks
Structure will be sent from the server to the client in chunks of
RECVSIZ bytes, to deal with data fractionning over TCP.
Diffstat:
M synk.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
---
(DIR) diff --git a/synk.c b/synk.c
t@@ -23,6 +23,7 @@
#define TIMESTAMP_MAX 19 /* length of LONG_MAX */
#define CONNECTION_MAX 1
+#define RECVSIZ 512
/* hold a socket connection, used to pass a connection to a thread */
struct client_t {
t@@ -355,7 +356,7 @@ int
getpeermeta(struct peer_t *clt, struct metadata_t *local)
{
int cfd;
- ssize_t len = 0;
+ ssize_t r, len = 0;
if ((cfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket");
t@@ -367,15 +368,19 @@ getpeermeta(struct peer_t *clt, struct metadata_t *local)
return -1;
}
- if ((len = write(cfd, local, sizeof(struct metadata_t))) < 0) {
+ if (write(cfd, local, sizeof(struct metadata_t)) < 0) {
perror("write");
return -1;
}
/* ... which should return the metadata for this file */
- if ((len = read(cfd, &(clt->meta), sizeof(struct metadata_t))) < 0) {
- perror("read");
- return -1;
+ len = 0;
+ while (len < (ssize_t)sizeof(struct metadata_t)) {
+ if ((r = read(cfd, (unsigned char *)&(clt->meta) + len, RECVSIZ)) < 0) {
+ perror("read");
+ return -1;
+ }
+ len += r;
}
return close(cfd);