コード例 #1
0
ファイル: aicp.c プロジェクト: 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;
}
コード例 #2
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;
}
コード例 #3
0
ファイル: dns.c プロジェクト: 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;
}
コード例 #4
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;
}