...
authoralex <[email protected]>
Thu, 31 Mar 2022 17:49:51 +0000 (10:49 -0700)
committeralex <[email protected]>
Thu, 31 Mar 2022 17:49:51 +0000 (10:49 -0700)
src/net/expected.c
src/net/peek.c
src/net/wait.c
test/unit/net.tests.c

index 5916a5fde8f4ea1437cc76bf8aab483b8b18cf18..9a4ba5ba264c6cda5ae09f13b886822442860c9a 100644 (file)
@@ -39,8 +39,5 @@ ssize_t net_expected(struct peer *info, enum peer_message *type) {
                i += buf[j];
        }
 
-       // check whether invalid length/message given
-       if((i==0)&&(buf[4]==0xff)) { return -1; }
-
        return i+4;
 }
index 5617b99a493a854a198604984a025c8e4dd1bf9e..2103c3662f435ef5e0d0c5244283ab03568edbc9 100644 (file)
@@ -1,30 +1,23 @@
 #include<net.h>
 
 int net_peek(struct peer *info, void *p, size_t size) {
-       void *next;
+       int i;
 
        if(NULL==info) { return -1; }
        if(NULL==p) { return -1; }
        if(0==size) { return 0; }
 
-       next = malloc(size);
-       if(NULL==next) { return -1; }
-
-       if(net_wait(info,next,size)<0) { return -1; }
-       if(info->in!=NULL) {
-               /*
-                * incomplete net_wait; p wasn't filled
-                */
-               free(next);
+       if((i = net_wait(info,p,size))<=0) {
+               if(i<0) { return -1; }
                return 0;
        }
 
-       if(0==info->in_size) { return 0; }
-       
-       info->in = next;
-       info->in_size = size;
-
-       memcpy(p,next,size);
+       if(net_cache(
+               &(info->in), /* void **prev */
+               &(info->in_size), /* size_t *prev_size */
+               p, /* void *p */
+               size /* size_t size */
+               )<0) { return -1; }
 
        return 1;
 }
index e400665dac0a5af8366dee970cd1b114cebfcf7d..2c2730b3c72874cf3496a3955ef7b2cf7582bdc5 100644 (file)
@@ -51,10 +51,12 @@ static void *use_cached(struct peer *info,void *p,size_t *size) {
                        /* use up all cached data */
                        memcpy(p,info->in,info->in_size);
                        free(info->in);
-                       info->in = NULL;
 
                        (*size) -= info->in_size;
                        p += info->in_size;
+
+                       info->in = NULL;
+                       info->in_size = 0;
                }
        }
 
index 36bc51b019cba7cced0449a72135316867370573..cf0fa500a7e2af726a60ddfb428daed1973f77be 100644 (file)
@@ -305,20 +305,24 @@ static void net_peek_basic_test() {
        assert(info->in!=NULL);
        assert(info->in_size==5);
        assert(memcmp(info->in,expected,5)==0);
+
        for(size_t i=0;i<5;i++) {
-               assert(buf[i]==0);
+               assert(buf[i]==1);
+               assert(buf[i+5]==0);
        }
-       free(info->in);
-       info->in = NULL;
 
        assert(5==write(write_sock,expected,5));
-       assert(1==net_peek(info,buf,5));
+       assert(1==net_peek(info,&(buf[5]),5));
        assert(info->in!=NULL);
        assert(info->in_size==5);
        assert(memcmp(info->in,expected,5)==0);
-       assert(memcmp(buf,expected,5)==0);
+       assert(memcmp(buf,expected,10)==0);
        free(info->in);
        info->in = NULL;
+
+       peer_free(info);
+       close(read_sock);
+       close(write_sock);
 }
 
 static void net_queue_basic_test() {