Example #1
0
tb_transfer_pool_ref_t tb_transfer_pool_init(tb_aicp_ref_t aicp)
{
    // done
    tb_bool_t                   ok = tb_false;
    tb_transfer_pool_impl_t*    impl = tb_null;
    do
    {
        // using the default aicp if be null
        if (!aicp) aicp = tb_aicp();
        tb_assert_and_check_break(aicp);

        // make impl
        impl = tb_malloc0_type(tb_transfer_pool_impl_t);
        tb_assert_and_check_break(impl);

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

        // init pool
        impl->aicp      = aicp;
        impl->maxn      = tb_aicp_maxn(aicp);
        impl->state     = TB_STATE_OK;
        tb_assert_and_check_break(impl->maxn);

        // init idle task list
        tb_list_entry_init(&impl->idle, tb_transfer_task_t, entry, tb_null);

        // init work task list
        tb_list_entry_init(&impl->work, tb_transfer_task_t, entry, tb_null);

        // 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 it
        if (impl) tb_transfer_pool_exit((tb_transfer_pool_ref_t)impl);
        impl = tb_null;
    }

    // ok?
    return (tb_transfer_pool_ref_t)impl;
}
Example #2
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * interfaces
 */
tb_async_transfer_ref_t tb_async_transfer_init(tb_aicp_ref_t aicp, tb_bool_t autoclosing)
{
    // using the default aicp
    if (!aicp) aicp = tb_aicp();
    tb_assert_and_check_return_val(aicp, tb_null);

    // make impl
    tb_async_transfer_impl_t* impl = tb_malloc0_type(tb_async_transfer_impl_t);
    tb_assert_and_check_return_val(impl, tb_null);

    // init state
    impl->state         = TB_STATE_CLOSED;
    impl->state_pause   = TB_STATE_OK;
    impl->autoclosing   = autoclosing? 1 : 0;

    // init aicp
    impl->aicp          = aicp;

    // ok?
    return (tb_async_transfer_ref_t)impl;
}
Example #3
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */ 
tb_int_t tb_demo_stream_async_stream_null_main(tb_int_t argc, tb_char_t** argv)
{
    // done
    tb_event_ref_t              event = tb_null;
    tb_async_transfer_ref_t     transfer = tb_null;
    tb_async_stream_ref_t       istream = tb_null;
    tb_async_stream_ref_t       ostream = tb_null;
    tb_async_stream_ref_t       fstream = tb_null;
    do
    {
        // init event
        event = tb_event_init();
        tb_assert_and_check_break(event);

        // init istream
        istream = tb_async_stream_init_from_url(tb_aicp(), argv[1]);
        tb_assert_and_check_break(istream);

        // init ostream
        ostream = tb_async_stream_init_from_file(tb_aicp(), argv[2], TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC);
        tb_assert_and_check_break(ostream);

        // init fstream
        fstream = tb_async_stream_init_filter_from_null(istream);
        tb_assert_and_check_break(fstream);

        // init transfer
        transfer = tb_async_transfer_init(tb_null, tb_true);
        tb_assert_and_check_break(transfer);

        // init transfer stream
        if (!tb_async_transfer_init_istream(transfer, fstream)) break;
        if (!tb_async_transfer_init_ostream(transfer, ostream)) break;

        // open and save transfer
        if (!tb_async_transfer_open_done(transfer, 0, tb_demo_async_stream_null_done_func, event)) break;

        // wait it
        tb_event_wait(event, -1);

    } while (0);

    // exit transfer
    if (transfer) tb_async_transfer_exit(transfer);
    transfer = tb_null;

    // exit fstream
    if (fstream) tb_async_stream_exit(fstream);
    fstream = tb_null;

    // exit istream
    if (istream) tb_async_stream_exit(istream);
    istream = tb_null;

    // exit ostream
    if (ostream) tb_async_stream_exit(ostream);
    ostream = tb_null;

    // exit event
    if (event) tb_event_exit(event);
    event = tb_null;
    return 0;
}