GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/packetcounters.cpp Lines: 0 35 0.0 %
Date: 2021-03-17 Branches: 0 28 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "net/packetcounters.h"
25
26
#include "utils/cast.h"
27
28
#include "debug.h"
29
30
extern volatile time_t cur_time;
31
extern volatile bool runCounters;
32
33
int PacketCounters::mInCurrentSec = 0;
34
int PacketCounters::mInBytes = 0;
35
int PacketCounters::mInBytesCalc = 0;
36
int PacketCounters::mInPackets = 0;
37
int PacketCounters::mInPacketsCalc = 0;
38
int PacketCounters::mOutCurrentSec = 0;
39
int PacketCounters::mOutBytes = 0;
40
int PacketCounters::mOutBytesCalc = 0;
41
int PacketCounters::mOutPackets = 0;
42
int PacketCounters::mOutPacketsCalc = 0;
43
44
void PacketCounters::incInBytes(const int cnt)
45
{
46
    if (!runCounters)
47
        return;
48
49
    updateCounter(PacketCounters::mInCurrentSec, PacketCounters::mInBytesCalc,
50
                  PacketCounters::mInBytes);
51
52
    PacketCounters::mInBytes += cnt;
53
}
54
55
void PacketCounters::incInPackets()
56
{
57
    if (!runCounters)
58
        return;
59
60
    updateCounter(PacketCounters::mInCurrentSec,
61
                  PacketCounters::mInPacketsCalc, PacketCounters::mInPackets);
62
63
    PacketCounters::mInPackets ++;
64
}
65
66
int PacketCounters::getInBytes()
67
{
68
    return PacketCounters::mInBytesCalc;
69
}
70
71
int PacketCounters::getInPackets()
72
{
73
    return PacketCounters::mInPacketsCalc;
74
}
75
76
void PacketCounters::incOutBytes(const int cnt)
77
{
78
    if (!runCounters)
79
        return;
80
81
    updateCounter(PacketCounters::mOutCurrentSec,
82
                  PacketCounters::mOutBytesCalc, PacketCounters::mOutBytes);
83
84
    PacketCounters::mOutBytes += cnt;
85
}
86
87
void PacketCounters::incOutPackets()
88
{
89
    if (!runCounters)
90
        return;
91
92
    updateCounter(PacketCounters::mOutCurrentSec,
93
                  PacketCounters::mOutPacketsCalc,
94
                  PacketCounters::mOutPackets);
95
96
    PacketCounters::mOutPackets ++;
97
}
98
99
int PacketCounters::getOutBytes()
100
{
101
    return PacketCounters::mOutBytesCalc;
102
}
103
104
int PacketCounters::getOutPackets()
105
{
106
    return PacketCounters::mOutPacketsCalc;
107
}
108
109
110
void PacketCounters::updateCounter(int &restrict currentSec,
111
                                   int &restrict calc,
112
                                   int &restrict counter)
113
{
114
    const int idx = CAST_S32(cur_time % 60);
115
    if (currentSec != idx)
116
    {
117
        currentSec = idx;
118
        calc = counter;
119
        counter = 0;
120
    }
121
}
122
123
void PacketCounters::update()
124
{
125
    if (!runCounters)
126
        return;
127
128
    BLOCK_START("PacketCounters::update")
129
    updateCounter(PacketCounters::mInCurrentSec, PacketCounters::mInBytesCalc,
130
        PacketCounters::mInBytes);
131
    updateCounter(PacketCounters::mInCurrentSec,
132
        PacketCounters::mInPacketsCalc, PacketCounters::mInPackets);
133
    updateCounter(PacketCounters::mOutCurrentSec,
134
        PacketCounters::mOutBytesCalc, PacketCounters::mOutBytes);
135
    updateCounter(PacketCounters::mOutCurrentSec,
136
        PacketCounters::mOutPacketsCalc, PacketCounters::mOutPackets);
137
    BLOCK_END("PacketCounters::update")
138
}