diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index 05fac704..460cf118 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c @@ -35,30 +35,34 @@ #include "../toxcore/util.h" #define BWC_PACKET_ID 196 -#define BWC_SEND_INTERVAL_MS 950 /* 0.95s */ +#define BWC_SEND_INTERVAL_MS 950 // 0.95s #define BWC_AVG_PKT_COUNT 20 #define BWC_AVG_LOSS_OVER_CYCLES_COUNT 30 +typedef struct BWCCycle { + uint32_t last_recv_timestamp; /* Last recv update time stamp */ + uint32_t last_sent_timestamp; /* Last sent update time stamp */ + uint32_t last_refresh_timestamp; /* Last refresh time stamp */ + + uint32_t lost; + uint32_t recv; +} BWCCycle; + +typedef struct BWCRcvPkt { + uint32_t packet_length_array[BWC_AVG_PKT_COUNT]; + RingBuffer *rb; +} BWCRcvPkt; + struct BWController_s { - void (*mcb)(BWController *, uint32_t, float, void *); - void *mcb_data; + m_cb *mcb; + void *mcb_user_data; Messenger *m; uint32_t friend_number; - struct { - uint32_t last_recv_timestamp; /* Last recv update time stamp */ - uint32_t last_sent_timestamp; /* Last sent update time stamp */ - uint32_t last_refresh_timestamp; /* Last refresh time stamp */ + BWCCycle cycle; - uint32_t lost; - uint32_t recv; - } cycle; - - struct { - uint32_t packet_length_array[BWC_AVG_PKT_COUNT]; - RingBuffer *rb; - } rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */ + BWCRcvPkt rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */ uint32_t packet_loss_counted_cycles; }; @@ -71,24 +75,24 @@ struct BWCMessage { int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object); void send_update(BWController *bwc); -BWController *bwc_new(Messenger *m, uint32_t friendnumber, - void (*mcb)(BWController *, uint32_t, float, void *), - void *udata) +BWController *bwc_new(Messenger *m, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data) { BWController *retu = (BWController *)calloc(sizeof(struct BWController_s), 1); LOGGER_DEBUG(m->log, "Creating bandwidth controller"); retu->mcb = mcb; - retu->mcb_data = udata; + retu->mcb_user_data = mcb_user_data; retu->m = m; retu->friend_number = friendnumber; - retu->cycle.last_sent_timestamp = retu->cycle.last_refresh_timestamp = current_time_monotonic(); + uint64_t now = current_time_monotonic(); + retu->cycle.last_sent_timestamp = now; + retu->cycle.last_refresh_timestamp = now; retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT); retu->cycle.lost = 0; retu->cycle.recv = 0; retu->packet_loss_counted_cycles = 0; /* Fill with zeros */ - for (int i = 0; i < BWC_AVG_PKT_COUNT; i++) { + for (int i = 0; i < BWC_AVG_PKT_COUNT; ++i) { rb_write(retu->rcvpkt.rb, &retu->rcvpkt.packet_length_array[i]); } @@ -126,7 +130,7 @@ void bwc_add_recv(BWController *bwc, uint32_t recv_bytes) return; } - bwc->packet_loss_counted_cycles++; + ++bwc->packet_loss_counted_cycles; bwc->cycle.recv += recv_bytes; send_update(bwc); } @@ -181,7 +185,7 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg) (((double) lost / (recv + lost)) * 100.0)); bwc->mcb(bwc, bwc->friend_number, ((float) lost / (recv + lost)), - bwc->mcb_data); + bwc->mcb_user_data); } return 0; diff --git a/toxav/bwcontroller.h b/toxav/bwcontroller.h index fdbc1746..c0e2dfc9 100644 --- a/toxav/bwcontroller.h +++ b/toxav/bwcontroller.h @@ -24,9 +24,9 @@ typedef struct BWController_s BWController; -BWController *bwc_new(Messenger *m, uint32_t friendnumber, - void (*mcb)(BWController *, uint32_t, float, void *), - void *udata); +typedef void m_cb(BWController *bwc, uint32_t friend_number, float todo, void *user_data); + +BWController *bwc_new(Messenger *m, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data); void bwc_kill(BWController *bwc);