From 848f44266dd4b9213af632d55747d66f03881422 Mon Sep 17 00:00:00 2001
From: Uros Majstorovic <majstor@majstor.org>
Date: Wed, 6 Dec 2017 07:17:24 +0100
Subject: refactoring for memory limited systems

---
 code/test/basic.c      | 21 +++++++++------------
 code/test/client.c     | 13 ++++++-------
 code/test/echo.c       |  7 +++++--
 code/test/frag.c       |  8 ++++----
 code/test/server.c     |  8 +++-----
 code/test/stress.c     | 49 +++++++++++++++++++++++++++++++++++++------------
 code/test/vc_client.c  | 13 ++++++-------
 code/test/vc_server.c  | 12 +++++-------
 code/test/vid/Makefile |  4 ++--
 code/test/vid/client.c |  6 +++++-
 code/test/voip.c       |  6 +++---
 11 files changed, 85 insertions(+), 62 deletions(-)

(limited to 'code/test')

diff --git a/code/test/basic.c b/code/test/basic.c
index 5627fd3..85c4f31 100644
--- a/code/test/basic.c
+++ b/code/test/basic.c
@@ -20,40 +20,37 @@ ECPConnection conn;
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
     
-    ecp_conn_handle_open(conn, sq, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
     }
     
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "PERA JE CAR!";
+    unsigned char buf[1000];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000);
+
     return s;
 }
 
-ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG C:%s size:%ld\n", p, s);
     return s;
 }
 
-ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG S:%s size:%ld\n", p, s);
 
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "VAISTINU JE CAR!";
+    unsigned char buf[1000];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000);
 
     return s;
 }
diff --git a/code/test/client.c b/code/test/client.c
index edde70b..03b459f 100644
--- a/code/test/client.c
+++ b/code/test/client.c
@@ -16,26 +16,25 @@ ECPConnection conn;
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
     
-    ecp_conn_handle_open(conn, sq, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
     }
     
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "PERA JE CAR!";
+    unsigned char buf[1000];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000);
+
     return s;
 }
 
-ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG C:%s size:%ld\n", p, s);
     return s;
 }
diff --git a/code/test/echo.c b/code/test/echo.c
index aa27529..ff9c01c 100644
--- a/code/test/echo.c
+++ b/code/test/echo.c
@@ -14,8 +14,11 @@ ECPConnHandler handler_s;
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
-    ssize_t rv = ecp_pld_send(conn, p-ECP_SIZE_PLD_HDR-1, ECP_SIZE_PLD_HDR+1+s);
+ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
+    unsigned char buf[ECP_MAX_PLD];
+
+    memcpy(buf, p, s);
+    ssize_t _rv = ecp_send(conn, t, buf, s);
 
     return s;
 }
diff --git a/code/test/frag.c b/code/test/frag.c
index cbc8cdf..6e7f6be 100644
--- a/code/test/frag.c
+++ b/code/test/frag.c
@@ -25,10 +25,10 @@ unsigned char frag_buffer[8192];
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
     
-    ecp_conn_handle_open(conn, sq, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
@@ -42,7 +42,7 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign
     return s;
 }
 
-ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG C:%s size:%ld\n", p, s);
     
     ECPRBuffer *rbuf = &conn->rbuf.recv->rbuf;
@@ -50,7 +50,7 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
     return s;
 }
 
-ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG S:%s size:%ld\n", p, s);
 
     unsigned char content[5000];
diff --git a/code/test/server.c b/code/test/server.c
index 1b2e2b1..e2001eb 100644
--- a/code/test/server.c
+++ b/code/test/server.c
@@ -14,16 +14,14 @@ ECPConnHandler handler_s;
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG S:%s size:%ld\n", p, s);
 
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "VAISTINU JE CAR!";
+    unsigned char buf[1000];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000);
 
     return s;
 }
