feat: progress AyuSync implementation

This commit is contained in:
ZavaruKitsu 2023-07-09 23:26:23 +00:00
parent 7323233b8d
commit 52b78be450
5 changed files with 31 additions and 10 deletions

View file

@ -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([=]
{

View file

@ -9,6 +9,8 @@
#include <sstream>
#include "ayu/libs/bit_converter.hpp"
using stringbuf = std::basic_stringbuf<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char>>;
template <class T>
void ayu_pipe_wrapper::send(T obj)
{
@ -30,25 +32,30 @@ std::optional<json> 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<int>(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());

View file

@ -22,5 +22,5 @@ public:
std::optional<json> receive();
private:
nes::pipe_istream is{"AyuSync"};
nes::basic_pipe_istream<unsigned char> is{"AyuSync"};
};

View file

@ -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<void()> callback)
{
auto timer = new QTimer();

View file

@ -15,5 +15,6 @@
#include "main/main_session.h"
Main::Session* getSession(ID userId);
bool accountExists(ID userId);
void dispatchToMainThread(std::function<void()> callback);
not_null<History*> getHistoryFromDialogId(ID dialogId, Main::Session* session);