diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-01-19 21:03:48 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-01-19 21:03:48 +0000 |
commit | 1a4e56397b3d009b725cc7a2a0913168bbe698d3 (patch) | |
tree | 36505e5730bc11d1b0ef13e612bd03f2385ef2bf /toxav | |
parent | f1d726a3a8d2515b08fd58a80650bb21fe0b3e67 (diff) |
Use more descriptive names in bwcontroller.
Diffstat (limited to 'toxav')
-rw-r--r-- | toxav/bwcontroller.c | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index 91e502b2..eeeab129 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c | |||
@@ -36,10 +36,6 @@ | |||
36 | #define BWC_REFRESH_INTERVAL_MS 10000 | 36 | #define BWC_REFRESH_INTERVAL_MS 10000 |
37 | #define BWC_AVG_PKT_COUNT 20 | 37 | #define BWC_AVG_PKT_COUNT 20 |
38 | 38 | ||
39 | /** | ||
40 | * | ||
41 | */ | ||
42 | |||
43 | struct BWController_s { | 39 | struct BWController_s { |
44 | void (*mcb)(BWController *, uint32_t, float, void *); | 40 | void (*mcb)(BWController *, uint32_t, float, void *); |
45 | void *mcb_data; | 41 | void *mcb_data; |
@@ -48,18 +44,23 @@ struct BWController_s { | |||
48 | uint32_t friend_number; | 44 | uint32_t friend_number; |
49 | 45 | ||
50 | struct { | 46 | struct { |
51 | uint32_t lru; /* Last recv update time stamp */ | 47 | uint32_t last_recv_timestamp; /* Last recv update time stamp */ |
52 | uint32_t lsu; /* Last sent update time stamp */ | 48 | uint32_t last_sent_timestamp; /* Last sent update time stamp */ |
53 | uint32_t lfu; /* Last refresh time stamp */ | 49 | uint32_t last_refresh_timestamp; /* Last refresh time stamp */ |
54 | 50 | ||
55 | uint32_t lost; | 51 | uint32_t lost; |
56 | uint32_t recv; | 52 | uint32_t recv; |
57 | } cycle; | 53 | } cycle; |
58 | 54 | ||
59 | struct { | 55 | struct { |
60 | uint32_t rb_s[BWC_AVG_PKT_COUNT]; | 56 | uint32_t packet_length_array[BWC_AVG_PKT_COUNT]; |
61 | RingBuffer *rb; | 57 | RingBuffer *rb; |
62 | } rcvpkt; /* To calculate average received packet */ | 58 | } rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */ |
59 | }; | ||
60 | |||
61 | struct BWCMessage { | ||
62 | uint32_t lost; | ||
63 | uint32_t recv; | ||
63 | }; | 64 | }; |
64 | 65 | ||
65 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object); | 66 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object); |
@@ -75,20 +76,21 @@ BWController *bwc_new(Messenger *m, uint32_t friendnumber, | |||
75 | retu->mcb_data = udata; | 76 | retu->mcb_data = udata; |
76 | retu->m = m; | 77 | retu->m = m; |
77 | retu->friend_number = friendnumber; | 78 | retu->friend_number = friendnumber; |
78 | retu->cycle.lsu = retu->cycle.lfu = current_time_monotonic(); | 79 | retu->cycle.last_sent_timestamp = retu->cycle.last_refresh_timestamp = current_time_monotonic(); |
79 | retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); | 80 | retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); |
80 | 81 | ||
81 | /* Fill with zeros */ | 82 | /* Fill with zeros */ |
82 | int i = 0; | 83 | int i = 0; |
83 | 84 | ||
84 | for (; i < BWC_AVG_PKT_COUNT; i ++) { | 85 | for (; i < BWC_AVG_PKT_COUNT; i ++) { |
85 | rb_write(retu->rcvpkt.rb, retu->rcvpkt.rb_s + i); | 86 | rb_write(retu->rcvpkt.rb, retu->rcvpkt.packet_length_array + i); |
86 | } | 87 | } |
87 | 88 | ||
88 | m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu); | 89 | m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu); |
89 | 90 | ||
90 | return retu; | 91 | return retu; |
91 | } | 92 | } |
93 | |||
92 | void bwc_kill(BWController *bwc) | 94 | void bwc_kill(BWController *bwc) |
93 | { | 95 | { |
94 | if (!bwc) { | 96 | if (!bwc) { |
@@ -100,6 +102,7 @@ void bwc_kill(BWController *bwc) | |||
100 | rb_kill(bwc->rcvpkt.rb); | 102 | rb_kill(bwc->rcvpkt.rb); |
101 | free(bwc); | 103 | free(bwc); |
102 | } | 104 | } |
105 | |||
103 | void bwc_feed_avg(BWController *bwc, uint32_t bytes) | 106 | void bwc_feed_avg(BWController *bwc, uint32_t bytes) |
104 | { | 107 | { |
105 | uint32_t *p; | 108 | uint32_t *p; |
@@ -109,13 +112,14 @@ void bwc_feed_avg(BWController *bwc, uint32_t bytes) | |||
109 | 112 | ||
110 | *p = bytes; | 113 | *p = bytes; |
111 | } | 114 | } |
112 | void bwc_add_lost(BWController *bwc, uint32_t bytes) | 115 | |
116 | void bwc_add_lost(BWController *bwc, uint32_t bytes_lost) | ||
113 | { | 117 | { |
114 | if (!bwc) { | 118 | if (!bwc) { |
115 | return; | 119 | return; |
116 | } | 120 | } |
117 | 121 | ||
118 | if (!bytes) { | 122 | if (!bytes_lost) { |
119 | uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1; | 123 | uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1; |
120 | 124 | ||
121 | rb_data(bwc->rcvpkt.rb, (void **) t_avg); | 125 | rb_data(bwc->rcvpkt.rb, (void **) t_avg); |
@@ -123,43 +127,39 @@ void bwc_add_lost(BWController *bwc, uint32_t bytes) | |||
123 | int i = 0; | 127 | int i = 0; |
124 | 128 | ||
125 | for (; i < BWC_AVG_PKT_COUNT; i ++) { | 129 | for (; i < BWC_AVG_PKT_COUNT; i ++) { |
126 | bytes += *(t_avg[i]); | 130 | bytes_lost += *(t_avg[i]); |
127 | 131 | ||
128 | if (*(t_avg[i])) { | 132 | if (*(t_avg[i])) { |
129 | c++; | 133 | c++; |
130 | } | 134 | } |
131 | } | 135 | } |
132 | 136 | ||
133 | bytes /= c; | 137 | bytes_lost /= c; |
134 | } | 138 | } |
135 | 139 | ||
136 | bwc->cycle.lost += bytes; | 140 | bwc->cycle.lost += bytes_lost; |
137 | send_update(bwc); | 141 | send_update(bwc); |
138 | } | 142 | } |
139 | void bwc_add_recv(BWController *bwc, uint32_t bytes) | 143 | |
144 | void bwc_add_recv(BWController *bwc, uint32_t recv_bytes) | ||
140 | { | 145 | { |
141 | if (!bwc || !bytes) { | 146 | if (!bwc || !recv_bytes) { |
142 | return; | 147 | return; |
143 | } | 148 | } |
144 | 149 | ||
145 | bwc->cycle.recv += bytes; | 150 | bwc->cycle.recv += recv_bytes; |
146 | send_update(bwc); | 151 | send_update(bwc); |
147 | } | 152 | } |
148 | 153 | ||
149 | 154 | ||
150 | struct BWCMessage { | ||
151 | uint32_t lost; | ||
152 | uint32_t recv; | ||
153 | }; | ||
154 | |||
155 | void send_update(BWController *bwc) | 155 | void send_update(BWController *bwc) |
156 | { | 156 | { |
157 | if (current_time_monotonic() - bwc->cycle.lfu > BWC_REFRESH_INTERVAL_MS) { | 157 | if (current_time_monotonic() - bwc->cycle.last_refresh_timestamp > BWC_REFRESH_INTERVAL_MS) { |
158 | 158 | ||
159 | bwc->cycle.lost /= 10; | 159 | bwc->cycle.lost /= 10; |
160 | bwc->cycle.recv /= 10; | 160 | bwc->cycle.recv /= 10; |
161 | bwc->cycle.lfu = current_time_monotonic(); | 161 | bwc->cycle.last_refresh_timestamp = current_time_monotonic(); |
162 | } else if (current_time_monotonic() - bwc->cycle.lsu > BWC_SEND_INTERVAL_MS) { | 162 | } else if (current_time_monotonic() - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) { |
163 | 163 | ||
164 | if (bwc->cycle.lost) { | 164 | if (bwc->cycle.lost) { |
165 | LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u", | 165 | LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u", |
@@ -177,20 +177,21 @@ void send_update(BWController *bwc) | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | bwc->cycle.lsu = current_time_monotonic(); | 180 | bwc->cycle.last_sent_timestamp = current_time_monotonic(); |
181 | } | 181 | } |
182 | } | 182 | } |
183 | |||
183 | static int on_update(BWController *bwc, const struct BWCMessage *msg) | 184 | static int on_update(BWController *bwc, const struct BWCMessage *msg) |
184 | { | 185 | { |
185 | LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", bwc); | 186 | LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", bwc); |
186 | 187 | ||
187 | /* Peer must respect time boundary */ | 188 | /* Peer must respect time boundary */ |
188 | if (current_time_monotonic() < bwc->cycle.lru + BWC_SEND_INTERVAL_MS) { | 189 | if (current_time_monotonic() < bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS) { |
189 | LOGGER_DEBUG(bwc->m->log, "%p Rejecting extra update", bwc); | 190 | LOGGER_DEBUG(bwc->m->log, "%p Rejecting extra update", bwc); |
190 | return -1; | 191 | return -1; |
191 | } | 192 | } |
192 | 193 | ||
193 | bwc->cycle.lru = current_time_monotonic(); | 194 | bwc->cycle.last_recv_timestamp = current_time_monotonic(); |
194 | 195 | ||
195 | uint32_t recv = net_ntohl(msg->recv); | 196 | uint32_t recv = net_ntohl(msg->recv); |
196 | uint32_t lost = net_ntohl(msg->lost); | 197 | uint32_t lost = net_ntohl(msg->lost); |
@@ -205,6 +206,7 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg) | |||
205 | 206 | ||
206 | return 0; | 207 | return 0; |
207 | } | 208 | } |
209 | |||
208 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object) | 210 | int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object) |
209 | { | 211 | { |
210 | if (length - 1 != sizeof(struct BWCMessage)) { | 212 | if (length - 1 != sizeof(struct BWCMessage)) { |