Пример #1
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */
tb_int_t tb_demo_asio_aicpc_main(tb_int_t argc, tb_char_t** argv)
{
    // check
    tb_assert_and_check_return_val(argv[1], 0);

    // init
    tb_aicp_ref_t       aicp = tb_null;
    tb_demo_context_t   context = {0};
    do
    {
        // init aicp
        aicp = tb_aicp_init(2);
        tb_assert_and_check_break(aicp);

        // init data
        context.data = tb_malloc_bytes(TB_DEMO_SOCK_RECV_MAXN);
        tb_assert_and_check_break(context.data);

        // init sock aico
        context.sock = tb_aico_init(aicp);
        tb_assert_and_check_break(context.sock);

        // init addr
        tb_ipaddr_t addr; 
        if (!tb_ipaddr_set(&addr, "127.0.0.1", 9090, TB_IPADDR_FAMILY_NONE)) break;

        // open sock aico
        if (!tb_aico_open_sock_from_type(context.sock, TB_SOCKET_TYPE_TCP, tb_ipaddr_family(&addr))) break;

        // init file aico
        context.file = tb_aico_init(aicp);
        tb_assert_and_check_break(context.file);

        // open file aico
        if (!tb_aico_open_file_from_path(context.file, argv[1], TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC)) break;

        // init conn timeout
        tb_aico_timeout_set(context.sock, TB_AICO_TIMEOUT_CONN, 10000);

        // post conn
        tb_trace_i("conn: ..");
        if (!tb_aico_conn(context.sock, &addr, tb_demo_sock_conn_func, &context)) break;

        // loop aicp
        tb_aicp_loop(aicp);
        
        // trace
        if (tb_mclock() > context.base) tb_trace_i("size: %llu, sped: %llu KB/s", context.size, context.size / (tb_mclock() - context.base));

    } while (0);

    // trace
    tb_trace_i("end");

    // exit aicp
    if (aicp) tb_aicp_exit(aicp);

    return 0;
}
Пример #2
0
static tb_bool_t tb_aiop_push_acpt(tb_aiop_ptor_impl_t* impl, tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(impl && aice, tb_false);
    tb_assert_and_check_return_val(aice->code == TB_AICE_CODE_ACPT, tb_false);

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

    // the priority
    tb_size_t priority = tb_aice_impl_priority(aice);
    tb_assert_and_check_return_val(priority < tb_arrayn(impl->spak) && impl->spak[priority], tb_false);

    // init the acpt aice
    tb_aice_t acpt_aice = *aice;
    acpt_aice.state = TB_STATE_OK;

    // done
    tb_size_t       list_indx = 0;
    tb_size_t       list_size = 0;
    tb_socket_ref_t list_sock[2048];
    tb_ipaddr_t       list_addr[2048];
    tb_size_t       list_maxn = tb_arrayn(list_sock);
    tb_socket_ref_t acpt = (tb_socket_ref_t)aico->base.handle;
    tb_queue_ref_t  spak = impl->spak[priority];
    tb_socket_ref_t sock = tb_null;
    do
    {
        // accept it
        for (list_size = 0; list_size < list_maxn && (list_sock[list_size] = tb_socket_accept(acpt, list_addr + list_size)); list_size++) ;

        // enter
        tb_spinlock_enter(&impl->lock);

        // push some acpt aice
        for (list_indx = 0; list_indx < list_size && (sock = list_sock[list_indx]); list_indx++)
        {
            // init aico
            acpt_aice.u.acpt.aico = tb_aico_init(aico->base.aicp);

            // trace
            tb_trace_d("push: acpt[%p]: sock: %p, aico: %p", aico->base.handle, sock, acpt_aice.u.acpt.aico);

            // open aico and push the acpt aice if not full?
            if (    acpt_aice.u.acpt.aico
                    &&  tb_aico_open_sock(acpt_aice.u.acpt.aico, sock)
                    &&  !tb_queue_full(spak))
            {
                // save addr
                tb_ipaddr_copy(&acpt_aice.u.acpt.addr, list_addr + list_indx);

                // push to the spak queue
                tb_queue_put(spak, &acpt_aice);
            }
            else
            {
                // close the left sock
                tb_size_t i;
                for (i = list_indx; i < list_size; i++)
                {
                    // close it
                    if (list_sock[i]) tb_socket_exit(list_sock[i]);
                    list_sock[i] = tb_null;
                }

                // exit aico
                if (acpt_aice.u.acpt.aico) tb_aico_exit(acpt_aice.u.acpt.aico);
                acpt_aice.u.acpt.aico = tb_null;

                // trace
                tb_trace_e("push: acpt failed!");
                break;
            }
        }

        // leave
        tb_spinlock_leave(&impl->lock);

    } while (list_indx == list_maxn);

    // ok
    return tb_true;
}
Пример #3
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */
tb_int_t tb_demo_asio_aicpd_main(tb_int_t argc, tb_char_t** argv)
{
    // check
    tb_assert_and_check_return_val(argv[1], 0);

    // init
    tb_aicp_ref_t       aicp = tb_null;
    tb_aico_ref_t       aico = tb_null;
//    tb_aico_ref_t       task = tb_null;
    tb_thread_ref_t     loop[16] = {tb_null};
    do
    {
        // init aicp
        aicp = tb_aicp_init(16);
        tb_assert_and_check_break(aicp);

        // init sock aico
        aico = tb_aico_init(aicp);
        tb_assert_and_check_break(aico);

        // init addr
        tb_ipaddr_t addr;
        tb_ipaddr_set(&addr, tb_null, 9090, TB_IPADDR_FAMILY_IPV4);

        // open sock aico
        if (!tb_aico_open_sock_from_type(aico, TB_SOCKET_TYPE_TCP, tb_ipaddr_family(&addr))) break;

        // bind port
        if (!tb_socket_bind(tb_aico_sock(aico), &addr)) break;

        // listen sock
        if (!tb_socket_listen(tb_aico_sock(aico), 20)) break;

#if 0
        // init task aico
        task = tb_aico_init(aicp);
        tb_assert_and_check_break(task);

        // open task aico
        if (!tb_aico_open_task(task, tb_false)) break;

        // run task
        if (!tb_aico_task_run(task, 0, tb_demo_task_func, tb_null)) break;
        if (!tb_aico_task_run(aico, 0, tb_demo_task_func, tb_null)) break;
#endif

        // post acpt
        if (!tb_aico_acpt(aico, tb_demo_sock_acpt_func, argv[1])) break;

        // done loop
        loop[0] = tb_thread_init(tb_null, tb_demo_loop, aicp, 0);
        loop[1] = tb_thread_init(tb_null, tb_demo_loop, aicp, 0);
        loop[2] = tb_thread_init(tb_null, tb_demo_loop, aicp, 0);
        loop[3] = tb_thread_init(tb_null, tb_demo_loop, aicp, 0);

        // wait exit
        getchar();

    } while (0);

    // trace
    tb_trace_i("end");
 
#if 1
    if (aicp)
    {
        // kill all
        tb_aicp_kill_all(aicp);
   
        // wait all
        tb_aicp_wait_all(aicp, -1);

        // kill aicp
        tb_aicp_kill(aicp);
    }

    // wait exit
    {
        // exit loop
        tb_thread_ref_t* l = loop;
        for (; *l; l++)
        {
            tb_thread_wait(*l, -1, tb_null);
            tb_thread_exit(*l);
        }
    }
#endif

    // exit aicp
    if (aicp) tb_aicp_exit(aicp);
    return 0;
}
Пример #4
0
static tb_bool_t tb_demo_sock_acpt_func(tb_aice_ref_t aice)
{
    // check
    tb_assert_and_check_return_val(aice && aice->code == TB_AICE_CODE_ACPT, tb_false);

    // the file path
    tb_char_t const* path = (tb_char_t const*)aice->priv;
    tb_assert_and_check_return_val(path, tb_false);

    // the aicp
    tb_aicp_ref_t aicp = tb_aico_aicp(aice->aico);
    tb_assert_and_check_return_val(aicp, tb_false);

    // acpt ok?
    if (aice->state == TB_STATE_OK)
    {
        // trace
        tb_trace_i("acpt[%p]: %p", aice->aico, aice->u.acpt.aico);

        // done
        tb_bool_t           ok = tb_false;
        tb_demo_context_t*  context = tb_null;
        do
        {
            // make context
            context = tb_malloc0_type(tb_demo_context_t);
            tb_assert_and_check_break(context);

#ifdef TB_DEMO_MODE_SENDF
            // init file
            context->file = tb_file_init(path, TB_FILE_MODE_RO | TB_FILE_MODE_ASIO);
            tb_assert_and_check_break(context->file);

            // init sock aico
            context->aico[0] = aice->u.acpt.aico;
            tb_assert_and_check_break(context->aico[0]);

            // post sendf from file
            if (!tb_aico_sendf(context->aico[0], context->file, 0ULL, tb_file_size(context->file), tb_demo_sock_sendf_func, context)) break;
#else
            // init data
            context->data = tb_malloc_bytes(TB_DEMO_FILE_READ_MAXN);
            tb_assert_and_check_break(context->file && context->data);

            // init sock aico
            context->aico[0] = aice->u.acpt.aico;
            tb_assert_and_check_break(context->aico[0]);

            // init file aico
            context->aico[1] = tb_aico_init(aicp);
            tb_assert_and_check_break(context->aico[1]);

            // open file aico
            if (!tb_aico_open_file_from_path(context->aico[1], path, TB_FILE_MODE_RO)) break;

            // post read from file
            if (!tb_aico_read(context->aico[1], context->size, context->data, TB_DEMO_FILE_READ_MAXN, tb_demo_file_read_func, context)) break;
#endif
            // ok
            ok = tb_true;

        } while (0);

        // failed?
        if (!ok)
        {
            // exit context
            if (context) tb_demo_context_exit(context);
        }
    }
    // failed?
    else
    {
        // exit loop
        tb_trace_i("acpt[%p]: state: %s", aice->aico, tb_state_cstr(aice->state));

        // clos aico
        if (aice->aico) tb_aico_clos(aice->aico, tb_demo_aico_clos, tb_null);
    }

    // ok
    return tb_true;
}
Пример #5
0
tb_bool_t tb_aicp_dns_done(tb_aicp_dns_ref_t dns, tb_char_t const* host, tb_long_t timeout, tb_aicp_dns_done_func_t func, tb_cpointer_t priv)
{
    // check
    tb_aicp_dns_impl_t* impl = (tb_aicp_dns_impl_t*)dns;
    tb_assert_and_check_return_val(impl && func && host && host[0], tb_false);
    
    // trace
    tb_trace_d("done: aico: %p, host: %s: ..", impl->aico, host);

    // init func
    impl->done.func = func;
    impl->done.priv = priv;

    // save host
    tb_strlcpy(impl->host, host, sizeof(impl->host));
 
    // only address? ok
    tb_ipaddr_t addr = {0};
    if (tb_ipaddr_ip_cstr_set(&addr, impl->host, TB_IPADDR_FAMILY_NONE))
    {
        impl->done.func(dns, impl->host, &addr, impl->done.priv);
        return tb_true;
    }

    // try to lookup it from cache first
    if (tb_dns_cache_get(impl->host, &addr))
    {
        impl->done.func(dns, impl->host, &addr, impl->done.priv);
        return tb_true;
    }

    // init server list
    if (!impl->size) impl->size = tb_dns_server_get(impl->list);
    tb_check_return_val(impl->size, tb_false);

    // get the server 
    tb_ipaddr_ref_t server = &impl->list[impl->indx = 0];
    tb_assert_and_check_return_val(!tb_ipaddr_is_empty(server), tb_false);

    // init reqt
    tb_size_t size = tb_aicp_dns_reqt_init(impl);
    tb_assert_and_check_return_val(size, tb_false);

    // init it first if no aico
    if (!impl->aico)
    {
        // init aico
        impl->aico = tb_aico_init(impl->aicp);
        tb_assert_and_check_return_val(impl->aico, tb_false);

        // open aico
        if (!tb_aico_open_sock_from_type(impl->aico, TB_SOCKET_TYPE_UDP, tb_ipaddr_family(server))) return tb_false;

        // init timeout
        tb_aico_timeout_set(impl->aico, TB_AICO_TIMEOUT_SEND, timeout);
        tb_aico_timeout_set(impl->aico, TB_AICO_TIMEOUT_RECV, timeout);
    }

    // post reqt
    return tb_aico_usend(impl->aico, server, impl->data, size, tb_aicp_dns_reqt_func, (tb_pointer_t)impl);
}