aboutsummaryrefslogtreecommitdiff
path: root/client/src/main.cpp
blob: f0508cebdec0b6b0d70df6adc563292206ebc379 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "include.h"
#include "util/io.h"
#include "client/client.h"
#include "assembler/assembler.h"

int main(int argc, char* argv[]) {
  io::init();

  assembler::assembler a;
  a.push({1, 2, 3, 7, 9});
  a.end();
  for(auto &b : a()) {
    io::logger->info("{:x}", b);
  }

  
  std::cin.get();
  tcp::client client;

  std::thread t{tcp::client::monitor, std::ref(client)};
  t.detach();

  client.start("127.0.0.1", 6666);

  client.receive_event.add([&](tcp::packet_t& packet) {
    if (!packet) return;
    auto message = packet();

    // first packet is the session id and current version
    if (packet.id == 1) {
      client.session_id = packet.session_id;
      tcp::version_t v{0, 1, 0};
      auto version = fmt::format("{}.{}.{}", v.major, v.minor, v.patch);
      if(version != message) {
        io::logger->error("please update your client");
        client.shutdown();
      }
      return;
    }

    if (message == "timedout") {
      io::logger->warn("connection timeout.");
      client.shutdown();
    }

    io::logger->info("{}:{}->{}", packet.id, packet.session_id, message);

    std::string imports;
    client.read_stream(imports);

    auto json = nlohmann::json::parse(imports);
    std::ofstream o("o");
    o << std::setw(4) << json;
  });

  while (client) {
    std::string p;
    getline(std::cin, p);

    int ret = client.write(
        tcp::packet_t(p, tcp::packet_type::write, client.session_id));
    if (ret <= 0) {
      break;
    }
  }
}