network

low level network subjects
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/network

commit 8f7362a26a9684e16667da03a4e884345e514f12
parent 654cc7ab13ae1b241f323236fc41a722deaad059
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 17 Mar 2017 21:17:16 +0100

Fix(Read / Write): Now working with Hexadecimals:

Replaces the implicits indexes of the callbacks arrays by explicit ones

Diffstat:
Minc/package.h | 26+++++++++++++-------------
Msrc/read_package.c | 41++++++++++++++++++++++++++---------------
Msrc/write_package.c | 47+++++++++++++++++++++++++++++------------------
3 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/inc/package.h b/inc/package.h @@ -29,19 +29,19 @@ typedef struct package_s { /* Type field of the header */ typedef enum package_type_e { - PKG_TYPE_AUTH = 0, - PKG_TYPE_AUTH_ACK, - PKG_TYPE_ERROR, - PKG_TYPE_REQ_GET_PKG, - PKG_TYPE_REQ_GET_FILE, - PKG_TYPE_REQ_GET_NEWS, - PKG_TYPE_REQ_CAT, - PKG_TYPE_REQ_UPD, - PKG_TYPE_RESP_PKG, - PKG_TYPE_RESP_FILE, - PKG_TYPE_RESP_NEWS, - PKG_TYPE_RESP_CAT, - PKG_TYPE_END + PKG_TYPE_AUTH = 0x1, + PKG_TYPE_AUTH_ACK = 0x2, + PKG_TYPE_ERROR = 0x3, + PKG_TYPE_REQ_GET_PKG = 0x10, + PKG_TYPE_REQ_GET_FILE = 0x11, + PKG_TYPE_REQ_GET_NEWS = 0x12, + PKG_TYPE_REQ_CAT = 0x13, + PKG_TYPE_REQ_UPD = 0x14, + PKG_TYPE_RESP_PKG = 0x20, + PKG_TYPE_RESP_FILE = 0x21, + PKG_TYPE_RESP_NEWS = 0x22, + PKG_TYPE_RESP_CAT = 0x23, + PKG_TYPE_END = 0x24 } package_type_t; /* Payloads */ diff --git a/src/read_package.c b/src/read_package.c @@ -4,7 +4,6 @@ static size_t read_header(package_t *pkg, void *data) { size_t count = 0; read_member(pkg->type); - pkg->type--; read_member(pkg->size); read_member(pkg->next_pkg_len); @@ -365,23 +364,35 @@ static size_t read_payload_resp_cat(package_t *pkg, void *data) { } typedef size_t (*payload_callback)(package_t *, void *); -static const payload_callback arr[] = { - &read_payload_auth, - &read_payload_auth_ack, - &read_payload_error, - &read_payload_get_package, - &read_payload_get_file, - &read_payload_get_news, - &read_payload_get_cat, - &read_payload_get_upd, - &read_payload_resp_pkg, - &read_payload_resp_file, - &read_payload_resp_news, - &read_payload_resp_cat +typedef struct callback_s { + u8_t index; + payload_callback fn; +} callback_t; + +static const callback_t arr[] = { + {PKG_TYPE_AUTH, &read_payload_auth}, + {PKG_TYPE_AUTH_ACK, &read_payload_auth_ack}, + {PKG_TYPE_ERROR, &read_payload_error}, + {PKG_TYPE_REQ_GET_PKG, &read_payload_get_package}, + {PKG_TYPE_REQ_GET_FILE, &read_payload_get_file}, + {PKG_TYPE_REQ_GET_NEWS, &read_payload_get_news}, + {PKG_TYPE_REQ_CAT, &read_payload_get_cat}, + {PKG_TYPE_REQ_UPD, &read_payload_get_upd}, + {PKG_TYPE_RESP_PKG, &read_payload_resp_pkg}, + {PKG_TYPE_RESP_FILE, &read_payload_resp_file}, + {PKG_TYPE_RESP_NEWS, &read_payload_resp_news}, + {PKG_TYPE_RESP_CAT, &read_payload_resp_cat} }; static size_t read_payload(package_t *pkg, void *data) { - return arr[pkg->type](pkg, data); + size_t index; + + for (index = 0; index < sizeof(arr) / sizeof(arr[0]); index++) + { + if (pkg->type == arr[index].index) + return arr[index].fn(pkg, data); + } + return 0; } package_t *read_pkg(void *data) { diff --git a/src/write_package.c b/src/write_package.c @@ -2,7 +2,6 @@ static void *write_header(package_t *pkg, size_t *count) { void *header; - u8_t tmp; *count = 0; header = malloc(sizeof(pkg->type) + @@ -12,8 +11,7 @@ static void *write_header(package_t *pkg, size_t *count) { pkg->next_pkg_len); assert(header != NULL); - tmp = pkg->type + 1; - write_member(tmp, header, *count); + write_member(pkg->type, header, *count); write_member(pkg->size, header, *count); write_member(pkg->next_pkg_len, header, *count); if (pkg->next_pkg_len != 0) { @@ -248,29 +246,42 @@ static void *write_payload_resp_cat(void *pkg, size_t *count) { } typedef void *(*write_callback)(void *, size_t *); -static const write_callback arr[] = { - &write_payload_auth, - &write_payload_auth_ack, - &write_payload_error, - &write_payload_req_get_pkg, - &write_payload_req_get_file, - &write_payload_req_get_news, - &write_payload_req_get_cat, - &write_payload_req_get_upd, - &write_payload_resp_pkg, - &write_payload_resp_file, - &write_payload_resp_news, - &write_payload_resp_cat +typedef struct callback_s { + u8_t index; + write_callback fn; +} callback_t; + +static const callback_t arr[] = { + {PKG_TYPE_AUTH, &write_payload_auth}, + {PKG_TYPE_AUTH_ACK, &write_payload_auth_ack}, + {PKG_TYPE_ERROR, &write_payload_error}, + {PKG_TYPE_REQ_GET_PKG, &write_payload_req_get_pkg}, + {PKG_TYPE_REQ_GET_FILE, &write_payload_req_get_file}, + {PKG_TYPE_REQ_GET_NEWS, &write_payload_req_get_news}, + {PKG_TYPE_REQ_CAT, &write_payload_req_get_cat}, + {PKG_TYPE_REQ_UPD, &write_payload_req_get_upd}, + {PKG_TYPE_RESP_PKG, &write_payload_resp_pkg}, + {PKG_TYPE_RESP_FILE, &write_payload_resp_file}, + {PKG_TYPE_RESP_NEWS, &write_payload_resp_news}, + {PKG_TYPE_RESP_CAT, &write_payload_resp_cat} }; void *write_payload(package_t *pkg, size_t *count) { mlist_t *tmp; void *ptr, *payl_tmp, *ret = NULL; - size_t size; + size_t size, index; list_for_each(pkg->payload, tmp, ptr) { size = 0; - payl_tmp = arr[pkg->type](ptr, &size); + for (index = 0; index < sizeof(arr) / sizeof(arr[0]); index++) + { + if (pkg->type == arr[index].index) + { + payl_tmp = arr[index].fn(ptr, &size); + break ; + } + } + assert(index != sizeof(arr) / sizeof(arr[0])); *count += size; ret = realloc(ret, *count); memcpy(ret + (*count - size), payl_tmp, size);