add client to app
This commit is contained in:
@@ -6,4 +6,5 @@ namespace DnaStore
|
||||
void saveData(DnaManagerData *data);
|
||||
void saveVec(DnaManagerData *data);
|
||||
void saveGen(DnaManagerData *data);
|
||||
void sync();
|
||||
} // namespace DnaStore
|
||||
|
@@ -1,14 +1,21 @@
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
|
||||
#include "sys.hpp"
|
||||
#include "DnaStore.hpp"
|
||||
#include "TcpSocket.hpp"
|
||||
#include "NetConst.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#define ID_FILE_NAME "ID.bin"
|
||||
#define DATA_FILE_NAME "DATA.bin"
|
||||
#define VECTOR_FILE_NAME "VECTOR.bin"
|
||||
#define GEN_FILE_PATTRN "gen%04d.bin"
|
||||
|
||||
void DnaStore::load(DnaManagerData *data)
|
||||
{
|
||||
@@ -134,7 +141,92 @@ void DnaStore::saveGen(DnaManagerData *data)
|
||||
gen[i].liked = Liked::no;
|
||||
}
|
||||
|
||||
const char *fileName = TextFormat("gen%04d.bin", data->generation);
|
||||
const char *fileName = TextFormat(GEN_FILE_PATTRN, data->generation);
|
||||
|
||||
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
|
||||
sync();
|
||||
}
|
||||
|
||||
void client(std::string prefix);
|
||||
|
||||
void DnaStore::sync()
|
||||
{
|
||||
const char *prefix = sys::transformFilePath("");
|
||||
std::string prefixs = prefix;
|
||||
|
||||
std::thread t(client, prefixs);
|
||||
t.detach();
|
||||
}
|
||||
|
||||
void client(std::string prefix)
|
||||
{
|
||||
|
||||
constexpr int extra_buff = 20; // len of 2**31 -> 2147483648 plus the pattern size
|
||||
std::string buffer;
|
||||
buffer.resize(prefix.size() + extra_buff);
|
||||
|
||||
int sock = TcpSocket::connectt("petrovv.com", 8888);
|
||||
|
||||
if (sock < 0)
|
||||
{
|
||||
// printf("Error %d", sock);
|
||||
return;
|
||||
}
|
||||
|
||||
int ret = sprintf(buffer.data(), "%s/" ID_FILE_NAME, prefix.c_str());
|
||||
buffer.data()[ret] = 0;
|
||||
std::ifstream idf(buffer, std::ios_base::binary);
|
||||
|
||||
uint64_t ID = 0;
|
||||
|
||||
idf.read((char *)&ID, sizeof(ID));
|
||||
idf.close();
|
||||
|
||||
// printf("id: %ld\n", ID);
|
||||
|
||||
TcpSocket::sendt(sock, &StartHeader, sizeof(StartHeader));
|
||||
TcpSocket::sendt(sock, &ID, sizeof(ID));
|
||||
|
||||
Message message;
|
||||
|
||||
int needed_gen = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
TcpSocket::recvt(sock, &message, sizeof(Message));
|
||||
if (message.mess != Mess::REQ_SEND_GEN)
|
||||
{
|
||||
message.mess = Mess::RES_NO;
|
||||
TcpSocket::sendt(sock, &message, sizeof(Message));
|
||||
break;
|
||||
}
|
||||
|
||||
needed_gen = message.data;
|
||||
|
||||
int ret = sprintf(buffer.data(), "%s/" GEN_FILE_PATTRN, prefix.c_str(), needed_gen);
|
||||
buffer.data()[ret] = 0;
|
||||
|
||||
if (!std::filesystem::exists(buffer))
|
||||
{
|
||||
message.mess = Mess::RES_NO;
|
||||
TcpSocket::sendt(sock, &message, sizeof(Message));
|
||||
break;
|
||||
}
|
||||
|
||||
std::ifstream sfile(buffer, std::ios_base::binary | std::ios_base::in);
|
||||
|
||||
std::vector<NetUnit> net;
|
||||
net.resize(NUM_PER_GEN);
|
||||
|
||||
sfile.read((char *)net.data(), NUM_PER_GEN * sizeof(NetUnit));
|
||||
|
||||
message.mess = Mess::RES_OK;
|
||||
message.data = NUM_PER_GEN * sizeof(NetUnit);
|
||||
|
||||
TcpSocket::sendt(sock, &message, sizeof(Message));
|
||||
|
||||
TcpSocket::sendt(sock, net.data(), NUM_PER_GEN * sizeof(NetUnit));
|
||||
}
|
||||
|
||||
TcpSocket::closet(sock);
|
||||
}
|
||||
|
Reference in New Issue
Block a user