--- libaitsess/src/sess.c 2011/09/07 13:11:56 1.3 +++ libaitsess/src/sess.c 2011/09/07 15:52:35 1.3.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: sess.c,v 1.3 2011/09/07 13:11:56 misho Exp $ +* $Id: sess.c,v 1.3.2.1 2011/09/07 15:52:35 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -322,4 +322,117 @@ sess_SetValue(tagSess * __restrict s, const char *csAt free(Shared); free(Buffer); return upd | def; +} + + +/* + * sess_prepareSession() Attach to shared memory and de-marshaling data + * @s = Session + * @useDirect = Use direct shared memory if !=0 or snapshot of data to array + * return: NULL error or no data, !=NULL array with variables, + * after use must free resources with sess_doneSession() + */ +array_t * +sess_prepareSession(tagSess * __restrict s, u_char useDirect) +{ + array_t *arr = NULL; + sess_hdr_t *hdr; + int ret; + + assert(s); + if (!s) { + sess_SetErr(EINVAL, "Error:: invalid argument\n"); + return NULL; + } + + ATTACH_MEMORY(s); + if (!s->addr) + return NULL; + else + hdr = (sess_hdr_t*) s->addr; + if (hdr->hdr_magic != SESS_AIT_MAGIC) { + DETACH_MEMORY(s); + + sess_SetErr(EINVAL, "Error:: shared memory not contains values with proper format\n"); + return NULL; + } + + DEC_SEMAPHORE(s, ret); + s->zcopy = useDirect; + arr = io_map2vals(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), + hdr->hdr_argc, useDirect); + ADD_SEMAPHORE(s, ret); + + if (!s->zcopy) + DETACH_MEMORY(s); + return arr; +} + +/* + * sess_doneSession() Free resources allocated with sess_prepareSession() + * @s = Session + * @arr = Array with variables for free + * return: none + */ +void +sess_doneSession(tagSess * __restrict s, array_t ** __restrict arr) +{ + assert(s); + if (!s) { + sess_SetErr(EINVAL, "Error:: invalid argument\n"); + return; + } + + if (!s->zcopy) + io_arrayFree(*arr); + else + DETACH_MEMORY(s); + io_arrayDestroy(arr); +} + +/* + * sess_commitSession() Commit data to shared memory + * @s = Session + * @arr = Array with variables for save + * return -1 error or !=-1 size of stored variables into shared memory + */ +int +sess_commitSession(tagSess * __restrict s, array_t * __restrict arr) +{ + sess_hdr_t *hdr; + int rs, ret = 0; + + assert(s && arr); + if (!s || !arr) { + sess_SetErr(EINVAL, "Error:: invalid argument\n"); + return -1; + } + + if (!s->zcopy) + ATTACH_MEMORY(s); + if (!s->addr) { + if (!s->zcopy) + DETACH_MEMORY(s); + + return -1; + } else + hdr = (sess_hdr_t*) s->addr; + if (hdr->hdr_magic != SESS_AIT_MAGIC) { + if (!s->zcopy) + DETACH_MEMORY(s); + + sess_SetErr(EINVAL, "Error:: shared memory not contains values with proper format\n"); + return -1; + } + + DEC_SEMAPHORE(s, rs); + if ((ret = io_vals2map(s->addr + sizeof(sess_hdr_t), s->eom - sizeof(sess_hdr_t), arr)) != -1) { + hdr->hdr_argc = io_arraySize(arr); + ret += sizeof(sess_hdr_t); + } + ADD_SEMAPHORE(s, rs); + + if (!s->zcopy) + DETACH_MEMORY(s); + return ret; }