@@ -55,7 +55,7 @@ bool rconpp::rcon_server::startup_server() {
5555 if (sock == -1 ) {
5656#endif
5757 on_log (" Failed to open socket." );
58- report_error ();
58+ report_get_last_error ();
5959 return false ;
6060 }
6161
@@ -73,14 +73,14 @@ bool rconpp::rcon_server::startup_server() {
7373 int status = bind (sock, reinterpret_cast <const sockaddr*>(&server), sizeof (server));
7474
7575 if (status == -1 ) {
76- report_error ();
76+ report_get_last_error ();
7777 return false ;
7878 }
7979
8080 status = listen (sock, SOMAXCONN);
8181
8282 if (status == -1 ) {
83- report_error ();
83+ report_get_last_error ();
8484 return false ;
8585 }
8686
@@ -119,7 +119,7 @@ void rconpp::rcon_server::disconnect_client(const int client_socket, const bool
119119}
120120
121121void rconpp::rcon_server::read_packet (connected_client& client) {
122- const int packet_size = read_packet_size (static_cast < int >( client.socket ) );
122+ const int packet_size = read_packet_size (client.socket );
123123
124124 // Silently ignore packet size.
125125 if (packet_size < MIN_PACKET_SIZE) {
@@ -129,9 +129,9 @@ void rconpp::rcon_server::read_packet(connected_client& client) {
129129 std::vector<char > buffer{};
130130 buffer.resize (packet_size);
131131
132- if (recv (client.socket , buffer.data (), packet_size, 0 ) == -1 ) {
132+ if (recv (client.socket , buffer.data (), packet_size, MSG_NOSIGNAL ) == -1 ) {
133133 on_log (" Failed to get a packet from client." );
134- report_error ();
134+ report_get_last_error ();
135135 return ;
136136 }
137137
@@ -149,8 +149,10 @@ void rconpp::rcon_server::read_packet(connected_client& client) {
149149 if (packet_data == password) {
150150 packet_to_send = form_packet (" " , id, SERVERDATA_AUTH_RESPONSE);
151151 client.authenticated = true ;
152+ on_log (" Client [" + std::string (inet_ntoa (client.sock_info .sin_addr )) + " :" + std::to_string (ntohs (client.sock_info .sin_port )) + " ] has authenticated successfully!" );
152153 } else {
153154 packet_to_send = form_packet (" " , -1 , SERVERDATA_AUTH_RESPONSE);
155+ on_log (" Client [" + std::string (inet_ntoa (client.sock_info .sin_addr )) + " :" + std::to_string (ntohs (client.sock_info .sin_port )) + " ] failed authentication!" );
154156 }
155157 } else {
156158 if (type != SERVERDATA_EXECCOMMAND) {
@@ -184,9 +186,9 @@ void rconpp::rcon_server::read_packet(connected_client& client) {
184186
185187 on_log (" Sending packet (of size: " + std::to_string (packet_to_send.length ) + " ) to client [" + std::string (inet_ntoa (client.sock_info .sin_addr )) + " :" + std::to_string (ntohs (client.sock_info .sin_port )) + " ]" );
186188
187- if (send (client.socket , packet_to_send.data .data (), packet_to_send.length , 0 ) < 0 ) {
189+ if (send (client.socket , packet_to_send.data .data (), packet_to_send.length , MSG_NOSIGNAL ) < 0 ) {
188190 on_log (" Sending failed!" );
189- report_error ();
191+ report_get_last_error ();
190192 return ;
191193 }
192194}
@@ -195,9 +197,9 @@ bool rconpp::rcon_server::send_heartbeat(connected_client& client) {
195197 on_log (" Sending heartbeat to client [" + std::string (inet_ntoa (client.sock_info .sin_addr )) + " :" + std::to_string (ntohs (client.sock_info .sin_port )) + " ]" );
196198
197199 packet packet_to_send = form_packet (" " , -1 , SERVERDATA_RESPONSE_VALUE);
198- if (send (client.socket , packet_to_send.data .data (), packet_to_send.length , 0 ) < 0 ) {
200+ if (send (client.socket , packet_to_send.data .data (), packet_to_send.length , MSG_NOSIGNAL ) < 0 ) {
199201 on_log (" Failed to send a heartbeat to client [" + std::string (inet_ntoa (client.sock_info .sin_addr )) + " :" + std::to_string (ntohs (client.sock_info .sin_port )) + " ]" );
200- report_error ();
202+ report_get_last_error ();
201203 return false ;
202204 }
203205
@@ -238,11 +240,11 @@ void rconpp::rcon_server::start(bool return_after) {
238240
239241 if (client_socket == -1 ) {
240242 on_log (" client with socket: \" " + std::to_string (client_socket) + " \" failed to connect." );
241- report_error ();
243+ report_get_last_error ();
242244 continue ;
243245 }
244246
245- on_log (" Client [" + std::string (inet_ntoa (client_info.sin_addr )) + " :" + std::to_string (ntohs (client_info.sin_port )) + " ] has connected to the server." );
247+ on_log (" Client [" + std::string (inet_ntoa (client_info.sin_addr )) + " :" + std::to_string (ntohs (client_info.sin_port )) + " ] has connected to the server, asking for authentication ." );
246248
247249 connected_client client{};
248250
@@ -260,12 +262,16 @@ void rconpp::rcon_server::start(bool return_after) {
260262 if (client.last_heartbeat == 0 || current_time - client.last_heartbeat >= HEARTBEAT_TIME)
261263 {
262264 if (!send_heartbeat (client)) {
263- disconnect_client (client.socket );
264- return ;
265+ client.force_disconnect = true ;
265266 }
266267 }
267268 }
268269
270+ if (client.force_disconnect ) {
271+ disconnect_client (client.socket );
272+ return ;
273+ }
274+
269275 // No need to let the server keep running this causing 100% usage on a thread, we can wait a bit between requests.
270276 std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
271277 }
0 commit comments