1
0
mirror of https://github.com/p2r3/bareiron.git synced 2025-10-01 23:25:09 +02:00

support native windows binary compilation

* add winsock2 to globals.c

* add winsocket2 compatibility to main function

* add winsocket2 to packets.c

* and winsocket2 to procedures.c

* add winsocket 2 compatibility to tools.c

* add winsocket2 to varnum.c

* add mingw64 linker options to build.sh

* fix build

* style nitpicks

* remove old_fd

* update compilation instructions for windows

---------

Co-authored-by: p2r3 <41925384+p2r3@users.noreply.github.com>
This commit is contained in:
jcfb
2025-09-14 15:11:56 +01:00
committed by GitHub
parent f6333429eb
commit 26f068bc09
8 changed files with 115 additions and 16 deletions

View File

@@ -19,9 +19,14 @@
#include "lwip/netdb.h"
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <unistd.h>
#include <time.h>
#endif
@@ -491,6 +496,13 @@ void handlePacket (int client_fd, int length, int packet_id, int state) {
}
int main () {
#ifdef _WIN32 //initialize windows socket
WSADATA wsa;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
fprintf(stderr, "WSAStartup failed\n");
exit(EXIT_FAILURE);
}
#endif
// Hash the seeds to ensure they're random enough
world_seed = splitmix64(world_seed);
@@ -528,8 +540,12 @@ int main () {
perror("socket failed");
exit(EXIT_FAILURE);
}
#ifdef _WIN32
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR,
(const char*)&opt, sizeof(opt)) < 0) {
#else
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
#endif
perror("socket options failed");
exit(EXIT_FAILURE);
}
@@ -555,8 +571,16 @@ int main () {
// Make the socket non-blocking
// This is necessary to not starve the idle task during slow connections
#ifdef _WIN32
u_long mode = 1; // 1 = non-blocking
if (ioctlsocket(server_fd, FIONBIO, &mode) != 0) {
fprintf(stderr, "Failed to set non-blocking mode\n");
exit(EXIT_FAILURE);
}
#else
int flags = fcntl(server_fd, F_GETFL, 0);
fcntl(server_fd, F_SETFL, flags | O_NONBLOCK);
#endif
// Track time of last server tick (in microseconds)
int64_t last_tick_time = get_program_time();
@@ -577,8 +601,13 @@ int main () {
// If the accept was successful, make the client non-blocking too
if (clients[i] != -1) {
printf("New client, fd: %d\n", clients[i]);
#ifdef _WIN32
u_long mode = 1;
ioctlsocket(clients[i], FIONBIO, &mode);
#else
int flags = fcntl(clients[i], F_GETFL, 0);
fcntl(clients[i], F_SETFL, flags | O_NONBLOCK);
#endif
client_count ++;
}
break;
@@ -600,6 +629,22 @@ int main () {
int client_fd = clients[client_index];
// Check if at least 2 bytes are available for reading
#ifdef _WIN32
recv_count = recv(client_fd, recv_buffer, 2, MSG_PEEK);
if (recv_count == 0) {
disconnectClient(&clients[client_index], 1);
continue;
}
if (recv_count == SOCKET_ERROR) {
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK) {
continue; // no data yet, keep client alive
} else {
disconnectClient(&clients[client_index], 1);
continue;
}
}
#else
recv_count = recv(client_fd, &recv_buffer, 2, MSG_PEEK);
if (recv_count < 2) {
if (recv_count == 0 || (recv_count < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
@@ -607,7 +652,7 @@ int main () {
}
continue;
}
#endif
// Handle 0xBEEF and 0xFEED packets for dumping/uploading world data
#ifdef DEV_ENABLE_BEEF_DUMPS
// Received BEEF packet, dump world data and disconnect
@@ -674,6 +719,11 @@ int main () {
}
close(server_fd);
#ifdef _WIN32 //cleanup windows socket
WSACleanup();
#endif
printf("Server closed.\n");
}