YAPOG
0.0.1
Yet Another Pokemon Online Game
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
Server.cpp
Go to the documentation of this file.
1
#include "
YAPOG/System/Error/Exception.hpp
"
2
#include "
YAPOG/System/StringHelper.hpp
"
3
#include "
YAPOG/System/Network/ClientSocket.hpp
"
4
#include "
YAPOG/System/Network/IPacket.hpp
"
5
#include "
YAPOG/System/Network/Packet.hpp
"
6
#include "
YAPOG/Content/ContentManager.hpp
"
7
#include "
YAPOG/Game/Factory/ObjectFactory.hpp
"
8
#include "
YAPOG/Game/Factory/XmlObjectIDLoader.hpp
"
9
#include "
YAPOG/Game/Factory/XmlObjectLoader.hpp
"
10
#include "
YAPOG/Game/World/Map/WorldObjectStateFactory.hpp
"
11
#include "
YAPOG/System/IO/Log/DebugLogger.hpp
"
12
#include "
YAPOG/System/IO/Log/CountLoggerMode.hpp
"
13
#include "
YAPOG/System/IO/Log/TimeLoggerMode.hpp
"
14
#include "
YAPOG/System/RandomHelper.hpp
"
15
#include "
YAPOG/Game/World/Map/MapElement.hpp
"
16
#include "
YAPOG/Game/World/Map/MapElementReader.hpp
"
17
#include "
YAPOG/Game/World/Map/TeleporterReader.hpp
"
18
#include "
YAPOG/Game/World/Map/DestructibleObject.hpp
"
19
#include "
YAPOG/Game/World/Map/DestructibleObjectReader.hpp
"
20
#include "
YAPOG/Game/World/Map/OpenBattleSpawnerArea.hpp
"
21
#include "
YAPOG/Game/World/Map/OpenBattleSpawnerAreaReader.hpp
"
22
#include "
YAPOG/Game/Pokemon/PokemonInfoReader.hpp
"
23
#include "
YAPOG/Game/Pokemon/PokemonInfo.hpp
"
24
#include "
YAPOG/Game/Pokemon/NatureInfoReader.hpp
"
25
#include "
YAPOG/Game/Pokemon/NatureInfo.hpp
"
26
#include "
YAPOG/Game/Pokemon/TypeInfoReader.hpp
"
27
#include "
YAPOG/Game/Pokemon/TypeInfo.hpp
"
28
#include "
YAPOG/Game/Pokemon/SkillInfoReader.hpp
"
29
#include "
YAPOG/Game/Pokemon/SkillInfo.hpp
"
30
31
#include "
Server/Server.hpp
"
32
#include "
Server/ClientSession.hpp
"
33
#include "World/Map/MapReader.hpp"
34
#include "World/Map/Map.hpp"
35
#include "World/Map/Player.hpp"
36
#include "World/Map/PlayerReader.hpp"
37
#include "World/Map/NPC.hpp"
38
#include "World/Map/NPCReader.hpp"
39
#include "World/Map/Teleporter.hpp"
40
41
namespace
yse
42
{
43
const
bool
Server::DEFAULT_RUNNING_STATE
=
false
;
44
const
yap::Int16
Server::DEFAULT_PORT
= 8008;
45
46
const
float
Server::DEFAULT_WORLD_UPDATE_RATE
= 100.0f;
47
48
Server::Server
()
49
: isRunning_ (DEFAULT_RUNNING_STATE)
50
,
socket_
()
51
, socketMutex_ ()
52
, listeningThread_ ([&] () {
HandleListening
(); })
53
,
port_
(DEFAULT_PORT)
54
,
clients_
()
55
,
clientsMutex_
()
56
,
world_
()
57
,
worldMutex_
()
58
,
worldUpdateTimer_
()
59
,
contentManager_
(
yap::ContentManager::Instance
())
60
,
objectFactory_
(
yap::ObjectFactory::Instance
())
61
,
worldObjectStateFactory_
(
yap::WorldObjectStateFactory::Instance
())
62
,
logger_
(
yap::DebugLogger::Instance
())
63
,
databaseManager_
()
64
,
databaseManagerMutex_
()
65
{
66
}
67
68
void
Server::Init
()
69
{
70
InitRandom
();
71
72
#ifndef YAPOG_WIN
73
InitContentManager
(
yap::Path
(
"../Content/"
));
74
#else
75
InitContentManager
(
yap::Path
(
"../../Content/"
));
76
#endif // YAPOG_WIN
77
78
InitObjectFactory
();
79
InitWorldObjectStateFactory
();
80
81
InitLoggerManager
();
82
83
databaseManager_
.
Connect
();
84
85
LoadMaps
();
86
87
if
(!
socket_
.
Listen
(
port_
))
88
YAPOG_THROW
(
89
"Failed to listen port `"
+
90
yap::StringHelper::ToString
(
port_
) +
91
"'."
);
92
}
93
94
void
Server::Launch
()
95
{
96
isRunning_
=
true
;
97
98
listeningThread_
.
Launch
();
99
100
clients_
.
LaunchReception
();
101
102
while
(
isRunning_
)
103
{
104
yap::Time
dt =
worldUpdateTimer_
.
GetCurrentTime
();
105
worldUpdateTimer_
.
Reset
();
106
107
{
108
yap::Lock
lock (
clientsMutex_
);
109
110
clients_
.
Refresh
();
111
clients_
.
ServerTick
(dt);
112
}
113
114
{
115
yap::Lock
lock (
worldMutex_
);
116
117
world_
.
Update
(dt);
118
}
119
120
yap::Time
sleepTime =
yap::Time
(1.0f /
DEFAULT_WORLD_UPDATE_RATE
) - dt;
121
if
(sleepTime.
GetValue
() > 0.0f)
122
yap::Thread::Sleep
(sleepTime);
123
}
124
125
Dispose
();
126
}
127
128
void
Server::Stop
()
129
{
130
isRunning_
=
false
;
131
}
132
133
void
Server::Dispose
()
134
{
135
clients_
.
Dispose
();
136
137
socket_
.
Close
();
138
}
139
140
void
Server::AddClient
(
ClientSession
* client)
141
{
142
{
143
yap::Lock
lock (
worldMutex_
);
144
client->
GetUser
().
SetWorld
(&
world_
);
145
}
146
147
{
148
yap::Lock
lock (
databaseManagerMutex_
);
149
client->
GetUser
().
SetDatabaseManager
(&
databaseManager_
);
150
}
151
152
client->
Init
();
153
154
{
155
yap::Lock
lock (
clientsMutex_
);
156
clients_
.
AddClient
(client);
157
}
158
}
159
160
void
Server::HandleListening
()
161
{
162
while
(
isRunning_
)
163
{
164
ClientSession
* client =
new
ClientSession
();
165
166
if
(!
socket_
.
Accept
(client->
GetSocket
()))
167
YAPOG_THROW
(
"Failed to accept client `"
+
168
client->
GetSocket
().
GetRemoteAddress
() +
169
"'."
);
170
171
yap::DebugLogger::Instance
().
LogLine
(
172
"Client connected: `"
+
173
client->
GetSocket
().
GetRemoteAddress
() +
174
"'."
);
175
176
AddClient
(client);
177
}
178
}
179
180
void
Server::InitRandom
()
181
{
182
yap::RandomHelper::Init
(time (
nullptr
));
183
}
184
185
void
Server::InitContentManager
(
const
yap::Path
& contentRootPath)
186
{
187
contentManager_
.
Init
(contentRootPath);
188
}
189
190
void
Server::InitObjectFactory
()
191
{
192
objectFactory_
.
RegisterLoader
(
193
"Map"
,
194
new
yap::XmlObjectIDLoader<Map, MapReader>
(
195
yap::Path
(
"Map"
),
"Map"
));
196
197
objectFactory_
.
RegisterLoader
(
198
"NPC"
,
199
new
yap::XmlObjectIDLoader<NPC, NPCReader>
(
200
yap::Path
(
"NPC"
),
201
"NPC"
));
202
203
objectFactory_
.
RegisterLoader
(
204
"DestructibleObject"
,
205
new
yap::XmlObjectIDLoader
<
206
yap::DestructibleObject
,
207
yap::DestructibleObjectReader
> (
208
yap::Path
(
"DestructibleObject"
),
209
"DestructibleObject"
));
210
211
objectFactory_
.
RegisterLoader
(
212
"Player"
,
213
new
yap::XmlObjectIDLoader<Player, PlayerReader>
(
214
yap::Path
(
"Player"
),
215
"Player"
));
216
217
objectFactory_
.
RegisterLoader
(
218
"MapElement"
,
219
new
yap::XmlObjectIDLoader<yap::MapElement, yap::MapElementReader>
(
220
yap::Path
(
"MapElement"
),
221
"MapElement"
));
222
223
objectFactory_
.
RegisterLoader
(
224
"OpenBattleSpawnerArea"
,
225
new
yap::XmlObjectIDLoader
<
226
yap::OpenBattleSpawnerArea
,
227
yap::OpenBattleSpawnerAreaReader
> (
228
yap::Path
(
"OpenBattleSpawnerArea"
),
229
"OpenBattleSpawnerArea"
));
230
231
objectFactory_
.
RegisterLoader
(
232
"Teleporter"
,
233
new
yap::XmlObjectIDLoader<Teleporter, yap::TeleporterReader>
(
234
yap::Path
(
"Teleporter"
),
235
"Teleporter"
));
236
237
objectFactory_
.
RegisterLoader
(
238
"PokemonInfo"
,
239
new
yap::XmlObjectIDLoader<yap::PokemonInfo, yap::PokemonInfoReader>
(
240
yap::Path
(
"Pokemon/Pokemon"
),
241
"PokemonInfo"
));
242
243
objectFactory_
.
RegisterLoader
(
244
"NatureInfo"
,
245
new
yap::XmlObjectIDLoader<yap::NatureInfo, yap::NatureInfoReader>
(
246
yap::Path
(
"Pokemon/Nature"
),
247
"Nature"
));
248
249
objectFactory_
.
RegisterLoader
(
250
"TypeInfo"
,
251
new
yap::XmlObjectIDLoader<yap::TypeInfo, yap::TypeInfoReader>
(
252
yap::Path
(
"Pokemon/Types"
),
253
"Type"
));
254
255
objectFactory_
.
RegisterLoader
(
256
"SkillInfo"
,
257
new
yap::XmlObjectIDLoader<yap::SkillInfo, yap::SkillInfoReader>
(
258
yap::Path
(
"Pokemon/Skills"
),
259
"Skill"
));
260
}
261
262
void
Server::InitWorldObjectStateFactory
()
263
{
264
worldObjectStateFactory_
.
AddState
(
"Inactive"
,
"Inactive"
);
265
worldObjectStateFactory_
.
AddState
(
"Moving"
,
"Moving"
);
266
}
267
268
void
Server::InitLoggerManager
()
269
{
270
logger_
.
AddMode
(
new
yap::CountLoggerMode
());
271
logger_
.
AddMode
(
new
yap::TimeLoggerMode
());
272
}
273
274
void
Server::LoadMaps
()
275
{
276
world_
.
LoadMaps
();
277
}
278
}
// namespace yse
YAPOG.Server
src
Server
Server.cpp
Generated on Mon Sep 17 2012 22:24:22 for YAPOG by
1.8.1.1