diff options
Diffstat (limited to 'toxav/bwcontroler.c')
-rw-r--r-- | toxav/bwcontroler.c | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/toxav/bwcontroler.c b/toxav/bwcontroler.c new file mode 100644 index 00000000..9dd15c93 --- /dev/null +++ b/toxav/bwcontroler.c | |||
@@ -0,0 +1,205 @@ | |||
1 | /** bwcontroler.c | ||
2 | * | ||
3 | * Copyright (C) 2013-2015 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #ifdef HAVE_CONFIG_H | ||
23 | #include "config.h" | ||
24 | #endif /* HAVE_CONFIG_H */ | ||
25 | |||
26 | #include <assert.h> | ||
27 | #include "bwcontroler.h" | ||
28 | #include "../toxcore/logger.h" | ||
29 | #include "../toxcore/util.h" | ||
30 | |||
31 | #define BWC_PACKET_ID 196 | ||
32 | #define BWC_SEND_INTERVAL_MS 1000 | ||
33 | #define BWC_REFRESH_INTERVAL_MS 10000 | ||
34 | #define BWC_AVG_PKT_COUNT 20 | ||
35 | |||
36 | /** | ||
37 | * | ||
38 | */ | ||
39 | |||
40 | struct BWControler_s { | ||
41 | void (*mcb) (BWControler *, uint32_t, float, void *); | ||
42 | void *mcb_data; | ||
43 | |||
44 | Messenger *m; | ||
45 | uint32_t friend_number; | ||
46 | |||
47 | struct { | ||
48 | uint32_t lru; /* Last recv update time stamp */ | ||
49 | uint32_t lsu; /* Last sent update time stamp */ | ||
50 | uint32_t lfu; /* Last refresh time stamp */ | ||
51 | |||
52 | uint32_t lost; | ||
53 | uint32_t recv; | ||
54 | } cycle; | ||
55 | |||
56 | struct { | ||
57 | uint32_t rb_s[BWC_AVG_PKT_COUNT]; | ||
58 | RingBuffer *rb; | ||
59 | } rcvpkt; /* To calculate average received packet */ | ||
60 | }; | ||
61 | |||
62 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object); | ||
63 | void send_update(BWControler *bwc); | ||
64 | |||
65 | BWControler *bwc_new(Messenger *m, uint32_t friendnumber, | ||
66 | void (*mcb) (BWControler *, uint32_t, float, void *), | ||
67 | void *udata) | ||
68 | { | ||
69 | BWControler *retu = calloc(sizeof(struct BWControler_s), 1); | ||
70 | |||
71 | retu->mcb = mcb; | ||
72 | retu->mcb_data = udata; | ||
73 | retu->m = m; | ||
74 | retu->friend_number = friendnumber; | ||
75 | retu->cycle.lsu = retu->cycle.lfu = current_time_monotonic(); | ||
76 | retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); | ||
77 | |||
78 | /* Fill with zeros */ | ||
79 | int i = 0; | ||
80 | |||
81 | for (; i < BWC_AVG_PKT_COUNT; i ++) | ||
82 | rb_write(retu->rcvpkt.rb, retu->rcvpkt.rb_s + i); | ||
83 | |||
84 | m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu); | ||
85 | |||
86 | return retu; | ||
87 | } | ||
88 | void bwc_kill(BWControler *bwc) | ||
89 | { | ||
90 | if (!bwc) | ||
91 | return; | ||
92 | |||
93 | m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, NULL, NULL); | ||
94 | |||
95 | rb_kill(bwc->rcvpkt.rb); | ||
96 | free(bwc); | ||
97 | } | ||
98 | void bwc_feed_avg(BWControler *bwc, uint32_t bytes) | ||
99 | { | ||
100 | uint32_t *p; | ||
101 | |||
102 | rb_read(bwc->rcvpkt.rb, (void **) &p); | ||
103 | rb_write(bwc->rcvpkt.rb, p); | ||
104 | |||
105 | *p = bytes; | ||
106 | } | ||
107 | void bwc_add_lost(BWControler *bwc, uint32_t bytes) | ||
108 | { | ||
109 | if (!bwc) | ||
110 | return; | ||
111 | |||
112 | if (!bytes) { | ||
113 | uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1; | ||
114 | |||
115 | rb_data(bwc->rcvpkt.rb, (void **) t_avg); | ||
116 | |||
117 | int i = 0; | ||
118 | |||
119 | for (; i < BWC_AVG_PKT_COUNT; i ++) { | ||
120 | bytes += *(t_avg[i]); | ||
121 | |||
122 | if (*(t_avg[i])) | ||
123 | c++; | ||
124 | } | ||
125 | |||
126 | bytes /= c; | ||
127 | } | ||
128 | |||
129 | bwc->cycle.lost += bytes; | ||
130 | send_update(bwc); | ||
131 | } | ||
132 | void bwc_add_recv(BWControler *bwc, uint32_t bytes) | ||
133 | { | ||
134 | if (!bwc || !bytes) | ||
135 | return; | ||
136 | |||
137 | bwc->cycle.recv += bytes; | ||
138 | send_update(bwc); | ||
139 | } | ||
140 | |||
141 | |||
142 | struct BWCMessage { | ||
143 | uint32_t lost; | ||
144 | uint32_t recv; | ||
145 | }; | ||
146 | |||
147 | void send_update(BWControler *bwc) | ||
148 | { | ||
149 | if (current_time_monotonic() - bwc->cycle.lfu > BWC_REFRESH_INTERVAL_MS) { | ||
150 | |||
151 | bwc->cycle.lost /= 10; | ||
152 | bwc->cycle.recv /= 10; | ||
153 | bwc->cycle.lfu = current_time_monotonic(); | ||
154 | } else if (current_time_monotonic() - bwc->cycle.lsu > BWC_SEND_INTERVAL_MS) { | ||
155 | |||
156 | if (bwc->cycle.lost) { | ||
157 | LOGGER_DEBUG ("%p Sent update rcv: %u lost: %u", | ||
158 | bwc, bwc->cycle.recv, bwc->cycle.lost); | ||
159 | |||
160 | uint8_t p_msg[sizeof(struct BWCMessage) + 1]; | ||
161 | struct BWCMessage *b_msg = (struct BWCMessage *)(p_msg + 1); | ||
162 | |||
163 | p_msg[0] = BWC_PACKET_ID; | ||
164 | b_msg->lost = htonl(bwc->cycle.lost); | ||
165 | b_msg->recv = htonl(bwc->cycle.recv); | ||
166 | |||
167 | if (-1 == send_custom_lossy_packet(bwc->m, bwc->friend_number, p_msg, sizeof(p_msg))) | ||
168 | LOGGER_WARNING("BWC send failed (len: %d)! std error: %s", sizeof(p_msg), strerror(errno)); | ||
169 | } | ||
170 | |||
171 | bwc->cycle.lsu = current_time_monotonic(); | ||
172 | } | ||
173 | } | ||
174 | int on_update (BWControler *bwc, struct BWCMessage *msg) | ||
175 | { | ||
176 | LOGGER_DEBUG ("%p Got update from peer", bwc); | ||
177 | |||
178 | /* Peer must respect time boundary */ | ||
179 | if (current_time_monotonic() < bwc->cycle.lru + BWC_SEND_INTERVAL_MS) { | ||
180 | LOGGER_DEBUG("%p Rejecting extra update", bwc); | ||
181 | return -1; | ||
182 | } | ||
183 | |||
184 | bwc->cycle.lru = current_time_monotonic(); | ||
185 | |||
186 | msg->recv = ntohl(msg->recv); | ||
187 | msg->lost = ntohl(msg->lost); | ||
188 | |||
189 | LOGGER_DEBUG ("recved: %u lost: %u", msg->recv, msg->lost); | ||
190 | |||
191 | if (msg->lost && bwc->mcb) | ||
192 | bwc->mcb(bwc, bwc->friend_number, | ||
193 | ((float) (msg->lost) / (msg->recv + msg->lost)), | ||
194 | bwc->mcb_data); | ||
195 | |||
196 | return 0; | ||
197 | } | ||
198 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object) | ||
199 | { | ||
200 | if (length - 1 != sizeof(struct BWCMessage)) | ||
201 | return -1; | ||
202 | |||
203 | /* NOTE the data is mutable */ | ||
204 | return on_update(object, (struct BWCMessage *) (data + 1)); | ||
205 | } | ||