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