Add sqlite and start work on comunication protocol

This commit is contained in:
2025-01-24 15:57:23 +01:00
parent a226d0f156
commit 4da7788a15
9 changed files with 266 additions and 31 deletions

View File

@@ -1,28 +1,103 @@
#include "TcpSocket.hpp"
#include "NetConst.hpp"
#include "sql.hpp"
#include "values/DnaManager.hpp"
#include <iostream>
using namespace std;
#include <map>
// use pthread rw_lock to lock db so you can safy clone .db file
typedef std::vector<NetUnit> NetList;
typedef std::vector<NetList> UserList;
std::map<uint64_t, UserList> data;
NetList generate_first(uint64_t id)
{
std::vector<NetUnit> ret;
ret.resize(NUM_PER_GEN);
uint128 randSeed = mrand::getState(id);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
Dna dna;
DNA::newDna(&dna, &randSeed);
ret[i].hash = mrand::computeCRC32(&dna, sizeof(Dna));
ret[i].liked = Liked::tbd;
ret[i].index = i;
}
return ret;
}
// When a new client connected:
void call(int sock, sockaddr_in newSocketInfo)
{
std::cout << "new User" << std::endl;
char tempBuffer[AS_DEFAULT_BUFFER_SIZE + 1];
ssize_t messageLength;
while ((messageLength = TcpSocket::recvt(sock, tempBuffer, AS_DEFAULT_BUFFER_SIZE)) > 0)
uint64_t conf, id;
TcpSocket::recvt(sock, &conf, sizeof(conf));
TcpSocket::recvt(sock, &id, sizeof(id));
if (conf != StartHeader)
{
tempBuffer[messageLength] = '\0';
TcpSocket::sendt(sock, tempBuffer, messageLength);
TcpSocket::closet(sock);
return;
}
if (id == 0)
{
printf("ID ERROR\n");
return;
}
auto found = data.find(id);
if (found == data.end())
{
printf("notfound\n");
UserList list;
NetList netList = generate_first(id);
list.push_back(netList);
data.insert(std::pair<uint64_t, UserList>(id, list));
}
found = data.find(id);
// repete point
size_t gen = found->second.size() - 1;
TcpSocket::sendt(sock, &gen, sizeof(size_t));
NetList list;
list.resize(NUM_PER_GEN);
TcpSocket::recvt(sock, list.data(), NUM_PER_GEN * sizeof(NetUnit));
for (size_t i = 0; i < NUM_PER_GEN; i++)
{
if (found->second[0][i].hash != list[i].hash)
{
found->second[0][i].liked != list[i].liked;
printf("Not same\n");
}
}
// generate the next generation hash form the new info and repete form repete point
printf("same\n");
printf("id: %ld\n", id);
std::cout << "del USER" << std::endl;
TcpSocket::closet(sock);
}
int main()
{
sql::init();
// Bind the server to a port.
int err = TcpSocket::listent("0.0.0.0", 8888, call);
if (err < 0)