aboutsummaryrefslogtreecommitdiff
path: root/client/src/main.cpp
blob: 7177511cd6d55a6c2ef89f44762f6cb5fc384f35 (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
#include "include.h"
#include "util/io.h"
#include "client/client.h"

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

  if (client.start("127.0.0.1", 6666)) {
    if(!client.set_session()) {
      io::logger->error("failed to set session id.");
      return 0;
    }

    io::logger->info("connected.");
    client.set_state(tcp::client_state::active);
  }

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

    io::logger->info(packet.message);
    if(packet.message == "stream") {
      std::vector<char> dat;
      client.read_stream(dat);

      std::ofstream o("out");
      o.write(dat.data(), dat.size());
      o.close();
    }
  });

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

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

    tcp::packet_t packet(p, tcp::packet_type::write, "1234567890");

    int ret = client.write(packet.message.data(), packet.message.size());
    if (ret <= 0) {
      break;
    }

  }

  t.join();
}