-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
150 lines (127 loc) · 3.53 KB
/
Server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include <algorithm>
#include "Server.h"
#include "config.h"
#include "constants.h"
#include "ClientsManager.h"
#include "client.h"
#include "db.h"
using namespace FTS;
FTSSrv2::Server::Server(std::string in_logDir, bool in_bVerbose, int in_dbgLevel, DataBase* pDataBase)
: m_bVerbose(in_bVerbose)
, m_dbgLvl(in_dbgLevel)
{
m_sLogFile = tryFile(DSRV_LOGFILE_LOG ,in_logDir);
m_sErrFile = tryFile(DSRV_LOGFILE_ERR ,in_logDir);
m_sNetLogFile = tryFile(DSRV_LOGFILE_NETLOG ,in_logDir);
m_sGamesFile = tryFile(DSRV_FILE_NGAMES ,in_logDir);
m_sPlayersFile = tryFile(DSRV_FILE_NPLAYERS ,in_logDir);
m_pClientsManager = new ClientsManager();
m_pDataBase = pDataBase;
}
FTSSrv2::Server::~Server()
{
delete m_pClientsManager;
delete m_pDataBase;
}
// Finds a writable path for a file.
std::string FTSSrv2::Server::tryFile(const std::string &in_sFilename, const std::string &in_sDir) const
{
std::string sDir = in_sDir;
std::replace( std::begin( sDir ), std::end( sDir ), '\\', '/' );
// Add a trailing slash if needed.
if( !sDir.empty() && sDir.back() != '/' ) {
sDir += "/";
}
std::string sHome = getenv("HOME");
std::string sFile = sDir + in_sFilename;
// First try.
std::cout << "Trying to access to the file at '" << sFile << "' ... ";
std::ofstream f;
f.open(sFile);
if( !f.is_open() ) {
std::cout << "Failed !\n";
} else {
std::cout << "Success !\n";
f.close();
return sFile;
}
// Second try.
sFile = sHome + "/" + in_sFilename;
std::cout << "Trying to access to the file at '" << sFile << "' ... ";
f.open(sFile);
if(!f.is_open()) {
std::cout << "Failed !\n";
std::cout << "Logging will be disabled.\n";
return "";
} else {
std::cout << "Success !\n";
f.close();
return sFile;
}
}
size_t FTSSrv2::Server::addPlayer()
{
// Modify the statistics.
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_nPlayers++;
std::ofstream of(m_sPlayersFile.c_str(), std::ios_base::trunc);
if(of) {
of << m_nPlayers;
}
return m_nPlayers;
}
size_t FTSSrv2::Server::remPlayer()
{
// Modify the statistics.
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_nPlayers--;
std::ofstream of(m_sPlayersFile.c_str(), std::ios_base::trunc);
if(of) {
of << m_nPlayers;
}
return m_nPlayers;
}
size_t FTSSrv2::Server::addGame()
{
// Modify the statistics.
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_nGames++;
std::ofstream of(m_sGamesFile.c_str(), std::ios_base::trunc);
if(of) {
of << m_nGames;
}
return m_nGames;
}
size_t FTSSrv2::Server::remGame()
{
// Modify the statistics.
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_nGames--;
std::ofstream of(m_sGamesFile.c_str(), std::ios_base::trunc);
if(of) {
of << m_nGames;
}
return m_nGames;
}
PacketStats FTSSrv2::Server::getStatTotalPackets()
{
return m_totalPackets;
}
FTSSrv2::IClientsManager* FTSSrv2::Server::getClientsManager()
{
return dynamic_cast<IClientsManager*>(m_pClientsManager);
}
void FTSSrv2::Server::clearStats()
{
std::lock_guard<std::recursive_mutex> l(m_mutex);
m_totalPackets.clear();
}
void FTSSrv2::Server::addStats( PacketStats stats)
{
for( auto it : stats ) {
m_totalPackets[it.first].first += it.second.first;
m_totalPackets[it.first].second += it.second.second;
}
}