Repository navigation
Expand file tree
/
Copy pathsimulated_socket.h
More file actions
122 lines (105 loc) · 3.15 KB
/
Copy pathsimulated_socket.h
File metadata and controls
122 lines (105 loc) · 3.15 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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <chrono>
#include <cstddef>
#include <optional>
#include "core/context.h"
#include "jobs/concurrent_queue.h"
#include "networking/socket.h"
namespace iris
{
/**
* An adaptor for Socket which can simulate different network conditions. Note
* that these conditions are compounded with any properties of the underlying
* Socket.
*/
class SimulatedSocket : public Socket
{
public:
/**
* Construct a new SimulatedSocket.
*
* @param context
* Engine context object.
*
* @param delay
* The fixed delay for all packets.
*
* @param jitter
* The random variance in delay. All packets will be delayed by:
* delay + rand[-jitter, jitter]
*
* @param drop_rate
* The rate at which packets will be dropped, must be in the range
* [0.0, 1.0] -> [no packets dropped, all packets dropped]
*
* @param socket
* Socket to adapt. Will be used for underlying communication, but with
* simulated conditions.
*/
SimulatedSocket(
Context &context,
std::chrono::milliseconds delay,
std::chrono::milliseconds jitter,
float drop_rate,
Socket *socket);
// defined in implementation
~SimulatedSocket() override;
// deleted
SimulatedSocket(const SimulatedSocket &) = delete;
SimulatedSocket &operator=(const SimulatedSocket &) = delete;
/**
* Try and read requested number bytes. Will return all bytes read up to
* count, but maybe less.
*
* @param count
* Maximum number of bytes to read.
*
* @returns
* DataBuffer of bytes if read succeeded, otherwise empty optional.
*/
std::optional<DataBuffer> try_read(std::size_t count) override;
/**
* Block and read up to count bytes. May return less.
*
* @param count
* Maximum number of bytes to read.
*
* @returns
* DataBuffer of bytes read.
*/
DataBuffer read(std::size_t count) override;
/**
* Write DataBuffer to socket.
*
* @param buffer
* Bytes to write.
*/
void write(const DataBuffer &buffer) override;
/**
* Write bytes to socket.
*
* @param data
* Pointer to bytes to write.
*
* @param size
* Amount of bytes to write.
*/
void write(const std::byte *data, std::size_t size) override;
private:
/** Packet delay. */
std::chrono::milliseconds delay_;
/** Delay jitter. */
std::chrono::milliseconds jitter_;
/** Packet drop rate. */
float drop_rate_;
/** Underlying socket. */
Socket *socket_;
/** Queue of data to send and when. */
ConcurrentQueue<std::tuple<DataBuffer, std::chrono::steady_clock::time_point>> write_queue_;
};
}