Пример #1
0
void 
tc_time_init()
{
    tc_update_time = 0;

    tc_time_update();
}
Пример #2
0
int tc_event_proc_cycle(tc_event_loop_t *loop)
{
    int                  ret;
    long                 timeout;
    tc_msec_t            delta;
    tc_event_t          *act_event, *act_next;
    tc_event_actions_t  *actions;

    actions = loop->actions;

    for ( ;; ) {
        timeout = tc_event_find_timer();
        if (timeout == 0 || timeout > 1000) {
            timeout = 500;
        }

        loop->active_events = NULL;

        delta = tc_current_time_msec;   
        ret = actions->poll(loop, timeout);
        if (tc_over) {
            goto FINISH;
        }

        tc_time_update();

        delta = tc_current_time_msec - delta;   

        if (delta) {
            tc_event_expire_timers();
        }

        if (ret == TC_EVENT_ERROR || ret == TC_EVENT_AGAIN) {
            continue;
        }

        for (act_event = loop->active_events; act_event; act_event = act_next) {
            act_next = act_event->next;

            if (act_event->events & TC_EVENT_READ) {
                if (act_event->read_handler(act_event) == TC_ERR_EXIT) {
                    goto FINISH;
                }
            }

            if (act_event->events & TC_EVENT_WRITE) {
                if (act_event->write_handler(act_event) == TC_ERR_EXIT) {
                    goto FINISH;
                }
            }

            if (act_event->reg_evs == TC_EVENT_NONE) {
                tc_event_destroy(act_event, 0);
            }
        }
    }

FINISH:
    return TC_EVENT_OK;
}
Пример #3
0
/*
 * Main entry point
 */
int
main(int argc ,char **argv)
{
    int             ret;

    /* first, init time */
    tc_time_update();

    /* Set defaults */
    settings_init();
    read_args(argc, argv);
    /* Init log for outputing debug info */
    log_init(clt_settings.log_path);
    /* Output debug info */
    output_for_debug(argc, argv);
    /* Set details for running */
    set_details();

    ret = tc_event_loop_init(&event_loop, MAX_FD_NUM);
    if (ret == TC_EVENT_ERROR) {
        log_info(LOG_ERR, "event loop init failed");
        return -1;
    }

    /* Initiate tcpcopy client*/
    ret = tcp_copy_init(&event_loop);
    if (SUCCESS != ret) {
        exit(EXIT_FAILURE);
    }

    /* Run now */
    tc_event_process_cycle(&event_loop);

    return 0;
}
Пример #4
0
void
tc_log_info(int level, int err, const char *fmt, ...)
{
    int             n, len;
    char            buffer[LOG_MAX_LEN], *p;
    va_list         args;
    tc_log_level_t *ll;

    if (!dbg_level)return;

    if (log_fd == -1) {
        return;
    }

#if (TCPCOPY_DEBUG)
    tc_time_update();
#endif

    ll = &tc_log_levels[level];

    p = buffer;

    p = tc_cpymem(p, tc_error_log_time, TC_ERR_LOG_TIME_LEN);
    *p++ = ' ';

    p = tc_cpymem(p, ll->level, ll->len);
    *p++ = ' ';

    n = len = TC_ERR_LOG_TIME_LEN + ll->len + 2;
    va_start(args, fmt);
    len += tc_vscnprintf(p, LOG_MAX_LEN - n, fmt, args);
    va_end(args);

    if (len < n) {
        return;
    }

    p = buffer + len;

    if (err > 0) {
        len += tc_scnprintf(p, LOG_MAX_LEN - len, " (%s)", strerror(err));
        if (len < (p - buffer)) {
            return;
        }

        p = buffer + len;
    }

    *p++ = '\n';

    write(log_fd, buffer, p - buffer);
}