Compare commits

...

4 Commits

Author SHA1 Message Date
b0173f6d68 tox iterate interval pow(1.6)
fix faux offline inbetween timer
crop by default
2024-02-03 15:00:32 +01:00
3da5872df8 fix tffom and have it actually functioning 2024-02-03 01:05:50 +01:00
3deb6e8469 fix using bool for timestamps (oops) 2024-02-02 20:55:20 +01:00
0c674e0137 add tox friend faux offline message (still wonky) + small file copy error handling 2024-02-02 20:26:50 +01:00
7 changed files with 299 additions and 6 deletions

View File

@ -61,6 +61,9 @@ add_executable(tomato
./tox_dht_cap_histo.hpp
./tox_dht_cap_histo.cpp
./tox_friend_faux_offline_messaging.hpp
./tox_friend_faux_offline_messaging.cpp
./chat_gui4.hpp
./chat_gui4.cpp
)

View File

@ -96,13 +96,21 @@ const void* clipboard_callback(void* userdata, const char* mime_type, size_t* si
}
void ChatGui4::setClipboardData(std::vector<std::string> mime_types, std::shared_ptr<std::vector<uint8_t>>&& data) {
std::vector<const char*> tmp_mimetype_list;
for (const auto& mime_type : mime_types) {
tmp_mimetype_list.push_back(mime_type.data());
if (!static_cast<bool>(data)) {
std::cerr << "CG error: tried to set clipboard with empty shp\n";
return;
}
if (data->empty()) {
std::cerr << "CG error: tried to set clipboard with empty data\n";
return;
}
std::vector<const char*> tmp_mimetype_list;
std::lock_guard lg{_set_clipboard_data_mutex};
for (const auto& mime_type : mime_types) {
tmp_mimetype_list.push_back(mime_type.data());
_set_clipboard_data[mime_type] = data;
}

View File

@ -5,6 +5,7 @@
#include <SDL3/SDL.h>
#include <memory>
#include <cmath>
MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins) :
renderer(renderer_),
@ -16,6 +17,7 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri
tcm(cr, tc, tc),
tmm(rmm, cr, tcm, tc, tc),
ttm(rmm, cr, tcm, tc, tc),
tffom(cr, rmm, tcm, tc, tc),
mmil(rmm),
tam(rmm, cr, conf),
sdlrtu(renderer_),
@ -241,6 +243,8 @@ Screen* MainScreen::tick(float time_delta, bool& quit) {
tcm.iterate(time_delta); // compute
const float fo_interval = tffom.tick(time_delta);
tam.iterate(); // compute
const float pm_interval = pm.tick(time_delta); // compute
@ -250,9 +254,17 @@ Screen* MainScreen::tick(float time_delta, bool& quit) {
mts.iterate(); // compute
_min_tick_interval = std::min<float>(
tc.toxIterationInterval()/1000.f,
// HACK: pow by 1.6 to increase 50 -> ~500 (~522)
// and it does not change 1
std::pow(tc.toxIterationInterval(), 1.6f)/1000.f,
pm_interval
);
_min_tick_interval = std::min<float>(
_min_tick_interval,
fo_interval
);
//std::cout << "MS: min tick interval: " << _min_tick_interval << "\n";
switch (_compute_perf_mode) {
// normal 1ms lower bound

View File

@ -25,6 +25,7 @@
#include "./settings_window.hpp"
#include "./tox_ui_utils.hpp"
#include "./tox_dht_cap_histo.hpp"
#include "./tox_friend_faux_offline_messaging.hpp"
#include <string>
#include <iostream>
@ -52,6 +53,7 @@ struct MainScreen final : public Screen {
ToxContactModel2 tcm;
ToxMessageManager tmm;
ToxTransferManager ttm;
ToxFriendFauxOfflineMessaging tffom;
MediaMetaInfoLoader mmil;
ToxAvatarManager tam;
@ -67,7 +69,7 @@ struct MainScreen final : public Screen {
bool _show_tool_style_editor {false};
bool _window_hidden {false};
bool _window_hidden_ts {0};
uint64_t _window_hidden_ts {0};
float _time_since_event {0.f};
MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins);

View File

@ -32,7 +32,7 @@ struct SendImagePopup {
Rect crop_rect;
Rect crop_before_drag;
bool cropping {false};
bool cropping {true};
bool dragging_last_frame_ul {false};
bool dragging_last_frame_lr {false};

View File

@ -0,0 +1,213 @@
#include "./tox_friend_faux_offline_messaging.hpp"
#include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/contact/components.hpp>
#include <solanaceae/tox_contacts/components.hpp>
#include <solanaceae/message3/components.hpp>
#include <solanaceae/tox_messages/components.hpp>
#include <limits>
#include <cstdint>
//#include <iostream>
namespace Message::Components {
struct LastSendAttempt {
uint64_t ts {0};
};
} // Message::Components
namespace Contact::Components {
struct NextSendAttempt {
uint64_t ts {0};
};
} // Contact::Components
ToxFriendFauxOfflineMessaging::ToxFriendFauxOfflineMessaging(
Contact3Registry& cr,
RegistryMessageModel& rmm,
ToxContactModel2& tcm,
ToxI& t,
ToxEventProviderI& tep
) : _cr(cr), _rmm(rmm), _tcm(tcm), _t(t), _tep(tep) {
_tep.subscribe(this, Tox_Event_Type::TOX_EVENT_FRIEND_CONNECTION_STATUS);
}
float ToxFriendFauxOfflineMessaging::tick(float time_delta) {
_interval_timer -= time_delta;
if (_interval_timer > 0.f) {
return std::max(_interval_timer, 0.001f); // TODO: min next timer
}
// interval ~ once per minute
_interval_timer = 60.f;
const uint64_t ts_now = Message::getTimeMS();
// check ALL
// for each online tox friend
uint64_t min_next_attempt_ts {std::numeric_limits<uint64_t>::max()};
_cr.view<Contact::Components::ToxFriendEphemeral, Contact::Components::ConnectionState>()
.each([this, &min_next_attempt_ts, ts_now](const Contact3 c, const auto& tfe, const auto& cs) {
if (cs.state == Contact::Components::ConnectionState::disconnected) {
// cleanup
if (_cr.all_of<Contact::Components::NextSendAttempt>(c)) {
_cr.remove<Contact::Components::NextSendAttempt>(c);
auto* mr = static_cast<const RegistryMessageModel&>(_rmm).get(c);
if (mr != nullptr) {
mr->storage<Message::Components::LastSendAttempt>().clear();
}
}
} else {
if (!_cr.all_of<Contact::Components::NextSendAttempt>(c)) {
if (false) { // has unsent messages
const auto& nsa = _cr.emplace<Contact::Components::NextSendAttempt>(c, ts_now + uint64_t(_delay_after_cc*1000)); // wait before first message is sent
min_next_attempt_ts = std::min(min_next_attempt_ts, nsa.ts);
}
} else {
auto ret = doFriendMessageCheck(c, tfe);
if (ret == dfmc_Ret::SENT_THIS_TICK) {
const auto ts = _cr.get<Contact::Components::NextSendAttempt>(c).ts = ts_now + uint64_t(_delay_inbetween*1000);
min_next_attempt_ts = std::min(min_next_attempt_ts, ts);
} else if (ret == dfmc_Ret::TOO_SOON) {
// TODO: set to _delay_inbetween? prob expensive for no good reason
min_next_attempt_ts = std::min(min_next_attempt_ts, _cr.get<Contact::Components::NextSendAttempt>(c).ts);
} else {
_cr.remove<Contact::Components::NextSendAttempt>(c);
}
}
}
});
if (min_next_attempt_ts <= ts_now) {
// we (probably) sent this iterate
_interval_timer = 0.1f; // TODO: ugly magic
} else if (min_next_attempt_ts == std::numeric_limits<uint64_t>::max()) {
// nothing to sync or all offline that need syncing
} else {
_interval_timer = std::min(_interval_timer, (min_next_attempt_ts - ts_now) / 1000.f);
}
//std::cout << "TFFOM: iterate (i:" << _interval_timer << ")\n";
return _interval_timer;
}
ToxFriendFauxOfflineMessaging::dfmc_Ret ToxFriendFauxOfflineMessaging::doFriendMessageCheck(const Contact3 c, const Contact::Components::ToxFriendEphemeral& tfe) {
// walk all messages and check if
// unacked message
// timeouts for exising unacked messages expired (send)
auto* mr = static_cast<const RegistryMessageModel&>(_rmm).get(c);
if (mr == nullptr) {
// no messages
return dfmc_Ret::NO_MSG;
}
const uint64_t ts_now = Message::getTimeMS();
// filter for unconfirmed messages
// we assume sorted
// ("reverse" iteration <.<)
auto msg_view = mr->view<Message::Components::Timestamp>();
bool valid_unsent {false};
// we search for the oldest, not too recently sent, unconfirmed message
for (auto it = msg_view.rbegin(), view_end = msg_view.rend(); it != view_end; it++) {
const Message3 msg = *it;
// require
if (!mr->all_of<
Message::Components::MessageText, // text only for now
Message::Components::ContactTo
>(msg)
) {
continue; // skip
}
// exclude
if (mr->any_of<
Message::Components::Remote::TimestampReceived // this acts like a tag, which is wrong in groups
>(msg)
) {
continue; // skip
}
if (mr->get<Message::Components::ContactTo>(msg).c != c) {
continue; // not outbound (in private)
}
valid_unsent = true;
uint64_t msg_ts = msg_view.get<Message::Components::Timestamp>(msg).ts;
if (mr->all_of<Message::Components::TimestampWritten>(msg)) {
msg_ts = mr->get<Message::Components::TimestampWritten>(msg).ts;
}
if (mr->all_of<Message::Components::LastSendAttempt>(msg)) {
const auto lsa = mr->get<Message::Components::LastSendAttempt>(msg).ts;
if (lsa > msg_ts) {
msg_ts = lsa;
}
}
if (ts_now < (msg_ts + uint64_t(_delay_retry * 1000))) {
// not time yet
continue;
}
// it is time
const auto [msg_id, _] = _t.toxFriendSendMessage(
tfe.friend_number,
(
mr->all_of<Message::Components::TagMessageIsAction>(msg)
? Tox_Message_Type::TOX_MESSAGE_TYPE_ACTION
: Tox_Message_Type::TOX_MESSAGE_TYPE_NORMAL
),
mr->get<Message::Components::MessageText>(msg).text
);
// TODO: this is ugly
mr->emplace_or_replace<Message::Components::LastSendAttempt>(msg, ts_now);
if (msg_id.has_value()) {
// tmm will pick this up for us
mr->emplace_or_replace<Message::Components::ToxFriendMessageID>(msg, msg_id.value());
} // else error
// we sent our message, no point further iterating
return dfmc_Ret::SENT_THIS_TICK;
}
if (!valid_unsent) {
// somehow cleanup lsa
mr->storage<Message::Components::LastSendAttempt>().clear();
//std::cout << "TFFOM: all sent, deleting lsa\n";
return dfmc_Ret::NO_MSG;
}
return dfmc_Ret::TOO_SOON;
}
bool ToxFriendFauxOfflineMessaging::onToxEvent(const Tox_Event_Friend_Connection_Status* e) {
const auto friend_number = tox_event_friend_connection_status_get_friend_number(e);
const auto friend_status = tox_event_friend_connection_status_get_connection_status(e);
if (friend_status == Tox_Connection::TOX_CONNECTION_NONE) {
return false; // skip
// maybe cleanup?
}
auto c = _tcm.getContactFriend(friend_number);
if (!static_cast<bool>(c) || !c.all_of<Contact::Components::ToxFriendEphemeral, Contact::Components::ConnectionState>()) {
// UH error??
return false;
}
_cr.emplace_or_replace<Contact::Components::NextSendAttempt>(c, Message::getTimeMS() + uint64_t(_delay_after_cc*1000)); // wait before first message is sent
_interval_timer = 0.f;
return false;
}

View File

@ -0,0 +1,55 @@
#pragma once
#include <solanaceae/toxcore/tox_event_interface.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include <solanaceae/contact/contact_model3.hpp>
#include <solanaceae/message3/registry_message_model.hpp>
// fwd
struct ToxI;
namespace Contact::Components {
struct ToxFriendEphemeral;
}
// resends unconfirmed messages.
// timers get reset on connection changes, and send order is preserved.
class ToxFriendFauxOfflineMessaging : public ToxEventI {
Contact3Registry& _cr;
RegistryMessageModel& _rmm;
ToxContactModel2& _tcm;
ToxI& _t;
ToxEventProviderI& _tep;
float _interval_timer{0.f};
// TODO: increase timer?
const float _delay_after_cc {4.5f};
const float _delay_inbetween {0.3f};
const float _delay_retry {10.f}; // retry sending after 10s
public:
ToxFriendFauxOfflineMessaging(
Contact3Registry& cr,
RegistryMessageModel& rmm,
ToxContactModel2& tcm,
ToxI& t,
ToxEventProviderI& tep
);
float tick(float time_delta);
private:
enum class dfmc_Ret {
TOO_SOON,
SENT_THIS_TICK,
NO_MSG,
};
// only called for online friends
// returns true if a message was sent
// dont call this too often
dfmc_Ret doFriendMessageCheck(const Contact3 c, const Contact::Components::ToxFriendEphemeral& tfe);
protected:
bool onToxEvent(const Tox_Event_Friend_Connection_Status* e) override;
};