2015-11-11 17:33:03 +08:00
|
|
|
/** bwcontroller.c
|
2015-10-11 05:54:23 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include "../toxcore/logger.h"
|
|
|
|
#include "../toxcore/util.h"
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "bwcontroller.h"
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
#define BWC_PACKET_ID 196
|
|
|
|
#define BWC_SEND_INTERVAL_MS 1000
|
|
|
|
#define BWC_REFRESH_INTERVAL_MS 10000
|
|
|
|
#define BWC_AVG_PKT_COUNT 20
|
|
|
|
|
|
|
|
/**
|
2015-11-04 02:42:05 +08:00
|
|
|
*
|
2015-10-11 05:54:23 +08:00
|
|
|
*/
|
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
struct BWController_s {
|
|
|
|
void (*mcb) (BWController *, uint32_t, float, void *);
|
2015-10-11 05:54:23 +08:00
|
|
|
void *mcb_data;
|
|
|
|
|
|
|
|
Messenger *m;
|
|
|
|
uint32_t friend_number;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t lru; /* Last recv update time stamp */
|
|
|
|
uint32_t lsu; /* Last sent update time stamp */
|
|
|
|
uint32_t lfu; /* Last refresh time stamp */
|
|
|
|
|
|
|
|
uint32_t lost;
|
|
|
|
uint32_t recv;
|
|
|
|
} cycle;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint32_t rb_s[BWC_AVG_PKT_COUNT];
|
|
|
|
RingBuffer *rb;
|
|
|
|
} rcvpkt; /* To calculate average received packet */
|
|
|
|
};
|
|
|
|
|
|
|
|
int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object);
|
2015-11-11 17:33:03 +08:00
|
|
|
void send_update(BWController *bwc);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
BWController *bwc_new(Messenger *m, uint32_t friendnumber,
|
2015-11-20 03:43:09 +08:00
|
|
|
void (*mcb) (BWController *, uint32_t, float, void *),
|
|
|
|
void *udata)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2015-11-11 17:33:03 +08:00
|
|
|
BWController *retu = calloc(sizeof(struct BWController_s), 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
retu->mcb = mcb;
|
|
|
|
retu->mcb_data = udata;
|
|
|
|
retu->m = m;
|
|
|
|
retu->friend_number = friendnumber;
|
|
|
|
retu->cycle.lsu = retu->cycle.lfu = current_time_monotonic();
|
|
|
|
retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT);
|
|
|
|
|
|
|
|
/* Fill with zeros */
|
|
|
|
int i = 0;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
for (; i < BWC_AVG_PKT_COUNT; i ++) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rb_write(retu->rcvpkt.rb, retu->rcvpkt.rb_s + i);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
|
|
|
m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return retu;
|
|
|
|
}
|
2015-11-11 17:33:03 +08:00
|
|
|
void bwc_kill(BWController *bwc)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!bwc) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, NULL, NULL);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
rb_kill(bwc->rcvpkt.rb);
|
|
|
|
free(bwc);
|
|
|
|
}
|
2015-11-11 17:33:03 +08:00
|
|
|
void bwc_feed_avg(BWController *bwc, uint32_t bytes)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
|
|
|
uint32_t *p;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
|
|
|
rb_read(bwc->rcvpkt.rb, (void **) &p);
|
2015-10-11 05:54:23 +08:00
|
|
|
rb_write(bwc->rcvpkt.rb, p);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
*p = bytes;
|
|
|
|
}
|
2015-11-11 17:33:03 +08:00
|
|
|
void bwc_add_lost(BWController *bwc, uint32_t bytes)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!bwc) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (!bytes) {
|
2015-11-04 02:42:05 +08:00
|
|
|
uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1;
|
|
|
|
|
|
|
|
rb_data(bwc->rcvpkt.rb, (void **) t_avg);
|
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
int i = 0;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
for (; i < BWC_AVG_PKT_COUNT; i ++) {
|
|
|
|
bytes += *(t_avg[i]);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (*(t_avg[i])) {
|
2015-10-11 05:54:23 +08:00
|
|
|
c++;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bytes /= c;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->cycle.lost += bytes;
|
|
|
|
send_update(bwc);
|
|
|
|
}
|
2015-11-11 17:33:03 +08:00
|
|
|
void bwc_add_recv(BWController *bwc, uint32_t bytes)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!bwc || !bytes) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->cycle.recv += bytes;
|
|
|
|
send_update(bwc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct BWCMessage {
|
|
|
|
uint32_t lost;
|
|
|
|
uint32_t recv;
|
2015-11-03 07:08:46 +08:00
|
|
|
};
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
void send_update(BWController *bwc)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
|
|
|
if (current_time_monotonic() - bwc->cycle.lfu > BWC_REFRESH_INTERVAL_MS) {
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->cycle.lost /= 10;
|
|
|
|
bwc->cycle.recv /= 10;
|
|
|
|
bwc->cycle.lfu = current_time_monotonic();
|
2015-11-04 02:42:05 +08:00
|
|
|
} else if (current_time_monotonic() - bwc->cycle.lsu > BWC_SEND_INTERVAL_MS) {
|
|
|
|
|
|
|
|
if (bwc->cycle.lost) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u",
|
|
|
|
bwc, bwc->cycle.recv, bwc->cycle.lost);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-11-03 07:08:46 +08:00
|
|
|
uint8_t p_msg[sizeof(struct BWCMessage) + 1];
|
2015-11-04 02:42:05 +08:00
|
|
|
struct BWCMessage *b_msg = (struct BWCMessage *)(p_msg + 1);
|
|
|
|
|
2015-11-03 07:08:46 +08:00
|
|
|
p_msg[0] = BWC_PACKET_ID;
|
|
|
|
b_msg->lost = htonl(bwc->cycle.lost);
|
|
|
|
b_msg->recv = htonl(bwc->cycle.recv);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (-1 == send_custom_lossy_packet(bwc->m, bwc->friend_number, p_msg, sizeof(p_msg))) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %d)! std error: %s", sizeof(p_msg), strerror(errno));
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->cycle.lsu = current_time_monotonic();
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 17:33:03 +08:00
|
|
|
int on_update (BWController *bwc, struct BWCMessage *msg)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", bwc);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Peer must respect time boundary */
|
|
|
|
if (current_time_monotonic() < bwc->cycle.lru + BWC_SEND_INTERVAL_MS) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "%p Rejecting extra update", bwc);
|
2015-10-11 05:54:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->cycle.lru = current_time_monotonic();
|
|
|
|
|
|
|
|
msg->recv = ntohl(msg->recv);
|
|
|
|
msg->lost = ntohl(msg->lost);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u", msg->recv, msg->lost);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (msg->lost && bwc->mcb) {
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->mcb(bwc, bwc->friend_number,
|
|
|
|
((float) (msg->lost) / (msg->recv + msg->lost)),
|
|
|
|
bwc->mcb_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length - 1 != sizeof(struct BWCMessage)) {
|
2015-10-11 06:01:44 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
/* NOTE the data is mutable */
|
2015-11-03 07:08:46 +08:00
|
|
|
return on_update(object, (struct BWCMessage *) (data + 1));
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|