YAPOG  0.0.1
Yet Another Pokemon Online Game
Session.cpp
Go to the documentation of this file.
4 
5 #include "Client/Session.hpp"
7 
9 namespace ycl
10 {
12 
14 
16  : packetHandler_ ()
17  , receptionThread_ ([this] () { HandleReception (); })
18  , receptionIsActive_ (false)
19  , socket_ ()
22  , user_ ()
23  , isConnected_ (false)
24  {
28 
32 
34 
38 
40  }
41 
43  {
44  }
45 
47  {
48  static Session instance;
49 
50  return instance;
51  }
52 
54  {
55  {
57 
58  while (!networkHandler_.IsEmpty ())
59  {
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;
86  packet.Write (login);
87  packet.Write (password);
88  SendPacket (packet);
89  }
90 
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;
102  packet.Write (login);
103  packet.Write (password);
104  packet.Write (email);
105  SendPacket (packet);
106  }
107 
109  {
110  return user_;
111  }
112 
114  {
115  return packetHandler_.HandlePacket (packet);
116  }
117 
119  {
120  return socket_.Send (packet);
121  }
122 
124  {
125  packetHandler_.AddRelay (relay);
126  }
127 
129  {
130  relay->SetParent (nullptr);
131 
132  packetHandler_.RemoveRelay (relay);
133  }
134 
136  {
137  YAPOG_THROW("Unallowed to set parent for Session.");
138  }
139 
141  {
142  if (isConnected_)
143  return true;
144 
146  return false;
147 
148  AddRelay (&user_);
149  user_.SetParent (this);
150 
151  receptionIsActive_ = true;
153 
154  isConnected_ = true;
155 
156  return true;
157  }
158 
160  {
161  yap::Packet packet;
163  SendPacket (packet);
164 
165  socket_.Disconnect ();
166 
167  receptionIsActive_ = false;
168 
169  isConnected_ = false;
170  }
171 
173  {
174  if (!isConnected_)
175  return;
176 
177  while (receptionIsActive_)
178  {
180 
181  {
183 
185  }
186  }
187  }
188 
190  {
192 
193  yap::DebugLogger::Instance ().LogLine ("Login successful !");
194 
195  yap::Packet response;
197  SendPacket (response);
198  }
199 
201  {
203 
204  yap::DebugLogger::Instance ().LogLine ("Registration successful !");
205  }
206 
208  {
209  OnLoginError (*this, yap::EmptyEventArgs ());
210 
211  yap::DebugLogger::Instance ().LogLine ("Wrong login.");
212  }
213 
215  {
217 
218  yap::DebugLogger::Instance ().LogLine ("Registration error.");
219  }
220 
222  {
224  }
225 
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