YAPOG
0.0.1
Yet Another Pokemon Online Game
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
Session.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/Packet.hpp
"
4
5
#include "
Client/Session.hpp
"
6
#include "
Configuration/GameData.hpp
"
7
8
#include "
YAPOG/System/IO/Log/DebugLogger.hpp
"
9
namespace
ycl
10
{
11
const
yap::Int16
Session::DEFAULT_REMOTE_PORT
= 8008;
12
13
const
yap::Time
Session::DEFAULT_RECEPTION_SLEEP_DELAY
=
yap::Time
(0.005f);
14
15
Session::Session
()
16
: packetHandler_ ()
17
, receptionThread_ ([this] () {
HandleReception
(); })
18
,
receptionIsActive_
(
false
)
19
,
socket_
()
20
,
networkHandler_
(
socket_
)
21
,
networkHandlerMutex_
()
22
,
user_
()
23
,
isConnected_
(
false
)
24
{
25
ADD_HANDLER
(
26
ServerInfoLoginValidation
,
27
Session::HandleServerInfoLoginValidation
);
28
29
ADD_HANDLER
(
30
ServerInfoRegistrationValidation
,
31
Session::HandleServerInfoRegistrationValidation
);
32
33
ADD_HANDLER
(
ServerInfoLoginError
,
Session::HandleServerInfoLoginError
);
34
35
ADD_HANDLER
(
36
ServerInfoRegistrationError
,
37
Session::HandleServerInfoRegistrationError
);
38
39
ADD_HANDLER
(
ServerInfoPrimaryData
,
Session::HandleServerInfoPrimaryData
);
40
}
41
42
Session::~Session
()
43
{
44
}
45
46
Session
&
Session::Instance
()
47
{
48
static
Session
instance;
49
50
return
instance;
51
}
52
53
void
Session::Refresh
()
54
{
55
{
56
yap::Lock
lock (
networkHandlerMutex_
);
57
58
while
(!
networkHandler_
.
IsEmpty
())
59
{
60
yap::PacketPtrType
packet (
networkHandler_
.
GetPacket
());
61
yap::DebugLogger::Instance
().LogLine (
62
"Packet: "
+
63
yap::StringHelper::ToString
(static_cast<int> (packet->GetType ())));
64
if
(!
HandlePacket
(*packet))
65
{
66
Disconnect
();
67
YAPOG_THROW
(
"Wrong packet received."
);
68
}
69
}
70
}
71
}
72
73
void
Session::Login
(
const
yap::String
& login,
const
yap::String
& password)
74
{
75
if
(!
Connect
())
76
return
;
77
// YAPOG_THROW("Failed to connect to the server `"
78
// + DEFAULT_REMOTE_IP
79
// + "'.");
80
81
user_
.
SetLogin
(login);
82
84
yap::Packet
packet;
85
packet.
CreateFromType
(
yap::PacketType::ClientRequestLogin
);
86
packet.
Write
(login);
87
packet.
Write
(password);
88
SendPacket
(packet);
89
}
90
91
void
Session::Register
(
92
const
yap::String
& login,
93
const
yap::String
& password,
94
const
yap::String
& email)
95
{
96
if
(!
Connect
())
97
return
;
98
100
yap::Packet
packet;
101
packet.
CreateFromType
(
yap::PacketType::ClientRequestRegistration
);
102
packet.
Write
(login);
103
packet.
Write
(password);
104
packet.
Write
(email);
105
SendPacket
(packet);
106
}
107
108
User
&
Session::GetUser
()
109
{
110
return
user_
;
111
}
112
113
bool
Session::HandlePacket
(
yap::IPacket
& packet)
114
{
115
return
packetHandler_
.
HandlePacket
(packet);
116
}
117
118
bool
Session::SendPacket
(
yap::IPacket
& packet)
119
{
120
return
socket_
.
Send
(packet);
121
}
122
123
void
Session::AddRelay
(
yap::IPacketHandler
* relay)
124
{
125
packetHandler_
.
AddRelay
(relay);
126
}
127
128
void
Session::RemoveRelay
(
yap::IPacketHandler
* relay)
129
{
130
relay->
SetParent
(
nullptr
);
131
132
packetHandler_
.
RemoveRelay
(relay);
133
}
134
135
void
Session::SetParent
(
yap::IPacketHandler
* parent)
136
{
137
YAPOG_THROW
(
"Unallowed to set parent for Session."
);
138
}
139
140
bool
Session::Connect
()
141
{
142
if
(
isConnected_
)
143
return
true
;
144
145
if
(!
socket_
.
Connect
(
GameData::RemoteIPAddress
(),
DEFAULT_REMOTE_PORT
))
146
return
false
;
147
148
AddRelay
(&
user_
);
149
user_
.
SetParent
(
this
);
150
151
receptionIsActive_
=
true
;
152
receptionThread_
.
Launch
();
153
154
isConnected_
=
true
;
155
156
return
true
;
157
}
158
159
void
Session::Disconnect
()
160
{
161
yap::Packet
packet;
162
packet.
CreateFromType
(
yap::PacketType::ClientInfoDeconnection
);
163
SendPacket
(packet);
164
165
socket_
.
Disconnect
();
166
167
receptionIsActive_
=
false
;
168
169
isConnected_
=
false
;
170
}
171
172
void
Session::HandleReception
()
173
{
174
if
(!
isConnected_
)
175
return
;
176
177
while
(
receptionIsActive_
)
178
{
179
yap::Thread::Sleep
(
DEFAULT_RECEPTION_SLEEP_DELAY
);
180
181
{
182
yap::Lock
lock (
networkHandlerMutex_
);
183
184
networkHandler_
.
Refresh
();
185
}
186
}
187
}
188
189
void
Session::HandleServerInfoLoginValidation
(
yap::IPacket
& packet)
190
{
191
OnLoginValidation
(*
this
,
yap::EmptyEventArgs
());
192
193
yap::DebugLogger::Instance
().LogLine (
"Login successful !"
);
194
195
yap::Packet
response;
196
response.
CreateFromType
(
yap::PacketType::ClientRequestStartInfo
);
197
SendPacket
(response);
198
}
199
200
void
Session::HandleServerInfoRegistrationValidation
(
yap::IPacket
& packet)
201
{
202
OnRegistrationValidation
(*
this
,
yap::EmptyEventArgs
());
203
204
yap::DebugLogger::Instance
().LogLine (
"Registration successful !"
);
205
}
206
207
void
Session::HandleServerInfoLoginError
(
yap::IPacket
& packet)
208
{
209
OnLoginError
(*
this
,
yap::EmptyEventArgs
());
210
211
yap::DebugLogger::Instance
().LogLine (
"Wrong login."
);
212
}
213
214
void
Session::HandleServerInfoRegistrationError
(
yap::IPacket
& packet)
215
{
216
OnRegistrationError
(*
this
,
yap::EmptyEventArgs
());
217
218
yap::DebugLogger::Instance
().LogLine (
"Registration error."
);
219
}
220
221
void
Session::HandleServerInfoPrimaryData
(
yap::IPacket
& packet)
222
{
223
UpdateObjectFactory
(packet,
yap::ObjectFactory::Instance
());
224
}
225
226
void
Session::UpdateObjectFactory
(
227
yap::IPacket
& packet,
228
yap::ObjectFactory
& objectFactory)
229
{
230
yap::UInt64
typeCount = packet.
ReadUInt64
();
231
232
for
(
yap::UInt64
count = 0; count < typeCount; ++count)
233
{
234
yap::ID
id
= packet.
ReadID
();
235
yap::String
type = packet.
ReadString
();
236
237
objectFactory.
AddType
(
id
, type);
238
}
239
}
240
}
// namespace ycl
YAPOG.Client
src
Client
Session.cpp
Generated on Mon Sep 17 2012 22:24:21 for YAPOG by
1.8.1.1