int am_url_validator_init() {
    if (validator_timer != NULL) {
        return AM_SUCCESS;
    }

    AM_MUTEX_INIT(&table_mutex);

    validator_timer = am_create_timer_event(AM_TIMER_EVENT_RECURRING,
            MIN_URL_VALIDATOR_TICK, NULL, am_url_validator_tick);
    if (validator_timer == NULL) {
        return AM_ENOMEM;
    }

    if (validator_timer->error != 0) {
        return AM_ERROR;
    }

    am_start_timer_event(validator_timer);

    return AM_SUCCESS;
}
Beispiel #2
0
static void net_async_poll(am_net_t *n) {
    static const char *thisfunc = "net_async_poll():";
    int ev = 0;
    char first_run = 1;
#ifdef _WIN32
    WSAPOLLFD fds[1];
#else
    struct pollfd fds[1];
#endif

    n->tm = am_create_timer_event(AM_TIMER_EVENT_ONCE, AM_NET_POOL_TIMEOUT, n,
            net_async_poll_timeout);
    if (n->tm == NULL) {
        AM_LOG_ERROR(n->instance_id,
                "%s failed to create response timeout control", thisfunc);
        n->error = AM_ENOMEM;
        return;
    }
    if (n->tm->error != 0) {
        AM_LOG_ERROR(n->instance_id,
                "%s error %d creating response timeout control", thisfunc, n->tm->error);
        n->error = AM_ERROR;
        return;
    }

    am_start_timer_event(n->tm);

    memset(fds, 0, sizeof (fds));
    while (ev != -1) {

        fds[0].fd = n->sock;
        fds[0].events = read_ev;
        fds[0].revents = 0;

        if (first_run) {
            set_event(n->ce);
            if (n->on_connected) n->on_connected(n->data, 0);
            first_run = 0;
        }

        if (wait_for_exit_event(n->de) != 0) {
            break;
        }

        ev = sockpoll(fds, 1, 100);
        if (ev < 0) {
            net_log_error(n->instance_id, net_error());
            break;
        }
        if (ev == 1 && fds[0].revents & (POLLNVAL | POLLERR)) {
            if (n->on_close) n->on_close(n->data, 0);
            break;
        }
        if (ev == 1 && fds[0].revents & read_avail_ev) {
            /* read an output from a remote side */
            int er = 0, error = 0;
            char tmp[1024];
            int got = 0;
            SOCKLEN_T errlen = sizeof (error);
            er = getsockopt(n->sock, SOL_SOCKET, SO_ERROR, (void *) &error, &errlen);
            memset(&tmp[0], 0, sizeof (tmp));
            if (error != 0) break;

            got = recv(n->sock, tmp, sizeof (tmp), 0);
            if (n->ssl.on) {
                error = net_read_ssl(n, tmp, got);
                if (error != AM_SUCCESS) {
                    if (error != AM_EAGAIN) {
                        if (n->on_close) n->on_close(n->data, 0);
                        break;
                    }
                }
            } else {
                if (got < 0) {
                    if (!net_in_progress(errno)) {
                        if (n->on_close) n->on_close(n->data, 0);
                        break;
                    }
                } else if (got == 0) {
                    if (n->on_close) n->on_close(n->data, 0);
                    break;
                } else {
                    http_parser_execute(n->hp, n->hs, tmp, got);
                }
            }
        }
    }
}