void check_timeout(acetables *g_ape) { USERS *list, *wait; long int ctime = time(NULL); list = g_ape->uHead; while (list != NULL) { wait = list->next; if ((ctime - list->idle) >= TIMEOUT_SEC && list->type == HUMAN) { deluser(list, g_ape); } else if (list->type == HUMAN) { subuser **n = &(list->subuser); while (*n != NULL) { if ((ctime - (*n)->idle) >= TIMEOUT_SEC) { delsubuser(n); continue; } if ((*n)->state == ALIVE && (*n)->nraw && !(*n)->need_update) { /* Data completetly sent => closed */ if (send_raws(*n, g_ape)) { do_died(*n); } else { (*n)->burn_after_writing = 1; } } else { FIRE_EVENT_NONSTOP(tickuser, *n, g_ape); } n = &(*n)->next; } } list = wait; } }
void delsubuser(subuser **current, acetables *g_ape) { subuser *del = *current; FIRE_EVENT_NONSTOP(delsubuser, del, g_ape); ((*current)->user->nsub)--; *current = (*current)->next; destroy_raw_pool(del->raw_pools.low.rawhead); destroy_raw_pool(del->raw_pools.high.rawhead); clear_properties(&del->properties); if (del->state == ALIVE) { del->wait_for_free = 1; do_died(del); } else { free(del); } }
subuser *addsubuser(ape_socket *client, const char *channel, USERS *user, acetables *g_ape) { subuser *sub; if (getsubuser(user, channel) != NULL || strlen(channel) > MAX_HOST_LENGTH) { return NULL; } sub = xmalloc(sizeof(*sub)); sub->client = client; sub->state = ADIED; sub->user = user; memcpy(sub->channel, channel, strlen(channel)+1); sub->next = user->subuser; sub->nraw = 0; sub->wait_for_free = 0; sub->properties = NULL; sub->headers.sent = 0; sub->headers.content = NULL; sub->burn_after_writing = 0; sub->idle = time(NULL); sub->need_update = 0; sub->current_chl = 0; sub->raw_pools.nraw = 0; /* Pre-allocate a pool of raw to reduce the number of malloc calls */ /* Low priority raws */ sub->raw_pools.low.nraw = 0; sub->raw_pools.low.size = 32; sub->raw_pools.low.rawhead = init_raw_pool(sub->raw_pools.low.size); sub->raw_pools.low.rawfoot = sub->raw_pools.low.rawhead; /* High priority raws */ sub->raw_pools.high.nraw = 0; sub->raw_pools.high.size = 8; sub->raw_pools.high.rawhead = init_raw_pool(sub->raw_pools.high.size); sub->raw_pools.high.rawfoot = sub->raw_pools.high.rawhead; (user->nsub)++; user->subuser = sub; /* if the previous subuser have some messages in queue, copy them to the new subuser */ if (sub->next != NULL && sub->next->raw_pools.low.nraw) { struct _raw_pool *rTmp; for (rTmp = sub->next->raw_pools.low.rawhead; rTmp->raw != NULL; rTmp = rTmp->next) { if (rTmp->raw->refcount == 0) { rTmp->raw->refcount = 1; } post_raw_sub(copy_raw_z(rTmp->raw), sub, g_ape); } } FIRE_EVENT_NONSTOP(addsubuser, sub, g_ape); return sub; }