-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (104 loc) · 3.98 KB
/
Copy pathmain.cpp
File metadata and controls
130 lines (104 loc) · 3.98 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <qsox/UdpSocket.hpp>
#include <qsox/TcpStream.hpp>
#include <arc/prelude.hpp>
#include <qunet/dns/Resolver.hpp>
#include <qunet/Connection.hpp>
#include <qunet/Log.hpp>
#include <asp/time.hpp>
#include <fmt/color.h>
#include <fmt/ranges.h>
#ifdef QUNET_TLS_SUPPORT
# include <xtls/Backend.hpp>
#endif
using namespace qn;
using namespace asp::time;
using namespace arc;
arc::Future<> connLoop(Connection& conn) {
constexpr static size_t TRIES = 32768;
std::atomic<size_t> inFlight{0};
std::vector<std::vector<uint8_t>> messages{1};
auto [tx, rx] = arc::mpsc::channel<std::vector<uint8_t>>();
conn.setDataCallback([&](std::vector<uint8_t> data) {
tx.trySend(std::move(data)).unwrap();
});
auto receiver = arc::spawn([&] -> arc::Future<> {
for (size_t i = 1; i < TRIES; i++) {
auto val = (co_await rx.recv()).unwrap();
inFlight--;
if (val != messages[i]) {
fmt::println("Received incorrect message {} ({} bytes)\n", i, val.size());
} else {
fmt::print("Received good message {} ({} bytes)\n", i, val.size());
}
}
});
for (size_t i = 1; i < TRIES; i++) {
std::vector<uint8_t> msg(i);
for (size_t j = 0; j < i; j++) {
msg[j] = (uint8_t)rand();
}
messages.push_back(std::move(msg));
log::debug("Send {} bytes", messages.back().size());
conn.sendData(messages.back(), true);
inFlight++;
while (inFlight > 5) {
co_await arc::sleep(asp::time::Duration::fromMillis(10));
}
co_await arc::sleep(asp::time::Duration::fromMillis(1));
}
co_await receiver;
}
arc::Future<int> amain(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
co_return 1;
}
static Instant start = Instant::now();
qn::log::setLogFunction([&](qn::log::Level level, const std::string& message) {
auto timestr = fmt::format("{:.6f}", start.elapsed().seconds<double>());
switch (level) {
#ifdef QUNET_DEBUG
case qn::log::Level::Trace: fmt::println("[{}] [{}] {}", timestr, styled("TRACE", fg(fmt::color::gray)), message); break;
#endif
case qn::log::Level::Debug: fmt::println("[{}] [{}] {}", timestr, styled("DEBUG", fg(fmt::color::gray)), message); break;
case qn::log::Level::Info: fmt::println("[{}] [{}] {}", timestr, styled("INFO", fg(fmt::color::cyan)), message); break;
case qn::log::Level::Warning: fmt::println("[{}] [{}] {}", timestr, styled("WARN", fg(fmt::color::yellow)), message); break;
case qn::log::Level::Error: fmt::println("[{}] [{}] {}", timestr, styled("ERROR", fg(fmt::color::indian_red)), message); break;
}
});
// if (auto err = (co_await testTLS()).err()) {
// std::cerr << "DOT test failed: " << err->message() << std::endl;
// co_return 1;
// }
// co_return 1;
auto conn = co_await Connection::create();
conn->setQdbFolder("./qdb-storage");
conn->setDebugOptions(ConnectionDebugOptions {
// .packetLossSimulation = 0.1f,
});
#ifdef QUNET_QUIC_SUPPORT
auto ctx = xtls::Backend::get().createContext(xtls::ContextType::Client1_3).unwrap();
ctx->setCertVerification(false).unwrap();
setupQuicContext(*ctx).unwrap();
conn->setQuicTlsContext(ctx);
#endif
auto res = co_await conn->connectWait(argv[1]);
if (!res) {
std::cerr << "Failed to connect: " << res.unwrapErr().message() << std::endl;
co_return 1;
}
co_await arc::select(
arc::selectee(connLoop(*conn)),
arc::selectee(arc::ctrl_c())
);
conn->disconnect();
while (!conn->disconnected()) {
co_await arc::sleep(asp::time::Duration::fromMillis(10));
}
conn->destroy();
// wait a bit for cleanup
co_await arc::sleep(asp::time::Duration::fromMillis(100));
co_return 0;
}
ARC_DEFINE_MAIN_NT(amain, 1);