AyuGramDesktop/Telegram/SourceFiles/ayu/sync/utils/ayu_pipe_wrapper.cpp
ZavaruKitsu f62eea3952 feat: refactor
feat: use another JSON library
fix: specify credits
2023-07-01 19:42:03 +03:00

43 lines
No EOL
1 KiB
C++

// This is the source code of AyuGram for Desktop.
//
// We do not and cannot prevent the use of our code,
// but be respectful and credit the original author.
//
// Copyright @Radolyn, 2023
#include <sstream>
#include <iostream>
#include "ayu_pipe_wrapper.h"
#include "ayu/libs/bit_converter.hpp"
template<class T>
void ayu_pipe_wrapper::send(T obj) {
auto s = json(obj).dump();
auto length = s.length();
char lengthBuff[4];
bit_converter::i32_to_bytes(length, false, lengthBuff);
os.write(lengthBuff, 4);
os.write(s.c_str(), length);
os.flush();
}
json ayu_pipe_wrapper::receive() {
char lengthBuff[4];
is.read(lengthBuff, 4);
auto length = bit_converter::bytes_to_i32(lengthBuff, false);
auto sb = std::stringbuf();
char buff[4096];
while (length > 0) {
auto readSize = std::min(length, (int)sizeof(buff));
is.read(buff, readSize);
sb.sputn(buff, readSize);
length -= readSize;
}
auto p = json::parse(sb.str());
return p;
}