示例#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
tb_bool_t tb_transfer_pool_done(tb_transfer_pool_ref_t pool, tb_char_t const* iurl, tb_char_t const* ourl, tb_hize_t offset, tb_size_t rate, tb_async_transfer_done_func_t done, tb_async_transfer_ctrl_func_t ctrl, tb_cpointer_t priv)
{
    // check
    tb_transfer_pool_impl_t* impl = (tb_transfer_pool_impl_t*)pool;
    tb_assert_and_check_return_val(impl && impl->aicp && iurl && ourl, tb_false);
    
    // enter
    tb_spinlock_enter(&impl->lock);

    // done
    tb_bool_t               ok = tb_false;
    tb_transfer_task_t*     task = tb_null;
    do
    {
        // check 
        tb_check_break(TB_STATE_OK == tb_atomic_get(&impl->state));

        // too many tasks?
        if (tb_list_entry_size(&impl->work) >= impl->maxn)
        {
            // trace
            tb_trace_e("too many tasks, done task: %s => %s failed!", iurl, ourl);
            break;
        }

        // init task
        task = tb_transfer_task_init(impl, done, ctrl, priv);
        tb_assert_and_check_break(task && task->transfer);

        // init transfer stream
        if (!tb_async_transfer_init_istream_from_url(task->transfer, iurl)) break;
        if (!tb_async_transfer_init_ostream_from_url(task->transfer, ourl)) break;

        // init transfer rate
        tb_async_transfer_limitrate(task->transfer, rate);

        // check
        tb_assert_and_check_break(impl->pool);

        // append to the work list
        tb_list_entry_insert_tail(&impl->work, &task->entry);

        // ok
        ok = tb_true;

    } while (0);

    // trace
    tb_trace_d("done: task: %p, %s => %s, work: %lu, idle: %lu, state: %s", task, iurl, ourl, tb_list_entry_size(&impl->work), tb_list_entry_size(&impl->idle), ok? "ok" : "no");

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

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

    // ok? done it
    if (ok && task && task->transfer)
    {
        // done
        if (!tb_async_transfer_open_done(task->transfer, 0, tb_transfer_task_done, task))
        {
            // enter
            tb_spinlock_enter(&impl->lock);

            // remove task from the work list
            tb_list_entry_remove(&impl->work, &task->entry);

            // exit task
            tb_transfer_task_exit(impl, task);

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

            // failed
            ok = tb_false;
        }
    }

    // ok?
    return ok;
}
示例#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;
}