Skip to content

Commit

Permalink
packet counter: modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinPulec committed Aug 1, 2023
1 parent f106180 commit 371e99d
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions src/utils/packet_counter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,27 @@
#endif // defined HAVE_CONFIG_H

#include "utils/packet_counter.h"
#include <cassert>
#include <map>
#include <vector>

using std::map;
using std::vector;

struct packet_counter {
explicit packet_counter(int ns) : num_substreams(ns), packets(ns) {}
explicit packet_counter(int ns) : packets(ns) {}

void register_packet(int substream_id, int bufnum, int offset, int len) {
assert(substream_id < num_substreams);

packets[substream_id][bufnum][offset] = len;
packets.at(substream_id)[bufnum][offset] = len;
current_bufnum = bufnum;
}

int get_total_bytes() {
int ret = 0;

for(int i = 0; i < num_substreams; ++i) {
for(map<int, map<int, int> >::const_iterator it = packets[i].begin();
it != packets[i].end();
++it) {
for(map<int, int>::const_iterator it2 = it->second.begin();
it2 != it->second.end();
++it2) {
ret += it2->second;
for (auto &&chan : packets) {
for (auto &&buffer : chan) {
for (auto &&packet : buffer.second) {
ret += packet.second;
}
}
}
Expand All @@ -80,12 +73,11 @@ struct packet_counter {
int get_all_bytes() {
int ret = 0;

for(int i = 0; i < num_substreams; ++i) {
for(map<int, map<int, int> >::const_iterator it = packets[i].begin();
it != packets[i].end();
++it) {
if(!it->second.empty()) {
ret += (--it->second.end())->first + (--it->second.end())->second;
for (auto &&chan : packets) {
for (auto &buf : chan) {
if (!buf.second.empty()) {
ret += (--buf.second.end())->first +
(--buf.second.end())->second;
}
}
}
Expand All @@ -94,8 +86,8 @@ struct packet_counter {
}

void clear() {
for(int i = 0; i < num_substreams; ++i) {
packets[i].clear();
for (auto &&chan : packets) {
chan.clear();
}
}

Expand All @@ -119,25 +111,18 @@ struct packet_counter {
}

private:
int num_substreams;
vector<map<int, map<int, int>>> packets; ///< channel, bufnum, off, len
int current_bufnum = 0;

friend int packet_counter_get_channels(struct packet_counter *state);
};

struct packet_counter *packet_counter_init(int num_substreams) {
struct packet_counter *state;

state = new packet_counter(num_substreams);

return state;
return new packet_counter(num_substreams);
}

void packet_counter_destroy(struct packet_counter *state) {
if(state) {
delete state;
}
delete state;
}

void packet_counter_register_packet(struct packet_counter *state, unsigned int substream_id, unsigned int bufnum,
Expand All @@ -158,7 +143,7 @@ int packet_counter_get_all_bytes(struct packet_counter *state)

int packet_counter_get_channels(struct packet_counter *state)
{
return state->num_substreams;
return (int) state->packets.size();
}

void packet_counter_clear(struct packet_counter *state)
Expand Down

0 comments on commit 371e99d

Please sign in to comment.