GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/serverinfo.h Lines: 29 56 51.8 %
Date: 2017-11-29 Branches: 30 72 41.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 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
#ifndef NET_SERVERINFO_H
24
#define NET_SERVERINFO_H
25
26
#include "enums/net/serverfreetype.h"
27
#include "enums/net/servertype.h"
28
29
#include "net/hostsgroup.h"
30
#include "net/serverurlinfo.h"
31
32
#include "utils/stringutils.h"
33
34
#include "localconsts.h"
35
36
6028
class ServerInfo final
37
{
38
    public:
39
        typedef std::pair<int, std::string> VersionString;
40
41
        ServerTypeT type;
42
        ServerFreeTypeT freeType;
43
        std::string name;
44
        std::string hostname;
45
        std::string althostname;
46
        std::string description;
47
        std::string registerUrl;
48
        std::string onlineListUrl;
49
        std::string supportUrl;
50
        std::string serverUrl;
51
        std::string defaultHostName;
52
        STD_VECTOR<std::string> updateMirrors;
53
        STD_VECTOR<HostsGroup> updateHosts;
54
        STD_VECTOR<ServerUrlInfo> freeSources;
55
        STD_VECTOR<ServerUrlInfo> nonFreeSources;
56
        STD_VECTOR<ServerUrlInfo> docs;
57
        uint16_t port;
58
        VersionString version;
59
        int packetVersion;
60
        bool save;
61
        bool persistentIp;
62
63
488
        ServerInfo() :
64
            type(ServerType::TMWATHENA),
65
            freeType(ServerFreeType::NotSet),
66
            name(),
67
            hostname(),
68
            althostname(),
69
            description(),
70
            registerUrl(),
71
            onlineListUrl(),
72
            supportUrl(),
73
            serverUrl(),
74
            defaultHostName(),
75
            updateMirrors(),
76
            updateHosts(),
77
            freeSources(),
78
            nonFreeSources(),
79
            docs(),
80
#ifdef TMWA_SUPPORT
81
            port(6901),
82
#else  // TMWA_SUPPORT
83
            port(6900),
84
#endif  // TMWA_SUPPORT
85
            version(),
86
            packetVersion(),
87
            save(false),
88
7808
            persistentIp(true)
89
        {
90
            version.first = 0;
91
488
        }
92
93
60
        ServerInfo(const ServerInfo &info) :
94
60
            type(info.type),
95
60
            freeType(info.freeType),
96
            name(info.name),
97
            hostname(info.hostname),
98
            althostname(info.althostname),
99
            description(info.description),
100
            registerUrl(info.registerUrl),
101
            onlineListUrl(info.onlineListUrl),
102
            supportUrl(info.supportUrl),
103
            serverUrl(info.serverUrl),
104
            defaultHostName(info.defaultHostName),
105
            updateMirrors(info.updateMirrors),
106
            updateHosts(info.updateHosts),
107
            freeSources(info.freeSources),
108
            nonFreeSources(info.nonFreeSources),
109
            docs(info.docs),
110
60
            port(info.port),
111
            version(),
112
60
            packetVersion(info.packetVersion),
113
60
            save(info.save),
114


900
            persistentIp(info.persistentIp)
115
        {
116
60
            version.first = info.version.first;
117
120
            version.second = info.version.second;
118
60
        }
119
120
        A_DEFAULT_COPY(ServerInfo)
121
122
        ServerInfo &operator=(const ServerInfo &info)
123
        {
124
            type = info.type;
125
            freeType = info.freeType;
126
            name = info.name;
127
            hostname = info.hostname;
128
            althostname = info.althostname;
129
            description = info.description;
130
            registerUrl = info.registerUrl;
131
            onlineListUrl = info.onlineListUrl;
132
            supportUrl = info.supportUrl;
133
            serverUrl = info.serverUrl;
134
            defaultHostName = info.defaultHostName;
135
            updateMirrors = info.updateMirrors;
136
            updateHosts = info.updateHosts;
137
            freeSources = info.freeSources;
138
            nonFreeSources = info.nonFreeSources;
139
            docs = info.docs;
140
            port = info.port;
141
            save = info.save;
142
            persistentIp = info.persistentIp;
143
            version.first = info.version.first;
144
            version.second = info.version.second;
145
            packetVersion = info.packetVersion;
146
            return *this;
147
        }
148
149
        bool isValid() const noexcept2 A_WARN_UNUSED
150
        {
151



120
            return !(hostname.empty()
152
                || port == 0
153
                || type == ServerType::UNKNOWN);
154
        }
155
156
        void clear()
157
        {
158
            type = ServerType::UNKNOWN;
159
            name.clear();
160
            hostname.clear();
161
            althostname.clear();
162
            port = 0;
163
            description.clear();
164
            registerUrl.clear();
165
            onlineListUrl.clear();
166
            supportUrl.clear();
167
            serverUrl.clear();
168
            defaultHostName.clear();
169
            updateMirrors.clear();
170
            updateHosts.clear();
171
            freeSources.clear();
172
            nonFreeSources.clear();
173
            docs.clear();
174
            version.first = 0;
175
            version.second.clear();
176
            packetVersion = 0;
177
            save = false;
178
            persistentIp = true;
179
            freeType = ServerFreeType::Unknown;
180
        }
181
182
84
        bool operator==(const ServerInfo &other) const
183
        {
184
84
            return name == other.name &&
185
                hostname == other.hostname &&
186
84
                port == other.port;
187
        }
188
189
        bool operator!=(const ServerInfo &other) const
190
        {
191
            return name != other.name ||
192
                hostname != other.hostname ||
193
                port != other.port;
194
        }
195
196
88
        static ServerTypeT parseType(const std::string &serverType)
197
                                     A_WARN_UNUSED
198
        {
199
#ifdef TMWA_SUPPORT
200

352
            if (compareStrI(serverType, "tmwathena") == 0)
201
                return ServerType::TMWATHENA;
202
            else
203
#endif  // TMWA_SUPPORT
204

272
            if (compareStrI(serverType, "eathena") == 0)
205
                return ServerType::EATHENA;
206

256
            else if (compareStrI(serverType, "hercules") == 0)
207
                return ServerType::EATHENA;
208

256
            else if (compareStrI(serverType, "evol2") == 0)
209
                return ServerType::EVOL2;
210
60
            return ServerType::UNKNOWN;
211
        }
212
213
28
        static ServerFreeTypeT parseFreeType(const std::string &serverFreeType)
214
                                             A_WARN_UNUSED
215
        {
216

112
            if (compareStrI(serverFreeType, "free") == 0)
217
                return ServerFreeType::Free;
218

112
            else if (compareStrI(serverFreeType, "nonfree") == 0)
219
                return ServerFreeType::NonFree;
220

112
            else if (compareStrI(serverFreeType, "unknown") == 0)
221
                return ServerFreeType::Unknown;
222
28
            return ServerFreeType::NotSet;
223
        }
224
};
225
226
typedef STD_VECTOR<ServerInfo> ServerInfos;
227
228
#endif  // NET_SERVERINFO_H