sources/network/rconsession.cpp

changeset 58
d175243ad169
parent 57
a2f41245e387
child 70
0e947b487b18
equal deleted inserted replaced
57:a2f41245e387 58:d175243ad169
1 #include "rconsession.h" 1 #include "rconsession.h"
2 #include "../interface.h" 2 #include "../interface.h"
3
4 RCONSession* g_rconSession = nullptr;
5 3
6 // ------------------------------------------------------------------------------------------------- 4 // -------------------------------------------------------------------------------------------------
7 // 5 //
8 RCONSession::RCONSession() : 6 RCONSession::RCONSession() :
9 m_state (RCON_DISCONNECTED), 7 m_state (RCON_DISCONNECTED),
10 m_lastPing (0), 8 m_lastPing (0),
11 m_numAdmins (0) {} 9 m_numAdmins (0)
12 10 {
13 // ------------------------------------------------------------------------------------------------- 11 if (not m_socket.set_blocking (false))
14 // 12 {
15 STATIC METHOD 13 print_to (stderr, "unable to set socket as non-blocking: %s\n",
16 RCONSession::new_session() -> RCONSession* 14 m_socket.error_string().chars());
17 { 15 exit (EXIT_FAILURE);
18 if (g_rconSession != NULL) 16 }
19 { 17 }
20 g_rconSession->disconnect(); 18
21 delete g_rconSession; 19 // -------------------------------------------------------------------------------------------------
22 } 20 //
23 21 RCONSession::~RCONSession() {}
24 g_rconSession = new RCONSession;
25
26 if (not g_rconSession->socket()->set_blocking (false))
27 {
28 print ("unable to set socket as non-blocking: %s\n",
29 g_rconSession->socket()->error_string().chars());
30 delete g_rconSession;
31 return nullptr;
32 }
33
34 return g_rconSession;
35 }
36
37 // -------------------------------------------------------------------------------------------------
38 //
39 RCONSession::~RCONSession()
40 {
41 disconnect();
42
43 if (g_rconSession == this)
44 g_rconSession = nullptr;
45 }
46 22
47 // ------------------------------------------------------------------------------------------------- 23 // -------------------------------------------------------------------------------------------------
48 // 24 //
49 METHOD 25 METHOD
50 RCONSession::connect (IPAddress address) -> void 26 RCONSession::connect (IPAddress address) -> void
277 // ------------------------------------------------------------------------------------------------- 253 // -------------------------------------------------------------------------------------------------
278 // 254 //
279 STATIC METHOD 255 STATIC METHOD
280 RCONSession::get_session() -> RCONSession* 256 RCONSession::get_session() -> RCONSession*
281 { 257 {
282 if (g_rconSession == nullptr) 258 static RCONSession session;
283 new_session(); 259 return &session;
284
285 return g_rconSession;
286 } 260 }
287 261
288 // ------------------------------------------------------------------------------------------------- 262 // -------------------------------------------------------------------------------------------------
289 // 263 //
290 METHOD 264 METHOD

mercurial