40 m_state(RCON_DISCONNECTED), |
40 m_state(RCON_DISCONNECTED), |
41 m_lastPing(0), |
41 m_lastPing(0), |
42 m_adminCount(0), |
42 m_adminCount(0), |
43 m_interface(nullptr) |
43 m_interface(nullptr) |
44 { |
44 { |
45 if (not m_socket.set_blocking(false)) |
45 std::stringstream errors; |
46 { |
46 if (not m_socket.set_blocking(false, errors)) |
47 fprintf(stderr, "unable to set socket as non-blocking: %s\n", |
47 { |
48 m_socket.error_string().data()); |
48 fprintf(stderr, "unable to set socket as non-blocking: %s\n", errors.str().data()); |
49 exit(EXIT_FAILURE); |
49 exit(EXIT_FAILURE); |
50 } |
50 } |
51 } |
51 } |
52 |
52 |
53 // ------------------------------------------------------------------------------------------------- |
53 // ------------------------------------------------------------------------------------------------- |
78 m_state = RCON_DISCONNECTED; |
78 m_state = RCON_DISCONNECTED; |
79 } |
79 } |
80 |
80 |
81 // ------------------------------------------------------------------------------------------------- |
81 // ------------------------------------------------------------------------------------------------- |
82 // |
82 // |
83 void RCONSession::send(const ByteArray& packet) |
83 bool RCONSession::send(const ByteArray& packet) |
84 { |
84 { |
85 m_socket.send(m_address, packet); |
85 std::stringstream errors; |
|
86 const bool result = m_socket.send(m_address, packet, errors); |
|
87 if (not result) |
|
88 { |
|
89 this->m_interface->printError("Network error: %s\n", errors.str().data()); |
|
90 } |
|
91 return result; |
86 } |
92 } |
87 |
93 |
88 // ------------------------------------------------------------------------------------------------- |
94 // ------------------------------------------------------------------------------------------------- |
89 // |
95 // |
90 void RCONSession::tick() |
96 void RCONSession::tick() |
110 send({CLRC_PONG}); |
116 send({CLRC_PONG}); |
111 bumpLastPing(); |
117 bumpLastPing(); |
112 } |
118 } |
113 } |
119 } |
114 |
120 |
115 for (Datagram datagram; m_socket.read(datagram);) |
121 std::stringstream errors; |
116 { |
122 for (net::Datagram datagram; m_socket.read(datagram, errors);) |
|
123 { |
|
124 if (errors.tellp() > 0) |
|
125 { |
|
126 m_interface->printError("Network error: %s\n", errors.str().data()); |
|
127 errors = {}; |
|
128 } |
117 // Only process packets that originate from the game server. |
129 // Only process packets that originate from the game server. |
118 if (datagram.address == m_address) |
130 if (datagram.address == m_address) |
119 handlePacket(datagram.message); |
131 handlePacket(datagram.message); |
120 } |
132 } |
121 } |
133 } |