forked from sorokin/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_tester.cpp
152 lines (134 loc) · 3.84 KB
/
echo_tester.cpp
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
150
151
152
#include "echo_tester.h"
#include <iostream>
echo_tester::connection::connection(epoll &ep, ipv4_endpoint const& remote, uint32_t number)
: socket(client_socket::connect(ep, remote, [] {}))
, number(number)
, sent(0)
, received(0)
, no_disconnect(false)
, no_read(false)
{}
void echo_tester::connection::do_send()
{
size_t size = rand() % 10000;
size_t sent_total = 0;
std::cout << "sending " << size << " bytes over connection " << number;
uint8_t buf[2000];
for (;;)
{
if (size == sent_total)
{
std::cout << std::endl;
break;
}
size_t to_send = std::min(size - sent_total, sizeof buf);
fill_buffer(buf, to_send);
size_t sent_now = socket.write_some(buf, to_send);
sent += sent_now;
sent_total += sent_now;
if (sent_now != to_send)
{
std::cout << " (" << sent_total << " bytes are sent)" << std::endl;
break;
}
}
}
bool echo_tester::connection::do_receive()
{
assert(sent >= received);
if (sent == received)
return true;
//std::cout << "receiving " << (sent - received) << " bytes over connection " << number << std::endl;
size_t size = rand() % (sent - received);
uint8_t buf[2000];
while (size != 0)
{
size_t to_receive = std::min(size, sizeof buf);
size_t bytes_received = socket.read_some(buf, to_receive);
if (bytes_received == 0)
{
std::cerr << "incomplete read (connection " << number << "), requested: " << to_receive << ", received: " << bytes_received << std::endl;
return false;
}
if (!check_buffer(buf, bytes_received))
return false;
size -= bytes_received;
}
return true;
}
void echo_tester::connection::fill_buffer(uint8_t *buf, size_t size)
{
uint8_t val = (uint8_t)sent;
for (size_t i = 0; i != size; ++i)
*buf++ = val++;
}
bool echo_tester::connection::check_buffer(uint8_t *buf, size_t size)
{
for (size_t i = 0; i != size; ++i)
{
if (*buf != (uint8_t)received)
{
std::cerr << "mismatch, expected: " << (uint32_t)(uint8_t)received << "(" << received << "), received: " << (uint32_t)*buf << std::endl;
return false;
}
++buf;
++received;
}
return true;
}
uint32_t echo_tester::connection::get_number() const
{
return number;
}
echo_tester::echo_tester(epoll &ep, ipv4_endpoint remote_endpoint)
: ep(ep)
, remote_endpoint(remote_endpoint)
, next_connection_number(0)
, desired_number_of_connections(4)
, number_of_permanent_connections(0)
{}
bool echo_tester::do_step()
{
size_t i = rand() % (desired_number_of_connections * 2);
if (i < connections.size())
{
auto& c = *connections[i];
size_t act = rand() % 112;
if (number_of_permanent_connections < 2 && act == 111)
{
++number_of_permanent_connections;
c.no_disconnect = true;
}
else if (!c.no_read && act == 110)
{
c.no_read = true;
}
else if (!c.no_disconnect && act >= 100)
{
//std::cout << "destroy connection " << connections[i]->get_number() << std::endl;
std::swap(connections[i], connections.back());
connections.pop_back();
}
else if (!c.no_read && act >= 50)
{
if (!c.do_receive())
return false;
}
else
{
c.do_send();
}
}
else
{
std::unique_ptr<connection> cc(new connection(ep, remote_endpoint, next_connection_number++));
connections.push_back(std::move(cc));
}
return true;
}
void echo_tester::do_n_steps(size_t n)
{
for (size_t i = 0; i != n; ++i)
if (!do_step())
break;
}