-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtestclient.cpp
More file actions
149 lines (117 loc) · 3.56 KB
/
Copy pathtestclient.cpp
File metadata and controls
149 lines (117 loc) · 3.56 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include "rtmfp/PosixPlatformAdapter.hpp"
#include "rtmfp/PlainCryptoAdapter.hpp"
#include "rtmfp/RunLoops.hpp"
#include "addrlist.hpp"
using namespace com::zenomt;
using namespace com::zenomt::rtmfp;
namespace {
class LossyPlatformAdapter : public PosixPlatformAdapter {
public:
LossyPlatformAdapter(RunLoop *runloop) :
PosixPlatformAdapter(runloop),
m_thresh(0)
{}
bool writePacket(const void *bytes, size_t len, int interfaceID, const struct sockaddr *addr, socklen_t addrLen, int tos) override
{
if((rand() % 10000) < m_thresh)
len = 0;
return PosixPlatformAdapter::writePacket(bytes, len, interfaceID, addr, addrLen, tos);
}
void setThresh(double thresh)
{
m_thresh = thresh * 10000;
}
int m_thresh;
};
}
static int usage(const char *name, const char *message = nullptr)
{
if(message)
printf("%s\n", message);
printf("usage: %s [-4] [-p port] [-n name] [-l lossrate] dstname dstaddr dstport [dstaddr dstport...]\n", name);
return 1;
}
static std::shared_ptr<SendFlow> openFlow(const std::shared_ptr<RTMFP> &rtmfp, const char *dst, Priority pri)
{
auto flow = rtmfp->openFlow(dst, strlen(dst), "metadata", 8, pri);
flow->onWritable = [flow, pri] {
uint8_t buf[4096] = { 0 };
printf("became writable!\n");
for(int count = 0; count < 60; count++)
flow->write(buf, sizeof(buf), 13)->onFinished = [count, pri, flow] (bool abn) {
printf("onFinished %d:%d (%d) adv:%lu inflight:%lu\n", pri, count, abn, (unsigned long)flow->getRecvBufferBytesAvailable(), (unsigned long)flow->getOutstandingBytes());
fflush(stdout);
};
flow->close();
return false;
};
flow->notifyWhenWritable();
flow->onException = [] (uintmax_t reason) { printf("flow exception: %lu\n", reason); };
return flow;
}
int main(int argc, char * const argv[])
{
int port = 0;
int family = AF_INET6;
const char *name = argv[0];
const char *dstName;
int ch;
double thresh = 0;
srand(time(nullptr));
while((ch = getopt(argc, argv, "h4p:n:l:")) != -1)
{
switch(ch)
{
case '4':
family = AF_INET;
break;
case 'p':
port = atoi(optarg);
break;
case 'n':
name = optarg;
break;
case 'l':
thresh = atof(optarg);
break;
case 'h':
default:
return usage(argv[0]);
}
}
if(argc - optind < 3)
return usage(argv[0], "specify dstname dstaddr dstport");
dstName = argv[optind++];
std::vector<Address> dstAddrs;
if(not addrlist_parse(argc, argv, optind, false, dstAddrs))
return 1;
PreferredRunLoop rl;
LossyPlatformAdapter platform(&rl);
platform.setThresh(thresh);
platform.onShutdownCompleteCallback = [&rl] { printf("shutdown complete\n"); rl.stop(); };
PlainCryptoAdapter crypto(name);
auto instance = share_ref(new RTMFP(&platform, &crypto), false);
platform.setRtmfp(instance.get());
instance->setDefaultSessionKeepalivePeriod(10);
instance->setDefaultSessionRetransmitLimit(10);
instance->setDefaultSessionIdleLimit(10);
auto addr = platform.addUdpInterface(port, family);
assert(addr);
printf("got port %d\n", addr->getPort());
auto flow = openFlow(instance, dstName, PRI_ROUTINE);
add_candidates(flow, dstAddrs);
flow->write("early", 5)->onFinished = [] (bool abn) { printf("early onFinished (%d)\n", abn); };
openFlow(instance, dstName, PRI_PRIORITY);
// rl.scheduleRel(Timer::makeAction([] (Time now) { printf("fire %Lf\n", now); }), 0, 1);
rl.scheduleRel(Timer::makeAction([instance] { printf("shutting down\n"); instance->shutdown(true); }), 30, 0);
rl.run();
platform.close();
instance.reset();
printf("end.\n");
return 0;
}