bool socket_add(NetworkInterface* ni, uint32_t ip, uint16_t port, Socket* socket) { Map* sockets = ni_config_get(ni, SOCKETS); if(!sockets) { sockets = map_create(16, NULL, NULL, ni->pool); if(!sockets) { printf("Can'nt create socket table\n"); return false; } if(!ni_config_put(ni, SOCKETS, sockets)) { map_destroy(sockets); return false; } } uint64_t key = (uint64_t)ip << 32 | (uint64_t)port; if(!map_put(sockets, (void*)key, socket)) { if(map_is_empty(sockets)) { map_destroy(sockets); ni_config_remove(ni, SOCKETS); } return false; } return true; }
Service* service_alloc(Endpoint* service_endpoint) { bool service_add(NetworkInterface* ni, Service* service) { Map* services = ni_config_get(ni, SERVICES); if(!services) { services = map_create(16, NULL, NULL, ni->pool); if(!services) return false; if(!ni_config_put(ni, SERVICES, services)) return false; } uint64_t key = (uint64_t)service->endpoint.protocol << 48 | (uint64_t)service->endpoint.addr << 16 | (uint64_t)service->endpoint.port; if(!map_put(services, (void*)key, service)) { if(map_is_empty(services)) { ni_config_remove(ni, SERVICES); map_destroy(services); } return false; } printf("here???\n"); return true; }
/* * returns true if the variable v is not empty (0 for nums) */ int v_is_nonzero(var_t *v) { switch (v->type) { case V_INT: return (v->v.i != 0); case V_NUM: // return (v->v.n != 0.0 && v->v.n != -0.0); return (ABS(v->v.n) > 1E-308); case V_STR: return (v->v.p.size != 0); case V_MAP: return !map_is_empty(v); case V_PTR: return (v->v.ap.p != 0); case V_ARRAY: return (v->v.a.size != 0); }; return 0; }
bool socket_remove(NetworkInterface* ni, uint32_t ip, uint16_t port) { Map* sockets = ni_config_get(ni, SOCKETS); if(!sockets) return false; uint64_t key = (uint64_t)ip << 32 | (uint64_t)port; Socket* socket = map_remove(sockets, (void*)key); if(!socket) return false; socket_delete(ni, socket); if(map_is_empty(sockets)) { map_destroy(sockets); ni_config_remove(ni, SOCKETS); } return true; }
/* * returns true if the user's program must use this var as an empty var * this is usefull for arrays */ int v_isempty(var_t *var) { switch (var->type) { case V_STR: return (strlen(var->v.p.ptr) == 0); case V_INT: return (var->v.i == 0); case V_MAP: return map_is_empty(var); case V_PTR: return (var->v.ap.p == 0); case V_NUM: return (var->v.n == 0.0); case V_ARRAY: return (var->v.a.size == 0); case V_REF: return v_isempty(var->v.ref); } return 1; }