From 47b89ae57765ea86d52b074fa4d2e136b19472a5 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 8 Aug 2023 16:39:13 +0200 Subject: [PATCH] events, compiles again --- src/ngcft1.cpp | 28 +++++++++---- src/ngcft1.hpp | 98 ++++++++++++++++++++++++++++--------------- src/plugin_ngcft1.cpp | 11 ++++- 3 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/ngcft1.cpp b/src/ngcft1.cpp index 6726a9a..9f29264 100644 --- a/src/ngcft1.cpp +++ b/src/ngcft1.cpp @@ -169,8 +169,6 @@ void NGCFT1::updateSendTransfer(float time_delta, uint32_t group_number, uint32_ return; } - assert(ngc_ft1_ctx->cb_send_data.count(tf.file_kind)); - // if chunks in flight < window size (2) //while (tf.ssb.size() < ngc_ft1_ctx->options.packet_window_size) { int64_t can_packet_size {static_cast(peer.cca.canSend())}; @@ -198,14 +196,26 @@ void NGCFT1::updateSendTransfer(float time_delta, uint32_t group_number, uint32_ new_data.resize(chunk_size); - ngc_ft1_ctx->cb_send_data[tf.file_kind]( - tox, - group_number, peer_number, - idx, - tf.file_size_current, - new_data.data(), new_data.size(), - ngc_ft1_ctx->ud_send_data.count(tf.file_kind) ? ngc_ft1_ctx->ud_send_data.at(tf.file_kind) : nullptr + //ngc_ft1_ctx->cb_send_data[tf.file_kind]( + //tox, + //group_number, peer_number, + //idx, + //tf.file_size_current, + //new_data.data(), new_data.size(), + //ngc_ft1_ctx->ud_send_data.count(tf.file_kind) ? ngc_ft1_ctx->ud_send_data.at(tf.file_kind) : nullptr + //); + assert(idx <= 0xffu); + // TODO: check return value + dispatch( + NGCFT1_Event::send_data, + Events::NGCFT1_send_data{ + group_number, peer_number, + static_cast(idx), + tf.file_size_current, + new_data.data(), new_data.size(), + } ); + uint16_t seq_id = tf.ssb.add(std::move(new_data)); sendPKG_FT1_DATA(group_number, peer_number, idx, seq_id, tf.ssb.entries.at(seq_id).data.data(), tf.ssb.entries.at(seq_id).data.size()); peer.cca.onSent({idx, seq_id}, chunk_size); diff --git a/src/ngcft1.hpp b/src/ngcft1.hpp index 87b20a2..131f5c7 100644 --- a/src/ngcft1.hpp +++ b/src/ngcft1.hpp @@ -16,49 +16,79 @@ #include #include -// TODO: events -//typedef void NGC_FT1_recv_request_cb( - //Tox *tox, - //uint32_t group_number, uint32_t peer_number, - //const uint8_t* file_id, size_t file_id_size, - //void* user_data -//); +namespace Events { -// return true to accept, false to deny -//typedef bool NGC_FT1_recv_init_cb( - //Tox *tox, - //uint32_t group_number, uint32_t peer_number, - //const uint8_t* file_id, size_t file_id_size, - //const uint8_t transfer_id, - //const size_t file_size, - //void* user_data -//); + struct NGCFT1_recv_request { + uint32_t group_number; + uint32_t peer_number; -//typedef void NGC_FT1_recv_data_cb( - //Tox *tox, + NGCFT1_file_kind file_kind; - //uint32_t group_number, - //uint32_t peer_number, - //uint8_t transfer_id, + const uint8_t* file_id; + size_t file_id_size; + }; - //size_t data_offset, const uint8_t* data, size_t data_size, - //void* user_data -//); + struct NGCFT1_recv_init { + uint32_t group_number; + uint32_t peer_number; -// request to fill data_size bytes into data -//typedef void NGC_FT1_send_data_cb( - //Tox *tox, + NGCFT1_file_kind file_kind; - //uint32_t group_number, - //uint32_t peer_number, - //uint8_t transfer_id, + const uint8_t* file_id; + size_t file_id_size; - //size_t data_offset, uint8_t* data, size_t data_size, - //void* user_data -//); + const uint8_t transfer_id; + const size_t file_size; + // return true to accept, false to deny + bool& accept; + }; -class NGCFT1 : public ToxEventI, public NGCEXTEventI { + struct NGCFT1_recv_data { + uint32_t group_number; + uint32_t peer_number; + + uint8_t transfer_id; + + size_t data_offset; + const uint8_t* data; + size_t data_size; + }; + + // request to fill data_size bytes into data + struct NGCFT1_send_data { + uint32_t group_number; + uint32_t peer_number; + + uint8_t transfer_id; + + size_t data_offset; + uint8_t* data; + size_t data_size; + }; + +} // Events + +enum class NGCFT1_Event : uint8_t { + recv_request, + recv_init, + recv_data, + send_data, + + MAX +}; + +struct NGCFT1EventI { + using enumType = NGCFT1_Event; + virtual bool onEvent(const Events::NGCFT1_recv_request&) { return false; } + virtual bool onEvent(const Events::NGCFT1_recv_init&) { return false; } + virtual bool onEvent(const Events::NGCFT1_recv_data&) { return false; } + virtual bool onEvent(const Events::NGCFT1_send_data&) { return false; } // const? +}; + +using NGCFT1EventProviderI = EventProviderI; + +class NGCFT1 : public ToxEventI, public NGCEXTEventI, public NGCFT1EventProviderI { ToxI& _t; ToxEventProviderI& _tep; NGCEXTEventProviderI& _neep; diff --git a/src/plugin_ngcft1.cpp b/src/plugin_ngcft1.cpp index fd9c732..8886269 100644 --- a/src/plugin_ngcft1.cpp +++ b/src/plugin_ngcft1.cpp @@ -33,11 +33,18 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) return 1; } + ToxI* tox_i = nullptr; ToxEventProviderI* tox_event_provider_i = nullptr; { // make sure required types are loaded + tox_i = RESOLVE_INSTANCE(ToxI); tox_event_provider_i = RESOLVE_INSTANCE(ToxEventProviderI); + if (tox_i == nullptr) { + std::cerr << "PLUGIN NGCEXT missing ToxI\n"; + return 2; + } + if (tox_event_provider_i == nullptr) { std::cerr << "PLUGIN NGCEXT missing ToxEventProviderI\n"; return 2; @@ -47,10 +54,12 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) // static store, could be anywhere tho // construct with fetched dependencies g_ngcextep = std::make_unique(*tox_event_provider_i); - g_ngcft1 = std::make_unique(*tox_event_provider_i, *g_ngcextep.get()); + g_ngcft1 = std::make_unique(*tox_i, *tox_event_provider_i, *g_ngcextep.get()); // register types PROVIDE_INSTANCE(NGCEXTEventProviderI, "NGCEXT", g_ngcextep.get()); + + PROVIDE_INSTANCE(NGCFT1EventProviderI, "NGCEXT", g_ngcft1.get()); PROVIDE_INSTANCE(NGCFT1, "NGCEXT", g_ngcft1.get()); return 0;