forked from EXTERNAL/bareiron
terminate connections on long network timeouts
This commit is contained in:
@@ -15,6 +15,7 @@ void resetPlayerData (PlayerData *player);
|
|||||||
int reservePlayerData (int client_fd, uint8_t *uuid, char* name);
|
int reservePlayerData (int client_fd, uint8_t *uuid, char* name);
|
||||||
int getPlayerData (int client_fd, PlayerData **output);
|
int getPlayerData (int client_fd, PlayerData **output);
|
||||||
void handlePlayerDisconnect (int client_fd);
|
void handlePlayerDisconnect (int client_fd);
|
||||||
|
void disconnectClient (int *client_fd, int cause);
|
||||||
int givePlayerItem (PlayerData *player, uint16_t item, uint8_t count);
|
int givePlayerItem (PlayerData *player, uint16_t item, uint8_t count);
|
||||||
void spawnPlayer (PlayerData *player);
|
void spawnPlayer (PlayerData *player);
|
||||||
|
|
||||||
|
11
src/main.c
11
src/main.c
@@ -444,16 +444,6 @@ void handlePacket (int client_fd, int length, int packet_id, int state) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnectClient (int *client_fd, int cause) {
|
|
||||||
if (*client_fd == -1) return;
|
|
||||||
client_count --;
|
|
||||||
setClientState(*client_fd, STATE_NONE);
|
|
||||||
handlePlayerDisconnect(*client_fd);
|
|
||||||
close(*client_fd);
|
|
||||||
*client_fd = -1;
|
|
||||||
printf("Disconnected client %d, cause: %d, errno: %d\n\n", *client_fd, cause, errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
|
|
||||||
// Hash the seeds to ensure they're random enough
|
// Hash the seeds to ensure they're random enough
|
||||||
@@ -655,6 +645,7 @@ void wifi_init () {
|
|||||||
|
|
||||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||||
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||||
|
esp_wifi_set_ps(WIFI_PS_NONE);
|
||||||
esp_wifi_start();
|
esp_wifi_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
@@ -145,6 +146,16 @@ void handlePlayerDisconnect (int client_fd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void disconnectClient (int *client_fd, int cause) {
|
||||||
|
if (*client_fd == -1) return;
|
||||||
|
client_count --;
|
||||||
|
setClientState(*client_fd, STATE_NONE);
|
||||||
|
handlePlayerDisconnect(*client_fd);
|
||||||
|
close(*client_fd);
|
||||||
|
*client_fd = -1;
|
||||||
|
printf("Disconnected client %d, cause: %d, errno: %d\n\n", *client_fd, cause, errno);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t serverSlotToClientSlot (int window_id, uint8_t slot) {
|
uint8_t serverSlotToClientSlot (int window_id, uint8_t slot) {
|
||||||
|
|
||||||
if (window_id == 0) { // player inventory
|
if (window_id == 0) { // player inventory
|
||||||
|
11
src/tools.c
11
src/tools.c
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "varnum.h"
|
#include "varnum.h"
|
||||||
|
#include "procedures.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
|
|
||||||
static uint64_t htonll (uint64_t value) {
|
static uint64_t htonll (uint64_t value) {
|
||||||
@@ -57,7 +58,10 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
|
|||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
// handle network timeout
|
// handle network timeout
|
||||||
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) return -1;
|
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
|
||||||
|
disconnectClient(&client_fd, -1);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
task_yield();
|
task_yield();
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
@@ -101,7 +105,10 @@ ssize_t send_all (int client_fd, const void *buf, ssize_t len) {
|
|||||||
// not yet ready to transmit, try again
|
// not yet ready to transmit, try again
|
||||||
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
// handle network timeout
|
// handle network timeout
|
||||||
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) return -1;
|
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
|
||||||
|
disconnectClient(&client_fd, -2);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
task_yield();
|
task_yield();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user