static tb_bool_t tb_aicp_post_after_func(tb_aice_t const* aice) { // check tb_assert_and_check_return_val(aice && aice->aico && aice->code == TB_AICE_CODE_RUNTASK, tb_false); // the posted aice tb_aice_t* posted_aice = (tb_aice_t*)aice->priv; tb_assert_and_check_return_val(posted_aice && posted_aice->aico, tb_false); // the impl tb_aicp_impl_t* impl = (tb_aicp_impl_t*)tb_aico_aicp(aice->aico); tb_assert_and_check_return_val(impl && impl->ptor && impl->ptor->post, tb_false); // ok? tb_bool_t ok = tb_true; tb_bool_t posted = tb_true; if (aice->state == TB_STATE_OK) { // post it #ifdef __tb_debug__ if (!tb_aicp_post_((tb_aicp_ref_t)impl, posted_aice, ((tb_aico_impl_t*)aice->aico)->func, ((tb_aico_impl_t*)aice->aico)->line, ((tb_aico_impl_t*)aice->aico)->file)) #else if (!tb_aicp_post_((tb_aicp_ref_t)impl, posted_aice)) #endif { // not posted posted = tb_false; // failed posted_aice->state = TB_STATE_FAILED; } } // failed? else { // not posted posted = tb_false; // save state posted_aice->state = aice->state; } // not posted? done func now if (!posted) { // done func: notify failed if (posted_aice->func && !posted_aice->func(posted_aice)) ok = tb_false; } // exit the posted aice tb_free(posted_aice); // ok? return ok; }
tb_bool_t tb_aico_clos_(tb_aico_ref_t aico, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_CLOS; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; // closed? if (tb_aico_clos_try(aico)) { // close ok aice.state = TB_STATE_OK; // done func directly func(&aice); // ok return tb_true; } // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aicp_post_after_(tb_aicp_ref_t aicp, tb_size_t delay, tb_aice_ref_t aice __tb_debug_decl__) { // check tb_aicp_impl_t* impl = (tb_aicp_impl_t*)aicp; tb_assert_and_check_return_val(impl && impl->ptor && impl->ptor->post, tb_false); tb_assert_and_check_return_val(aice && aice->aico, tb_false); // killed? tb_check_return_val(!tb_atomic_get(&impl->kill_all), tb_false); // no delay? if (!delay) return tb_aicp_post_(aicp, aice __tb_debug_args__); // the aico tb_aico_impl_t* aico = (tb_aico_impl_t*)aice->aico; tb_assert_and_check_return_val(aico, tb_false); // make the posted aice tb_aice_ref_t posted_aice = tb_malloc0_type(tb_aice_t); tb_assert_and_check_return_val(posted_aice, tb_false); // init the posted aice *posted_aice = *aice; // run the delay task return tb_aico_task_run_((tb_aico_ref_t)aico, delay, tb_aicp_post_after_func, posted_aice __tb_debug_args__); }
tb_bool_t tb_aico_fsync_(tb_aico_ref_t aico, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_FSYNC; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_sendv_(tb_aico_ref_t aico, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp && list && size, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_SENDV; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.sendv.list = list; aice.u.sendv.size = size; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_urecv_(tb_aico_ref_t aico, tb_byte_t* data, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp && data && size, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_URECV; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.urecv.data = data; aice.u.urecv.size = size; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_conn_(tb_aico_ref_t aico, tb_ipv4_ref_t addr, tb_uint16_t port, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp && addr && addr->u32 && port, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_CONN; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.conn.port = port; aice.u.conn.addr = *addr; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_task_run_(tb_aico_ref_t aico, tb_size_t delay, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_RUNTASK; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.runtask.when = tb_cache_time_mclock() + delay; aice.u.runtask.delay = delay; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_sendf_(tb_aico_ref_t aico, tb_file_ref_t file, tb_hize_t seek, tb_hize_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp && file, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_SENDF; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.sendf.file = file; aice.u.sendf.seek = seek; aice.u.sendf.size = size; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }
tb_bool_t tb_aico_urecvv_(tb_aico_ref_t aico, tb_ipv4_ref_t addr, tb_uint16_t port, tb_iovec_t const* list, tb_size_t size, tb_aico_func_t func, tb_cpointer_t priv __tb_debug_decl__) { // check tb_aico_impl_t* impl = (tb_aico_impl_t*)aico; tb_assert_and_check_return_val(impl && impl->aicp && addr && addr->u32 && port && list && size, tb_false); // init tb_aice_t aice = {0}; aice.code = TB_AICE_CODE_URECVV; aice.state = TB_STATE_PENDING; aice.func = func; aice.priv = priv; aice.aico = aico; aice.u.urecvv.list = list; aice.u.urecvv.size = size; aice.u.urecvv.port = port; aice.u.urecvv.addr = *addr; // post return tb_aicp_post_(impl->aicp, &aice __tb_debug_args__); }