예제 #1
0
파일: aicp_aiop.c 프로젝트: RockyShi/tbox
static tb_void_t tb_aiop_ptor_kilo(tb_aicp_ptor_impl_t* ptor, tb_aico_impl_t* aico)
{
    // check
    tb_aiop_ptor_impl_t* impl = (tb_aiop_ptor_impl_t*)ptor;
    tb_assert_and_check_return(impl && impl->timer && impl->ltimer && impl->aiop && aico);

    // trace
    tb_trace_d("kilo: aico: %p, type: %u: ..", aico, aico->type);

    // the aiop aico
    tb_aiop_aico_t* aiop_aico = (tb_aiop_aico_t*)aico;

    // add timeout task for killing the accept socket
    if (aico->type == TB_AICO_TYPE_SOCK && aiop_aico->aice.code == TB_AICE_CODE_ACPT) 
    {
        // add task
        if (!aiop_aico->task) 
        {
            aiop_aico->task = tb_ltimer_task_init(impl->ltimer, 10000, tb_false, tb_aiop_spak_wait_timeout, aico);
            aiop_aico->bltimer = 1;
        }
    }

    // kill the task
    if (aiop_aico->task) 
    {
        // trace
        tb_trace_d("kilo: aico: %p, type: %u, task: %p: ..", aico, aico->type, aiop_aico->task);

        // kill task
        if (aiop_aico->bltimer) tb_ltimer_task_kill(impl->ltimer, aiop_aico->task);
        else tb_timer_task_kill(impl->timer, aiop_aico->task);
    }

    // kill sock
    if (aico->type == TB_AICO_TYPE_SOCK && aico->handle) 
    {
        // trace
        tb_trace_d("kilo: aico: %p, type: %u, sock: %p: ..", aico, aico->type, aico->handle);

        // kill it
        tb_socket_kill(aico->handle, TB_SOCKET_KILL_RW);
    }
    // kill file
    else if (aico->type == TB_AICO_TYPE_FILE)
    {
        // kill it
        tb_aicp_file_kilo(impl, aico);
    }

    /* the aiop will wait long time if the lastest task wait period is too long
     * so spak the aiop manually for spak the ltimer
     */
    tb_aiop_spak(impl->aiop);

    // trace
    tb_trace_d("kilo: aico: %p, type: %u: ok", aico, aico->type);
}
예제 #2
0
파일: aicp_aiop.c 프로젝트: ZuckerB/tbox
static tb_long_t tb_aiop_spak_runtask(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && impl->aiop && impl->ltimer && impl->timer && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_RUNTASK, -1);
    tb_assert_and_check_return_val(aice->u.runtask.when, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && !aico->task, -1);

    // now
    tb_hong_t now = tb_cache_time_mclock();

    // timeout?
    tb_long_t ok = -1;
    if (aice->u.runtask.when <= now)
    {
        // trace
        tb_trace_d("runtask: when: %llu, now: %lld: ok", aice->u.runtask.when, now);

        // ok
        aice->state = TB_STATE_OK;
        ok = 1;
    }
    else
    {
        // trace
        tb_trace_d("runtask: when: %llu, now: %lld: ..", aice->u.runtask.when, now);

        // wait it
        aico->aice = *aice;
        aico->waiting = 1;

        // add timeout task, is the higher precision timer?
        if (aico->base.handle)
        {
            // the top when
            tb_hize_t top = tb_timer_top(impl->timer);

            // add task
            aico->task = tb_timer_task_init_at(impl->timer, aice->u.runtask.when, 0, tb_false, tb_aiop_spak_runtask_timeout, aico);
            aico->bltimer = 0;

            // the top task is changed? spak aiop
            if (aico->task && aice->u.runtask.when < top)
                tb_aiop_spak(impl->aiop);
        }
        else
        {
            aico->task = tb_ltimer_task_init_at(impl->ltimer, aice->u.runtask.when, 0, tb_false, tb_aiop_spak_runtask_timeout, aico);
            aico->bltimer = 1;
        }

        // wait
        ok = 0;
    }

    // ok
    return ok;
}