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

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

    tcp::client client;

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

    if(client.start("127.0.0.1", 6666)) {
        io::logger->info("connected.");
        client.set_state(tcp::client_state::active);
    }

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

        // first packet is the session id and current version
        if(packet.id == 1) {
            client.session_id = packet.session_id;
        }

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

    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;
        }
    }
}