mirror of
https://github.com/p2r3/bareiron.git
synced 2025-10-02 07:35:08 +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:
@@ -1,6 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <arpa/inet.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
|
||||
#include "globals.h"
|
||||
|
60
src/main.c
60
src/main.c
@@ -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");
|
||||
|
||||
}
|
||||
|
@@ -6,7 +6,12 @@
|
||||
#include "lwip/netdb.h"
|
||||
#include "esp_task_wdt.h"
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
|
@@ -2,6 +2,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#include "globals.h"
|
||||
#include "tools.h"
|
||||
@@ -181,9 +184,14 @@ void disconnectClient (int *client_fd, int cause) {
|
||||
client_count --;
|
||||
setClientState(*client_fd, STATE_NONE);
|
||||
handlePlayerDisconnect(*client_fd);
|
||||
#ifdef _WIN32
|
||||
closesocket(*client_fd);
|
||||
printf("Disconnected client %d, cause: %d, errno: %d\n", *client_fd, cause, WSAGetLastError());
|
||||
#else
|
||||
close(*client_fd);
|
||||
*client_fd = -1;
|
||||
printf("Disconnected client %d, cause: %d, errno: %d\n\n", *client_fd, cause, errno);
|
||||
#endif
|
||||
*client_fd = -1;
|
||||
}
|
||||
|
||||
uint8_t serverSlotToClientSlot (int window_id, uint8_t slot) {
|
||||
|
20
src/tools.c
20
src/tools.c
@@ -7,8 +7,13 @@
|
||||
#include "lwip/netdb.h"
|
||||
#include "esp_timer.h"
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#ifndef CLOCK_MONOTONIC
|
||||
@@ -95,7 +100,11 @@ ssize_t send_all (int client_fd, const void *buf, ssize_t len) {
|
||||
|
||||
// Busy-wait (with task yielding) until all data has been sent
|
||||
while (sent < len) {
|
||||
ssize_t n = send(client_fd, p + sent, len - sent, MSG_NOSIGNAL);
|
||||
#ifdef _WIN32
|
||||
ssize_t n = send(client_fd, p + sent, len - sent, 0);
|
||||
#else
|
||||
ssize_t n = send(client_fd, p + sent, len - sent, MSG_NOSIGNAL);
|
||||
#endif
|
||||
if (n > 0) { // some data was sent, log it
|
||||
sent += n;
|
||||
last_update_time = get_program_time();
|
||||
@@ -106,7 +115,12 @@ ssize_t send_all (int client_fd, const void *buf, ssize_t len) {
|
||||
return -1;
|
||||
}
|
||||
// not yet ready to transmit, try again
|
||||
#ifdef _WIN32 //handles windows socket timeout
|
||||
int err = WSAGetLastError();
|
||||
if (err == WSAEWOULDBLOCK || err == WSAEINTR) {
|
||||
#else
|
||||
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
#endif
|
||||
// handle network timeout
|
||||
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
|
||||
disconnectClient(&client_fd, -2);
|
||||
|
@@ -1,5 +1,10 @@
|
||||
#include <stdint.h>
|
||||
#include <arpa/inet.h>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
|
||||
#include "varnum.h"
|
||||
|
Reference in New Issue
Block a user