/* Copyright (c) 2016-2018 Chung, Hyung-Hwan. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "hcl-x.h" #include "hcl-prv.h" #include "hcl-tmr.h" #include "hcl-xutl.h" #include #include #include #define HCL_SERVER_TOKEN_NAME_ALIGN 64 #define HCL_SERVER_WID_MAP_ALIGN 512 #define HCL_XPROTO_REPLY_BUF_SIZE 1300 #if defined(_WIN32) # include # include #elif defined(__OS2__) # define INCL_DOSMODULEMGR # define INCL_DOSPROCESS # define INCL_DOSERRORS # include #elif defined(__DOS__) # include # include # include #elif defined(macintosh) # include #else # if defined(HAVE_TIME_H) # include # endif # if defined(HAVE_SYS_TIME_H) # include # endif # if defined(HAVE_SIGNAL_H) # include # endif # if defined(HAVE_SYS_MMAN_H) # include # endif # if defined(HAVE_SYS_UIO_H) # include # endif # if defined(HAVE_SYS_EPOLL_H) # include # endif # include # include # include # include # include # include # include #endif struct client_hcl_xtn_t { hcl_client_t* client; }; typedef struct client_hcl_xtn_t client_hcl_xtn_t; struct hcl_client_t { hcl_oow_t _instsize; hcl_mmgr_t* _mmgr; hcl_cmgr_t* _cmgr; hcl_client_prim_t prim; hcl_t* dummy_hcl; hcl_errnum_t errnum; struct { hcl_ooch_t buf[HCL_ERRMSG_CAPA]; hcl_oow_t len; } errmsg; struct { hcl_bitmask_t trait; hcl_bitmask_t logmask; } cfg; }; /* ========================================================================= */ static void client_log_write_for_dummy (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len) { client_hcl_xtn_t* xtn = (client_hcl_xtn_t*)hcl_getxtn(hcl); hcl_client_t* client; client = xtn->client; client->prim.log_write (client, mask, msg, len); } hcl_client_t* hcl_client_open (hcl_mmgr_t* mmgr, hcl_oow_t xtnsize, hcl_client_prim_t* prim, hcl_errnum_t* errnum) { hcl_client_t* client; hcl_t* hcl; client_hcl_xtn_t* xtn; client = (hcl_client_t*)HCL_MMGR_ALLOC(mmgr, HCL_SIZEOF(*client) + xtnsize); if (!client) { if (errnum) *errnum = HCL_ESYSMEM; return HCL_NULL; } hcl = hcl_openstdwithmmgr(mmgr, HCL_SIZEOF(*xtn), errnum); if (!hcl) { HCL_MMGR_FREE (mmgr, client); return HCL_NULL; } /* replace the vmprim.log_write function */ hcl->vmprim.log_write = client_log_write_for_dummy; xtn = (client_hcl_xtn_t*)hcl_getxtn(hcl); xtn->client = client; HCL_MEMSET (client, 0, HCL_SIZEOF(*client) + xtnsize); client->_instsize = HCL_SIZEOF(*client); client->_mmgr = mmgr; client->_cmgr = hcl_get_utf8_cmgr(); client->prim = *prim; client->dummy_hcl = hcl; client->cfg.logmask = ~(hcl_bitmask_t)0; /* the dummy hcl is used for this client to perform primitive operations * such as getting system time or logging. so the heap size doesn't * need to be changed from the tiny value set above. */ hcl_setoption (client->dummy_hcl, HCL_LOG_MASK, &client->cfg.logmask); hcl_setcmgr (client->dummy_hcl, client->_cmgr); return client; } void hcl_client_close (hcl_client_t* client) { hcl_close (client->dummy_hcl); HCL_MMGR_FREE (client->_mmgr, client); } int hcl_client_setoption (hcl_client_t* client, hcl_client_option_t id, const void* value) { switch (id) { case HCL_CLIENT_TRAIT: client->cfg.trait = *(const hcl_bitmask_t*)value; return 0; case HCL_CLIENT_LOG_MASK: client->cfg.logmask = *(const hcl_bitmask_t*)value; if (client->dummy_hcl) { /* setting this affects the dummy hcl immediately. * existing hcl instances inside worker threads won't get * affected. new hcl instances to be created later * is supposed to use the new value */ hcl_setoption (client->dummy_hcl, HCL_LOG_MASK, value); } return 0; } hcl_client_seterrnum (client, HCL_EINVAL); return -1; } int hcl_client_getoption (hcl_client_t* client, hcl_client_option_t id, void* value) { switch (id) { case HCL_CLIENT_TRAIT: *(hcl_bitmask_t*)value = client->cfg.trait; return 0; case HCL_CLIENT_LOG_MASK: *(hcl_bitmask_t*)value = client->cfg.logmask; return 0; }; hcl_client_seterrnum (client, HCL_EINVAL); return -1; } void* hcl_client_getxtn (hcl_client_t* client) { return (void*)((hcl_uint8_t*)client + client->_instsize); } hcl_mmgr_t* hcl_client_getmmgr (hcl_client_t* client) { return client->_mmgr; } hcl_cmgr_t* hcl_client_getcmgr (hcl_client_t* client) { return client->_cmgr; } void hcl_client_setcmgr (hcl_client_t* client, hcl_cmgr_t* cmgr) { client->_cmgr = cmgr; } hcl_errnum_t hcl_client_geterrnum (hcl_client_t* client) { return client->errnum; } const hcl_ooch_t* hcl_client_geterrstr (hcl_client_t* client) { return hcl_errnum_to_errstr(client->errnum); } const hcl_ooch_t* hcl_client_geterrmsg (hcl_client_t* client) { if (client->errmsg.len <= 0) return hcl_errnum_to_errstr(client->errnum); return client->errmsg.buf; } void hcl_client_seterrnum (hcl_client_t* client, hcl_errnum_t errnum) { /*if (client->shuterr) return; */ client->errnum = errnum; client->errmsg.len = 0; } void hcl_client_seterrbfmt (hcl_client_t* client, hcl_errnum_t errnum, const hcl_bch_t* fmt, ...) { va_list ap; va_start (ap, fmt); hcl_seterrbfmtv (client->dummy_hcl, errnum, fmt, ap); va_end (ap); HCL_ASSERT (client->dummy_hcl, HCL_COUNTOF(client->errmsg.buf) == HCL_COUNTOF(client->dummy_hcl->errmsg.buf)); client->errnum = errnum; hcl_copy_oochars (client->errmsg.buf, client->dummy_hcl->errmsg.buf, HCL_COUNTOF(client->errmsg.buf)); client->errmsg.len = client->dummy_hcl->errmsg.len; } void hcl_client_seterrufmt (hcl_client_t* client, hcl_errnum_t errnum, const hcl_uch_t* fmt, ...) { va_list ap; va_start (ap, fmt); hcl_seterrufmtv (client->dummy_hcl, errnum, fmt, ap); va_end (ap); HCL_ASSERT (client->dummy_hcl, HCL_COUNTOF(client->errmsg.buf) == HCL_COUNTOF(client->dummy_hcl->errmsg.buf)); client->errnum = errnum; hcl_copy_oochars (client->errmsg.buf, client->dummy_hcl->errmsg.buf, HCL_COUNTOF(client->errmsg.buf)); client->errmsg.len = client->dummy_hcl->errmsg.len; } /* ========================================================================= */ void hcl_client_logbfmt (hcl_client_t* client, hcl_bitmask_t mask, const hcl_bch_t* fmt, ...) { va_list ap; va_start (ap, fmt); hcl_logbfmtv (client->dummy_hcl, mask, fmt, ap); va_end (ap); } void hcl_client_logufmt (hcl_client_t* client, hcl_bitmask_t mask, const hcl_uch_t* fmt, ...) { va_list ap; va_start (ap, fmt); hcl_logufmtv (client->dummy_hcl, mask, fmt, ap); va_end (ap); } /* ========================================================================= */ void* hcl_client_allocmem (hcl_client_t* client, hcl_oow_t size) { void* ptr; ptr = HCL_MMGR_ALLOC(client->_mmgr, size); if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM); return ptr; } void* hcl_client_callocmem (hcl_client_t* client, hcl_oow_t size) { void* ptr; ptr = HCL_MMGR_ALLOC(client->_mmgr, size); if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM); else HCL_MEMSET (ptr, 0, size); return ptr; } void* hcl_client_reallocmem (hcl_client_t* client, void* ptr, hcl_oow_t size) { ptr = HCL_MMGR_REALLOC(client->_mmgr, ptr, size); if (!ptr) hcl_client_seterrnum (client, HCL_ESYSMEM); return ptr; } void hcl_client_freemem (hcl_client_t* client, void* ptr) { HCL_MMGR_FREE (client->_mmgr, ptr); }