Пример #1
0
static tb_long_t tb_aiop_spak_acpt(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_ACPT, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);
    tb_assert_and_check_return_val(!aico->waiting, -1);

    // trace
    tb_trace_d("acpt[%p]: wait: ..", aico);

    // wait ok?
    if (tb_aiop_spak_wait(impl, aice)) return 0;
    // wait failed
    else aice->state = TB_STATE_FAILED;

    // trace
    tb_trace_d("acpt[%p]: wait: failed", aico);

    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #2
0
static tb_long_t tb_aiop_spak_sendf(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_SENDF, -1);
    tb_assert_and_check_return_val(aice->u.sendf.file && aice->u.sendf.size, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // try to send it
    tb_long_t   real = 0;
    tb_hize_t   send = 0;
    tb_hize_t   seek = aice->u.sendf.seek;
    tb_hize_t   size = aice->u.sendf.size;
    tb_handle_t file = aice->u.sendf.file;
    while (send < size)
    {
        // send it
        real = tb_socket_sendf(aico->base.handle, file, seek + send, size - send);

        // save send
        if (real > 0) send += real;
        else break;
    }

    // trace
    tb_trace_d("sendf[%p]: %llu", aico, send);

    // no send?
    if (!send)
    {
        // wait it
        if (!real && !aico->waiting)
        {
            // wait ok?
            if (tb_aiop_spak_wait(impl, aice)) return 0;
            // wait failed
            else aice->state = TB_STATE_FAILED;
        }
        // closed
        else aice->state = TB_STATE_CLOSED;
    }
    else
    {
        // ok or closed?
        aice->state = TB_STATE_OK;

        // save the send size
        aice->u.sendf.real = send;
    }

    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #3
0
static tb_long_t tb_aiop_spak_usend(tb_aiop_ptor_impl_t* impl, tb_aice_t* aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_USEND, -1);
    tb_assert_and_check_return_val(aice->u.usend.data && aice->u.usend.size, -1);
    tb_assert_and_check_return_val(aice->u.usend.addr.u32 && aice->u.usend.port, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // try to send it
    tb_size_t send = 0;
    tb_long_t real = 0;
    while (send < aice->u.usend.size)
    {
        // send it
        real = tb_socket_usend(aico->base.handle, &aice->u.usend.addr, aice->u.usend.port, aice->u.usend.data + send, aice->u.usend.size - send);
        
        // save send
        if (real > 0) send += real;
        else break;
    }

    // trace
    tb_trace_d("usend[%p]: %{ipv4}: %lu, %lu", aico, &aice->u.usend.addr, aice->u.usend.port, send);

    // no send? 
    if (!send) 
    {
        // wait it
        if (!real && !aico->waiting)
        {
            // wait ok?
            if (tb_aiop_spak_wait(impl, aice)) return 0;
            // wait failed
            else aice->state = TB_STATE_FAILED;
        }
        // closed
        else aice->state = TB_STATE_CLOSED;
    }
    else
    {
        // ok or closed?
        aice->state = TB_STATE_OK;

        // save the send size
        aice->u.usend.real = send;
    }
    
    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #4
0
static tb_long_t tb_aiop_spak_urecv(tb_aiop_ptor_impl_t* impl, tb_aice_t* aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_URECV, -1);
    tb_assert_and_check_return_val(aice->u.urecv.data && aice->u.urecv.size, -1);
    tb_assert_and_check_return_val(aice->u.urecv.addr.u32 && aice->u.urecv.port, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // try to recv it
    tb_size_t recv = 0;
    tb_long_t real = 0;
    while (recv < aice->u.urecv.size)
    {
        // recv it
        real = tb_socket_urecv(aico->base.handle, &aice->u.urecv.addr, aice->u.urecv.port, aice->u.urecv.data + recv, aice->u.urecv.size - recv);

        // save recv
        if (real > 0) recv += real;
        else break;
    }

    // trace
    tb_trace_d("urecv[%p]: %{ipv4}: %lu, %lu", aico, &aice->u.urecv.addr, aice->u.urecv.port, recv);

    // no recv? 
    if (!recv) 
    {
        // wait it
        if (!real && !aico->waiting)
        {
            // wait ok?
            if (tb_aiop_spak_wait(impl, aice)) return 0;
            // wait failed
            else aice->state = TB_STATE_FAILED;
        }
        // closed
        else aice->state = TB_STATE_CLOSED;
    }
    else
    {
        // ok or closed?
        aice->state = TB_STATE_OK;

        // save the recv size
        aice->u.urecv.real = recv;
    }
    
    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #5
0
static tb_long_t tb_aiop_spak_conn(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_CONN, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // check address
    tb_assert(!tb_ipaddr_is_empty(&aice->u.conn.addr));

    // try to connect it
    tb_long_t ok = tb_socket_connect(aico->base.handle, &aice->u.conn.addr);

    // trace
    tb_trace_d("conn[%p]: %{ipaddr}: %ld", aico, &aice->u.conn.addr, ok);

    // no connected? wait it
    if (!ok)
    {
        // wait it
        if (!aico->waiting)
        {
            // wait ok?
            if (tb_aiop_spak_wait(impl, aice)) return 0;
            // wait failed
            else aice->state = TB_STATE_FAILED;
        }
        // closed
        else aice->state = TB_STATE_FAILED;
    }

    // save it
    aice->state = ok > 0? TB_STATE_OK : TB_STATE_FAILED;

    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #6
0
static tb_long_t tb_aiop_spak_usendv(tb_aiop_ptor_impl_t* impl, tb_aice_t* aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_USENDV, -1);
    tb_assert_and_check_return_val(aice->u.usendv.list && aice->u.usendv.size, -1);
    tb_assert_and_check_return_val(aice->u.usendv.addr.u32 && aice->u.usendv.port, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // send it
    tb_long_t real = tb_socket_usendv(aico->base.handle, &aice->u.usendv.addr, aice->u.usendv.port, aice->u.usendv.list, aice->u.usendv.size);

    // trace
    tb_trace_d("usendv[%p]: %{ipv4}: %lu, %lu", aico, &aice->u.usendv.addr, aice->u.usendv.port, real);

    // ok? 
    if (real > 0) 
    {
        aice->u.usendv.real = real;
        aice->state = TB_STATE_OK;
    }
    // no send?
    else if (!real && !aico->waiting) 
    {
        // wait ok?
        if (tb_aiop_spak_wait(impl, aice)) return 0;
        // wait failed
        else aice->state = TB_STATE_FAILED;
    }
    // closed?
    else aice->state = TB_STATE_CLOSED;
    
    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}
Пример #7
0
static tb_long_t tb_aiop_spak_recvv(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, -1);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_RECVV, -1);
    tb_assert_and_check_return_val(aice->u.recvv.list && aice->u.recvv.size, -1);

    // the aico
    tb_aiop_aico_t* aico = (tb_aiop_aico_t*)aice->aico;
    tb_assert_and_check_return_val(aico && aico->base.handle, -1);

    // recv it
    tb_long_t real = tb_socket_recvv(aico->base.handle, aice->u.recvv.list, aice->u.recvv.size);

    // trace
    tb_trace_d("recvv[%p]: %lu", aico, real);

    // ok?
    if (real > 0)
    {
        aice->u.recvv.real = real;
        aice->state = TB_STATE_OK;
    }
    // no recv?
    else if (!real && !aico->waiting)
    {
        // wait ok?
        if (tb_aiop_spak_wait(impl, aice)) return 0;
        // wait failed
        else aice->state = TB_STATE_FAILED;
    }
    // closed?
    else aice->state = TB_STATE_CLOSED;

    // reset wait
    aico->waiting = 0;
    aico->aice.code = TB_AICE_CODE_NONE;

    // ok
    return 1;
}