Mstdlib-1.24.0
m_io_bwshaping.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2017 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_BWSHAPING_H__
25#define __M_IO_BWSHAPING_H__
26
27#include <mstdlib/base/m_defs.h>
28#include <mstdlib/base/m_types.h>
29#include <mstdlib/io/m_io.h>
30#include <mstdlib/io/m_event.h>
31
32__BEGIN_DECLS
33
34/*! \addtogroup m_io_bwshaping Addon for bandwidth shaping
35 * \ingroup m_eventio_base_addon
36 *
37 * Bandwidth Shaping Addon
38 *
39 * Allows tracking and altering data flow though an io object.
40 *
41 * Common uses:
42 * - Tracking bandwidth utilization.
43 * - Rate limiting download speed for connected clients.
44 * - Throttling to prevent network link saturation.
45 * - Testing real world network setups.
46 * - Determining how an application will perform in a bad environment.
47 *
48 * @{
49 */
50
51/*! Method of shaping. */
53 M_IO_BWSHAPING_MODE_BURST = 1, /*!< Allow bursting of data for each throttle period */
54 M_IO_BWSHAPING_MODE_TRICKLE = 2 /*!< Enforce data to be more evenly distributed across the throttle period */
55};
57
58
59/*! Shaping direction. */
63};
65
66/*! Add a BWShaping layer.
67 *
68 * Adding a layer without any settings will track bandwidth utilization
69 *
70 * \param[in] io io object.
71 * \param[out] layer_id Layer id this is added at.
72 *
73 * \return Result.
74 */
75M_API M_io_error_t M_io_add_bwshaping(M_io_t *io, size_t *layer_id);
76
77/*! Throttle data by bytes per seconds.
78 *
79 * \param[in] io io object.
80 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned form M_io_add_bwshaping.
81 * \param[in] direction Direction this applies to.
82 * \param[in] Bps Bytes per seconds that should be throttled. Bps = 0 is infinite.
83 *
84 * \return Result.
85 */
86M_API M_bool M_io_bwshaping_set_throttle(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 Bps);
87
88
89/*! Set how the data throttling should function.
90 *
91 * \param[in] io io object.
92 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned form M_io_add_bwshaping.
93 * \param[in] direction Direction this applies to.
94 * \param[in] mode Throttle mode.
95 *
96 * \return Result.
97 */
99
100
101/*! Set throttle period.
102 *
103 * The period must be longer than the sample frequency. Meaning period * 1000 > sample frequency.
104 *
105 * \param[in] io io object.
106 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned
107 * form M_io_add_bwshaping.
108 * \param[in] direction Direction this applies to.
109 * \param[in] period_s Period in seconds. Must be at least 1.
110 * \param[in] sample_frequency_ms Frequency in milliseconds. Must be at least 15.
111 *
112 * \return Result.
113 */
114M_API M_bool M_io_bwshaping_set_throttle_period(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 period_s, M_uint64 sample_frequency_ms);
115
116
117/*! Set latency.
118 *
119 * \param[in] io io object.
120 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned
121 * form M_io_add_bwshaping.
122 * \param[in] direction Direction this applies to.
123 * \param[in] latency_ms Latency in milliseconds. 0 is no latency.
124 *
125 * \return Result.
126 */
127M_API M_bool M_io_bwshaping_set_latency(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 latency_ms);
128
129
130/*! Get current Bps in period for direction
131 *
132 * \param[in] io io object.
133 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned
134 * form M_io_add_bwshaping.
135 * \param[in] direction Direction this applies to.
136 *
137 * \return Bps.
138 */
139M_API M_uint64 M_io_bwshaping_get_Bps(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction);
140
141
142
143/*! Get total number of bytes transferred for direction since beginning
144 *
145 * \param[in] io io object.
146 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned
147 * form M_io_add_bwshaping.
148 * \param[in] direction Direction this applies to.
149 *
150 * \return Count of bytes.
151 */
152M_API M_uint64 M_io_bwshaping_get_totalbytes(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction);
153
154/*! Get total time of connection in ms
155 *
156 * \param[in] io io object.
157 * \param[in] layer_id Layer id to apply this to. This should be the same layer id returned
158 * form M_io_add_bwshaping.
159 *
160 * \return Milliseconds.
161 */
162M_API M_uint64 M_io_bwshaping_get_totalms(M_io_t *io, size_t layer_id);
163
164/*! @} */
165
166__END_DECLS
167#endif
M_bool M_io_bwshaping_set_throttle_mode(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_io_bwshaping_mode_t mode)
enum M_io_bwshaping_direction M_io_bwshaping_direction_t
Definition: m_io_bwshaping.h:64
M_io_error_t M_io_add_bwshaping(M_io_t *io, size_t *layer_id)
M_bool M_io_bwshaping_set_throttle(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 Bps)
M_uint64 M_io_bwshaping_get_totalms(M_io_t *io, size_t layer_id)
M_io_bwshaping_direction
Definition: m_io_bwshaping.h:60
M_bool M_io_bwshaping_set_latency(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 latency_ms)
M_bool M_io_bwshaping_set_throttle_period(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction, M_uint64 period_s, M_uint64 sample_frequency_ms)
enum M_io_bwshaping_mode M_io_bwshaping_mode_t
Definition: m_io_bwshaping.h:56
M_uint64 M_io_bwshaping_get_Bps(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction)
M_io_bwshaping_mode
Definition: m_io_bwshaping.h:52
M_uint64 M_io_bwshaping_get_totalbytes(M_io_t *io, size_t layer_id, M_io_bwshaping_direction_t direction)
@ M_IO_BWSHAPING_DIRECTION_OUT
Definition: m_io_bwshaping.h:62
@ M_IO_BWSHAPING_DIRECTION_IN
Definition: m_io_bwshaping.h:61
@ M_IO_BWSHAPING_MODE_TRICKLE
Definition: m_io_bwshaping.h:54
@ M_IO_BWSHAPING_MODE_BURST
Definition: m_io_bwshaping.h:53
enum M_io_error M_io_error_t
Definition: m_io.h:93
struct M_io M_io_t
Definition: m_io.h:59