diff --git a/code/test/stress.c b/code/test/stress.c
index 0e46f84..d6bc74a 100644
--- a/code/test/stress.c
+++ b/code/test/stress.c
@@ -77,7 +77,15 @@ static void catchINFO(int sig) {
 
 void *sender(ECPConnection *c) {
     int idx = (int)(c->conn_data);
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
+    ECPBuffer packet;
+    ECPBuffer payload;
+    unsigned char pkt_buf[ECP_MAX_PKT];
+    unsigned char pld_buf[ECP_MAX_PLD];
+    
+    packet.buffer = pkt_buf;
+    packet.size = ECP_MAX_PKT;
+    payload.buffer = pld_buf;
+    payload.size = ECP_MAX_PLD;
     
     printf("OPEN:%d\n", idx);
     while(1) {
@@ -85,8 +93,8 @@ void *sender(ECPConnection *c) {
         c->sock->ctx->rng(&rnd, sizeof(uint32_t));
         usleep(rnd % (2000000/msg_rate));
 
-        ecp_pld_set_type(payload, MTYPE_MSG);
-        ssize_t _rv = ecp_pld_send(c, payload, sizeof(payload));
+        ecp_pld_set_type(pld_buf, MTYPE_MSG);
+        ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0));
         if (c_start && (_rv > 0)) {
             pthread_mutex_lock(&t_mtx[idx]);
             t_sent[idx]++;
@@ -95,11 +103,11 @@ void *sender(ECPConnection *c) {
     }
 }
 
-ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     int idx = (int)(conn->conn_data);
     int rv = 0;
     
-    ecp_conn_handle_open(conn, sq, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     rv = pthread_create(&s_thd[idx], NULL, (void *(*)(void *))sender, (void *)conn);
     if (rv) {
         char msg[256];
@@ -110,9 +118,17 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign
     return s;
 }
 
-ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     int idx = (int)(conn->conn_data);
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
+    ECPBuffer packet;
+    ECPBuffer payload;
+    unsigned char pkt_buf[ECP_MAX_PKT];
+    unsigned char pld_buf[ECP_MAX_PLD];
+    
+    packet.buffer = pkt_buf;
+    packet.size = ECP_MAX_PKT;
+    payload.buffer = pld_buf;
+    payload.size = ECP_MAX_PLD;
 
     if (c_start) {
         pthread_mutex_lock(&t_mtx[idx]);
@@ -121,14 +137,23 @@ ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigne
     }
     
     // ecp_pld_set_type(payload, MTYPE_MSG);
-    // ssize_t _rv = ecp_pld_send(c, payload, sizeof(payload));
+    // ssize_t _rv = ecp_pld_send(c, &packet, &payload, ECP_SIZE_PLD(1000, 0));
     return s;
 }
 
-ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    ecp_pld_set_type(payload, MTYPE_MSG);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+ssize_t handle_msg_s(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
+    ECPBuffer packet;
+    ECPBuffer payload;
+    unsigned char pkt_buf[ECP_MAX_PKT];
+    unsigned char pld_buf[ECP_MAX_PLD];
+    
+    packet.buffer = pkt_buf;
+    packet.size = ECP_MAX_PKT;
+    payload.buffer = pld_buf;
+    payload.size = ECP_MAX_PLD;
+
+    ecp_pld_set_type(pld_buf, MTYPE_MSG);
+    ssize_t _rv = ecp_pld_send(conn, &packet, &payload, ECP_SIZE_PLD(1000, 0));
     return s;
 }
 
diff --git a/code/test/vc_client.c b/code/test/vc_client.c
index eb23d05..9ea3d1b 100644
--- a/code/test/vc_client.c
+++ b/code/test/vc_client.c
@@ -20,10 +20,10 @@ ECPNode vconn_node[20];
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
     
-    ecp_conn_handle_open(conn, sq, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
@@ -31,17 +31,16 @@ ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned
     
     printf("OPEN!\n");
 
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "PERA JE CAR!";
+    unsigned char buf[1156];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1156);
+
     return s;
 }
 
-ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG S:%s size:%ld\n", p, s);
     return s;
 }
diff --git a/code/test/vc_server.c b/code/test/vc_server.c
index 455f1a0..8525ad4 100644
--- a/code/test/vc_server.c
+++ b/code/test/vc_server.c
@@ -18,21 +18,19 @@ ECPConnection conn;
 #define CTYPE_TEST  0
 #define MTYPE_MSG   8
 
-ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("OPEN RECEIVED\n");
-    return ecp_conn_handle_open(conn, sq, t, p, s);
+    return ecp_conn_handle_open(conn, sq, t, p, s, b);
 }
 
-ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     printf("MSG S:%s size:%ld\n", p, s);
 
-    unsigned char payload[ECP_SIZE_PLD(1000, 0)];
-    unsigned char *buf = ecp_pld_get_buf(payload, 0);
     char *msg = "VAISTINU JE CAR!";
+    unsigned char buf[1000];
 
-    ecp_pld_set_type(payload, MTYPE_MSG);
     strcpy((char *)buf, msg);
-    ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload));
+    ssize_t _rv = ecp_send(conn, MTYPE_MSG, buf, 1000);
 
     return s;
 }
diff --git a/code/test/vid/Makefile b/code/test/vid/Makefile
index 9ef247e..04d5b5c 100644
--- a/code/test/vid/Makefile
+++ b/code/test/vid/Makefile
@@ -1,5 +1,5 @@
-LIBVPX_HOME=/home/majstor/libvpx
-# LIBVPX_HOME=/Users/majstor/src/libvpx
+# LIBVPX_HOME=/home/majstor/libvpx
+LIBVPX_HOME=/Users/majstor/src/libvpx
 CFLAGS=-D_V4L2_KERNEL_ -I/usr/src/linux-headers-$(uname -r) -I$(LIBVPX_HOME) -I/opt/local/include/SDL2 -I../../core -I../../vconn -I../../util
 LDFLAGS=-L$(LIBVPX_HOME) -L/opt/local/lib
 dep=../../core/libecpcore.a ../../core/crypto/libecpcr.a ../../core/htable/libecpht.a ../../core/posix/libecptr.a ../../core/posix/libecptm.a ../../vconn/libecpvconn.a ../../util/libecputil.a ../init.o
diff --git a/code/test/vid/client.c b/code/test/vid/client.c
index 2c32c51..98e95b2 100644
--- a/code/test/vid/client.c
+++ b/code/test/vid/client.c
@@ -37,7 +37,11 @@ ssize_t handle_msg(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned
     vpx_codec_iter_t iter = NULL;
     vpx_image_t *img = NULL;
 
-    if (vpx_codec_decode(&codec, f, (unsigned int)sz, NULL, 0)) die_codec(&codec, "Failed to decode frame.");
+    if (vpx_codec_decode(&codec, f, (unsigned int)sz, NULL, 0)) {
+        fprintf(stderr, "\n%lu\n", sz);
+        fprintf(stderr, "ERROR!\n");
+        // die_codec(&codec, "Failed to decode frame.");
+    }
 
     while ((img = vpx_codec_get_frame(&codec, &iter)) != NULL) {
         if (!vpx_img_write(img, sdl_canvas.yuvBuffer, sdl_canvas.yPlaneSz + 2 * sdl_canvas.uvPlaneSz)) die_codec(NULL, "Failed to write image.");
diff --git a/code/test/voip.c b/code/test/voip.c
index 4c3d30b..ad5032b 100644
--- a/code/test/voip.c
+++ b/code/test/voip.c
@@ -125,10 +125,10 @@ int a_init(void) {
     return ECP_OK;
 }
 
-ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
     uint32_t seq = 0;
     
-    ecp_conn_handle_open(conn, t, p, s);
+    ecp_conn_handle_open(conn, sq, t, p, s, b);
     if (s < 0) {
         printf("OPEN ERR:%ld\n", s);
         return s;
@@ -139,7 +139,7 @@ ssize_t handle_open_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsign
     return s;
 }
 
-ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s) {
+ssize_t handle_msg_c(ECPConnection *conn, ecp_seq_t sq, unsigned char t, unsigned char *p, ssize_t s, ECP2Buffer *b) {
 	a_write(opus_dec, p, s, handle_plb, alsa_out_buf, alsa_frames);
     return s;
 }
-- 
cgit v1.2.3