forked from EXTERNAL/bareiron
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:
@@ -15,10 +15,13 @@ For PC x86_64 platforms, grab the [latest build binary](https://github.com/p2r3/
|
|||||||
For microcontrollers, see the section on **compilation** below.
|
For microcontrollers, see the section on **compilation** below.
|
||||||
|
|
||||||
## Compilation
|
## Compilation
|
||||||
Before compiling, you'll need to dump registry data from a vanilla Minecraft server. On Linux, this can be done automatically using the `extract_registries.sh` script. Otherwise, the manual process is as follows: create a folder called `notchian` here, and put a Minecraft server JAR in it. Then, follow [this guide](https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Data_Generators) to dump all of the registries. Finally, run `build_registries.js` with `node`, `bun`, or `deno`.
|
Before compiling, you'll need to dump registry data from a vanilla Minecraft server. On Linux, this can be done automatically using the `extract_registries.sh` script. Otherwise, the manual process is as follows: create a folder called `notchian` here, and put a Minecraft server JAR in it. Then, follow [this guide](https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Data_Generators) to dump all of the registries (use the _second_ command with the `--all` flag). Finally, run `build_registries.js` with either [bun](https://bun.sh/), [node](https://nodejs.org/en/download), or [deno](https://docs.deno.com/runtime/getting_started/installation/).
|
||||||
|
|
||||||
- To target Linux, install `gcc` and run `./build.sh`
|
- To compile on Linux, install `gcc` and run `./build.sh`.
|
||||||
- To target Windows, install [MSYS2](https://www.msys2.org/), and open the "MSYS2 MSYS" shell once installed. From there, install `gcc` (run `pacman -Sy gcc`), navigate to this project's directory and run `./build.sh`. Alternatively, install WSL and use `gcc`. Otherwise, there's no platform-native solution for Windows currently.
|
- For compiling on Windows, there are a few options:
|
||||||
|
- To compile a native Windows binary: install [MSYS2](https://www.msys2.org/) and open the "MSYS2 MINGW64" shell. From there, run `pacman -Sy mingw-w64-x86_64-gcc`, navigate to this project's directory, and run `./build.sh`.
|
||||||
|
- To compile a MSYS2-linked binary: install [MSYS2](https://www.msys2.org/), and open the "MSYS2 MSYS" shell. From there, install `gcc` (run `pacman -Sy gcc`), navigate to this project's directory and run `./build.sh`.
|
||||||
|
- To compile and run a Linux binary from Windows: install WSL, and from there install `gcc` and run `./build.sh` in this project's directory.
|
||||||
- To target an ESP variant, set up a PlatformIO project (select the ESP-IDF framework, **not Arduino**) and clone this repository on top of it. See **Configuration** below for further steps. For better performance, consider changing the clock speed and enabling compiler optimizations. If you don't know how to do this, there are plenty of resources online.
|
- To target an ESP variant, set up a PlatformIO project (select the ESP-IDF framework, **not Arduino**) and clone this repository on top of it. See **Configuration** below for further steps. For better performance, consider changing the clock speed and enabling compiler optimizations. If you don't know how to do this, there are plenty of resources online.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
11
build.sh
11
build.sh
@@ -13,6 +13,15 @@ case "$OSTYPE" in
|
|||||||
*) exe="" ;;
|
*) exe="" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
# mingw64-specific linker options
|
||||||
|
windows_linker=""
|
||||||
|
unameOut="$(uname -s)"
|
||||||
|
case "$unameOut" in
|
||||||
|
MINGW64_NT*)
|
||||||
|
windows_linker="-static -lws2_32 -pthread"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
rm -f "bareiron$exe"
|
rm -f "bareiron$exe"
|
||||||
gcc src/*.c -O3 -Iinclude -o "bareiron$exe"
|
gcc src/*.c -O3 -Iinclude -o "bareiron$exe" $windows_linker
|
||||||
"./bareiron$exe"
|
"./bareiron$exe"
|
||||||
|
@@ -1,6 +1,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.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 <unistd.h>
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
|
60
src/main.c
60
src/main.c
@@ -19,9 +19,14 @@
|
|||||||
#include "lwip/netdb.h"
|
#include "lwip/netdb.h"
|
||||||
#else
|
#else
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#ifdef _WIN32
|
||||||
#include <netinet/in.h>
|
#include <winsock2.h>
|
||||||
#include <arpa/inet.h>
|
#include <ws2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -491,6 +496,13 @@ void handlePacket (int client_fd, int length, int packet_id, int state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
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
|
// Hash the seeds to ensure they're random enough
|
||||||
world_seed = splitmix64(world_seed);
|
world_seed = splitmix64(world_seed);
|
||||||
@@ -528,8 +540,12 @@ int main () {
|
|||||||
perror("socket failed");
|
perror("socket failed");
|
||||||
exit(EXIT_FAILURE);
|
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) {
|
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
|
||||||
|
#endif
|
||||||
perror("socket options failed");
|
perror("socket options failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -555,8 +571,16 @@ int main () {
|
|||||||
|
|
||||||
// Make the socket non-blocking
|
// Make the socket non-blocking
|
||||||
// This is necessary to not starve the idle task during slow connections
|
// 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);
|
int flags = fcntl(server_fd, F_GETFL, 0);
|
||||||
fcntl(server_fd, F_SETFL, flags | O_NONBLOCK);
|
fcntl(server_fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Track time of last server tick (in microseconds)
|
// Track time of last server tick (in microseconds)
|
||||||
int64_t last_tick_time = get_program_time();
|
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 the accept was successful, make the client non-blocking too
|
||||||
if (clients[i] != -1) {
|
if (clients[i] != -1) {
|
||||||
printf("New client, fd: %d\n", clients[i]);
|
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);
|
int flags = fcntl(clients[i], F_GETFL, 0);
|
||||||
fcntl(clients[i], F_SETFL, flags | O_NONBLOCK);
|
fcntl(clients[i], F_SETFL, flags | O_NONBLOCK);
|
||||||
|
#endif
|
||||||
client_count ++;
|
client_count ++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -600,6 +629,22 @@ int main () {
|
|||||||
int client_fd = clients[client_index];
|
int client_fd = clients[client_index];
|
||||||
|
|
||||||
// Check if at least 2 bytes are available for reading
|
// 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);
|
recv_count = recv(client_fd, &recv_buffer, 2, MSG_PEEK);
|
||||||
if (recv_count < 2) {
|
if (recv_count < 2) {
|
||||||
if (recv_count == 0 || (recv_count < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
|
if (recv_count == 0 || (recv_count < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
|
||||||
@@ -607,7 +652,7 @@ int main () {
|
|||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
// Handle 0xBEEF and 0xFEED packets for dumping/uploading world data
|
// Handle 0xBEEF and 0xFEED packets for dumping/uploading world data
|
||||||
#ifdef DEV_ENABLE_BEEF_DUMPS
|
#ifdef DEV_ENABLE_BEEF_DUMPS
|
||||||
// Received BEEF packet, dump world data and disconnect
|
// Received BEEF packet, dump world data and disconnect
|
||||||
@@ -674,6 +719,11 @@ int main () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
|
|
||||||
|
#ifdef _WIN32 //cleanup windows socket
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("Server closed.\n");
|
printf("Server closed.\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,12 @@
|
|||||||
#include "lwip/netdb.h"
|
#include "lwip/netdb.h"
|
||||||
#include "esp_task_wdt.h"
|
#include "esp_task_wdt.h"
|
||||||
#else
|
#else
|
||||||
#include <arpa/inet.h>
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -2,6 +2,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
@@ -181,9 +184,14 @@ void disconnectClient (int *client_fd, int cause) {
|
|||||||
client_count --;
|
client_count --;
|
||||||
setClientState(*client_fd, STATE_NONE);
|
setClientState(*client_fd, STATE_NONE);
|
||||||
handlePlayerDisconnect(*client_fd);
|
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);
|
close(*client_fd);
|
||||||
*client_fd = -1;
|
|
||||||
printf("Disconnected client %d, cause: %d, errno: %d\n\n", *client_fd, cause, errno);
|
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) {
|
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 "lwip/netdb.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#else
|
#else
|
||||||
#include <sys/socket.h>
|
#ifdef _WIN32
|
||||||
#include <arpa/inet.h>
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#ifndef CLOCK_MONOTONIC
|
#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
|
// Busy-wait (with task yielding) until all data has been sent
|
||||||
while (sent < len) {
|
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
|
if (n > 0) { // some data was sent, log it
|
||||||
sent += n;
|
sent += n;
|
||||||
last_update_time = get_program_time();
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
// not yet ready to transmit, try again
|
// 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) {
|
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||||
|
#endif
|
||||||
// handle network timeout
|
// handle network timeout
|
||||||
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
|
if (get_program_time() - last_update_time > NETWORK_TIMEOUT_TIME) {
|
||||||
disconnectClient(&client_fd, -2);
|
disconnectClient(&client_fd, -2);
|
||||||
|
@@ -1,5 +1,10 @@
|
|||||||
#include <stdint.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 <unistd.h>
|
||||||
|
|
||||||
#include "varnum.h"
|
#include "varnum.h"
|
||||||
|
Reference in New Issue
Block a user