From 52b78be45068adbc58f32826cdd71beef571daea Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Sun, 9 Jul 2023 23:26:23 +0000 Subject: [PATCH] feat: progress AyuSync implementation --- .../ayu/sync/ayu_sync_controller.cpp | 16 ++++++++++++---- .../ayu/sync/utils/ayu_pipe_wrapper.cpp | 17 ++++++++++++----- .../ayu/sync/utils/ayu_pipe_wrapper.h | 2 +- .../ayu/sync/utils/telegram_helpers.cpp | 5 +++++ .../ayu/sync/utils/telegram_helpers.h | 1 + 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp index 4dd01a981..930233547 100644 --- a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp +++ b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp @@ -95,7 +95,11 @@ namespace AyuSync LOG(("userId: %1, type: %2").arg(userId).arg(type.c_str())); - // todo: check if account exists + if (!accountExists(userId)) + { + LOG(("Sync for unknown account: %1").arg(userId)); + return; + } if (type == "sync_force") { @@ -117,15 +121,19 @@ namespace AyuSync } } - DECLSPEC_NOINLINE void ayu_sync_controller::onSyncForce(SyncForce ev) + void ayu_sync_controller::onSyncForce(SyncForce ev) { } - DECLSPEC_NOINLINE void ayu_sync_controller::onSyncBatch(json ev) + void ayu_sync_controller::onSyncBatch(json ev) { + for (auto& item : ev["args"]["events"]) + { + invokeHandler(item); + } } - DECLSPEC_NOINLINE void ayu_sync_controller::onSyncRead(SyncRead ev) + void ayu_sync_controller::onSyncRead(SyncRead ev) { dispatchToMainThread([=] { diff --git a/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.cpp b/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.cpp index dcba70843..f23dedf2d 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.cpp +++ b/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.cpp @@ -9,6 +9,8 @@ #include #include "ayu/libs/bit_converter.hpp" +using stringbuf = std::basic_stringbuf, std::allocator>; + template void ayu_pipe_wrapper::send(T obj) { @@ -30,25 +32,30 @@ std::optional ayu_pipe_wrapper::receive() return std::nullopt; } - char lengthBuff[4]; + unsigned char lengthBuff[4]; is.read(lengthBuff, 4); auto length = bit_converter::bytes_to_i32(lengthBuff, false); + LOG(("ayu_pipe_wrapper::receive() length: %1").arg(length)); + if (length <= 0) { return std::nullopt; } - auto sb = std::stringbuf(); - char buff[4096]; + auto sb = stringbuf(); + unsigned char buff[4096]; while (length > 0) { auto readSize = std::min(length, static_cast(sizeof(buff))); is.read(buff, readSize); - sb.sputn(buff, readSize); - length -= readSize; + + auto reallyRead = is.gcount(); + + sb.sputn(buff, reallyRead); + length -= reallyRead; } auto p = json::parse(sb.str()); diff --git a/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.h b/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.h index 524a5e05e..70aba4dd1 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.h +++ b/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.h @@ -22,5 +22,5 @@ public: std::optional receive(); private: - nes::pipe_istream is{"AyuSync"}; + nes::basic_pipe_istream is{"AyuSync"}; }; diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp index 7bdf45ac0..1aec54062 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp @@ -32,6 +32,11 @@ Main::Session* getSession(ID userId) return nullptr; } +bool accountExists(ID userId) +{ + return userId == 0 || getSession(userId) != nullptr; +} + void dispatchToMainThread(std::function callback) { auto timer = new QTimer(); diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h index d7c1a0228..7d0782e28 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h @@ -15,5 +15,6 @@ #include "main/main_session.h" Main::Session* getSession(ID userId); +bool accountExists(ID userId); void dispatchToMainThread(std::function callback); not_null getHistoryFromDialogId(ID dialogId, Main::Session* session);