Ejemplo n.º 1
0
Archivo: aicp.c Proyecto: DonkeyWs/tbox
static tb_void_t tb_aicp_instance_exit(tb_handle_t handle, tb_cpointer_t priv)
{
    // check
    tb_assert_and_check_return(handle);

    // wait all
    if (!tb_aicp_wait_all((tb_aicp_ref_t)handle, 5000)) return ;

    // kill aicp
    tb_aicp_kill((tb_aicp_ref_t)handle);

    // exit loop
    tb_thread_ref_t loop = (tb_thread_ref_t)priv;
    if (loop)
    {
        // wait it
        if (!tb_thread_wait(loop, 5000)) return ;

        // exit it
        tb_thread_exit(loop);
    }

    // exit it
    tb_aicp_exit((tb_aicp_ref_t)handle);
}
Ejemplo n.º 2
0
Archivo: aicp.c Proyecto: DonkeyWs/tbox
static tb_handle_t tb_aicp_instance_init(tb_cpointer_t* ppriv)
{
    // check
    tb_assert_and_check_return_val(ppriv, tb_null);

    // done
    tb_bool_t       ok = tb_false;
    tb_aicp_ref_t   aicp = tb_null;
    do
    {
        // init aicp
        aicp = tb_aicp_init(0);
        tb_assert_and_check_break(aicp);

        // init loop
        *ppriv = (tb_cpointer_t)tb_thread_init(tb_null, tb_aicp_instance_loop, aicp, 0);
        tb_assert_and_check_break(*ppriv);

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit aicp
        if (aicp) tb_aicp_exit(aicp);
        aicp = tb_null;
    }

    // ok?
    return (tb_handle_t)aicp;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
Archivo: dns.c Proyecto: DonkeyWs/tbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */
tb_int_t tb_demo_asio_dns_main(tb_int_t argc, tb_char_t** argv)
{
    // check
    tb_assert_and_check_return_val(argv[1], 0);

    // done
    tb_aicp_ref_t       aicp = tb_null;
    tb_aicp_dns_ref_t   dns = tb_null;
    do
    {
        // init aicp
        aicp = tb_aicp_init(2);
        tb_assert_and_check_break(aicp);

        // init dns
        dns = tb_aicp_dns_init(aicp);
        tb_assert_and_check_break(dns);

        // sort server 
        tb_dns_server_sort();

        // init time
        tb_hong_t time = tb_mclock();

        // trace
        tb_trace_i("dns: %s: ..", argv[1]);

        // done dns
        tb_aicp_dns_done(dns, argv[1], -1, tb_demo_sock_dns_done_func, tb_null);

        // loop aicp
        tb_aicp_loop(aicp);

        // exit time
        time = tb_mclock() - time;

        // trace
        tb_trace_i("dns: %s: time: %lld ms", argv[1], time);

    } while (0);

    // trace
    tb_trace_i("end");

    // exit aicp
    if (aicp) tb_aicp_exit(aicp);
    return 0;
}
Ejemplo n.º 5
0
Archivo: aicp.c Proyecto: DonkeyWs/tbox
tb_aicp_ref_t tb_aicp_init(tb_size_t maxn)
{
    // check iovec
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, size, tb_iovec_t, size), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_send_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_send_t, size, tb_iovec_t, size), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_read_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_read_t, size, tb_iovec_t, size), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_writ_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_writ_t, size, tb_iovec_t, size), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_urecv_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_urecv_t, size, tb_iovec_t, size), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_usend_t, data, tb_iovec_t, data), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_usend_t, size, tb_iovec_t, size), tb_null);

    // check real
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_send_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_read_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_writ_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_sendf_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_sendv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_recvv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_readv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_writv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_urecv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_usend_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_urecvv_t, real), tb_null);
    tb_assert_and_check_return_val(tb_memberof_eq(tb_aice_recv_t, real, tb_aice_usendv_t, real), tb_null);

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

        // init impl
#ifdef __tb_small__
        impl->maxn = maxn? maxn : (1 << 4);
#else
        impl->maxn = maxn? maxn : (1 << 8);
#endif

        // init lock
        if (!tb_spinlock_init(&impl->lock)) break;

        // init proactor
        impl->ptor = tb_aicp_ptor_impl_init(impl);
        tb_assert_and_check_break(impl->ptor && impl->ptor->step >= sizeof(tb_aico_impl_t));

        // init aico pool
        impl->pool = tb_fixed_pool_init(tb_null, (impl->maxn >> 4) + 16, impl->ptor->step, tb_null, tb_null, tb_null);
        tb_assert_and_check_break(impl->pool);

        // register lock profiler
#ifdef TB_LOCK_PROFILER_ENABLE
        tb_lock_profiler_register(tb_lock_profiler(), (tb_pointer_t)&impl->lock, TB_TRACE_MODULE_NAME);
#endif

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok) 
    {
        // exit impl
        if (impl) tb_aicp_exit((tb_aicp_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (tb_aicp_ref_t)impl;
}
Ejemplo n.º 6
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;
}