示例#1
0
文件: null.c 项目: 1060460048/tbox
/* //////////////////////////////////////////////////////////////////////////////////////
 * 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;
}
示例#2
0
static tb_transfer_task_t* tb_transfer_task_init(tb_transfer_pool_impl_t* impl, tb_async_transfer_done_func_t done, tb_async_transfer_ctrl_func_t ctrl, tb_cpointer_t priv)
{
    // check
    tb_assert_and_check_return_val(impl, tb_null);

    // done
    tb_bool_t           ok = tb_false;
    tb_transfer_task_t* task = tb_null;
    do
    {
        // init task pool
        if (!impl->pool) impl->pool = tb_fixed_pool_init(tb_null, (impl->maxn >> 4) + 16, sizeof(tb_transfer_task_t), tb_null, tb_null, tb_null);
        tb_assert_and_check_break(impl->pool);

        // init task from the idle list first
        if (tb_list_entry_size(&impl->idle))
        {
            // get the head entry
            tb_list_entry_ref_t entry = tb_list_entry_head(&impl->idle);
            tb_assert_and_check_break(entry);

            // the task
            task = (tb_transfer_task_t*)tb_list_entry(&impl->idle, entry);
            tb_assert_and_check_break(task);

            // remove the last task
            tb_list_entry_remove(&impl->idle, entry);

            // check
            tb_assert_and_check_break(task->transfer);
        }
        // init task from the task pool
        else
        {
            // make task
            task = (tb_transfer_task_t*)tb_fixed_pool_malloc0(impl->pool);
            tb_assert_and_check_break(task);

            // init transfer
            task->transfer = tb_async_transfer_init(impl->aicp, tb_true);
            tb_assert_and_check_break(task->transfer);
        }

        // init ctrl
        if (ctrl && !tb_async_transfer_ctrl(task->transfer, ctrl, priv)) break;

        // init task
        task->func = done;
        task->priv = priv;
        task->pool = impl;

        // ok
        ok = tb_true;

    } while (0);

    // failed?
    if (!ok)
    {
        // exit it
        if (task) tb_transfer_task_exit(impl, task);
        task = tb_null;
    }

    // ok?
    return task;
}
示例#3
0
static tb_bool_t tb_demo_istream_open_func(tb_async_stream_ref_t stream, tb_size_t state, tb_cpointer_t priv)
{
    // check
    tb_demo_context_t* context = (tb_demo_context_t*)priv;
    tb_assert_and_check_return_val(stream && context && context->option, tb_false);

    // done
    tb_bool_t ok = tb_false;
    do
    {
        // check
        if (state != TB_STATE_OK)
        {
            // print verbose info
            if (context->verbose) 
            {
                tb_char_t const* url = tb_null;
                tb_async_stream_ctrl(stream, TB_STREAM_CTRL_GET_URL, &url);
                tb_printf("open: %s: %s\n", url, tb_state_cstr(state));
            }
            break;
        }
    
        // trace
        if (context->verbose) tb_printf("open: ok\n");

        // init ostream
        if (tb_option_find(context->option, "more0"))
        {
            // the path
            tb_char_t const* path = tb_option_item_cstr(context->option, "more0");

            // init
            context->ostream = tb_async_stream_init_from_file(tb_async_stream_aicp((tb_async_stream_ref_t)stream), path, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC);

            // print verbose info
            if (context->verbose) tb_printf("save: %s: ..\n", path);
        }
        else 
        {
            // the name
            tb_char_t const* name = tb_strrchr(tb_option_item_cstr(context->option, "url"), '/');
            if (!name) name = tb_strrchr(tb_option_item_cstr(context->option, "url"), '\\');
            if (!name) name = "/async_stream.file";

            // the path
            tb_char_t path[TB_PATH_MAXN] = {0};
            if (tb_directory_curt(path, TB_PATH_MAXN))
            {
                // append name
                tb_strcat(path, name);

                // init file
                context->ostream = tb_async_stream_init_from_file(tb_async_stream_aicp((tb_async_stream_ref_t)stream), path, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_BINARY | TB_FILE_MODE_TRUNC);

                // print verbose info
                if (context->verbose) tb_printf("save: %s: ..\n", path);
            }
        }
        tb_assert_and_check_break(context->ostream);

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

        // init transfer stream
        if (!tb_async_transfer_init_istream(context->transfer, stream)) break;
        if (!tb_async_transfer_init_ostream(context->transfer, context->ostream)) break;

        // the limit rate
        if (tb_option_find(context->option, "limitrate"))
            tb_async_transfer_limitrate(context->transfer, tb_option_item_uint32(context->option, "limitrate"));

        // open and done transfer
        if (!tb_async_transfer_open_done(context->transfer, 0, tb_demo_transfer_done_func, context)) break;

        // ok
        ok = tb_true;

    } while (0);

    // failed or closed? exit wait
    if (state != TB_STATE_OK && context->event) tb_event_post(context->event);

    // ok?
    return ok;
}
示例#4
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */ 
tb_int_t tb_demo_stream_async_transfer_main(tb_int_t argc, tb_char_t** argv)
{
    // done
    tb_async_transfer_ref_t transfer = tb_null;
    do
    {
        // 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_from_url(transfer, argv[1])) break;
        if (!tb_async_transfer_init_ostream_from_url(transfer, argv[2])) break;

        // limit rate
        tb_async_transfer_limitrate(transfer, argv[3]? tb_atoi(argv[3]) : 0);

        // trace
        tb_trace_i("done: ..");

        // open and done transfer
        if (!tb_async_transfer_open_done(transfer, 0, tb_demo_transfer_done_func, tb_null)) break;

        // wait
        getchar();

        // trace
        tb_trace_i("pause: ..");

        // pause transfer
        tb_async_transfer_pause(transfer);

        // wait 
        getchar();

        // trace
        tb_trace_i("resume: ..");

        // resume transfer
        if (!tb_async_transfer_resume(transfer)) break;

        // wait
        getchar();

        // trace
        tb_trace_i("done: ..");

        // open and done transfer
        if (!tb_async_transfer_open_done(transfer, 0, tb_demo_transfer_done_func, tb_null)) break;

        // wait
        getchar();

    } while (0);

    // trace
    tb_trace_i("exit: ..");

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

    return 0;
}