Skip to content

Commit

Permalink
rainbow: fix Linux + macOS compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BrokeStudio committed Nov 4, 2023
1 parent 9e52daf commit d166c96
Showing 1 changed file with 63 additions and 67 deletions.
130 changes: 63 additions & 67 deletions Core/NES/Mappers/Homebrew/RainbowESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include <sstream>
#include <stdexcept>

#define ERR_MSG_SIZE 513

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)

#define ERR_MSG_SIZE 513

// UDP networking
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define WIN32_LEAN_AND_MEAN
Expand All @@ -45,6 +45,10 @@ typedef SSIZE_T ssize_t;
#define cast_network_payload(x) reinterpret_cast<void*>(x)
#define close_sock(x) ::close(x)

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif

#endif

#ifndef RAINBOW_DEBUG_ESP
Expand Down Expand Up @@ -85,7 +89,7 @@ BrokeStudioFirmware::BrokeStudioFirmware()
UDBG("[Rainbow] BrokeStudioFirmware constructor");

// Get default host/port

#ifdef _WIN32
char* value = nullptr;
size_t len;

Expand All @@ -103,6 +107,18 @@ BrokeStudioFirmware::BrokeStudioFirmware()
}

free(value);
#else
char const* hostname = ::getenv("RAINBOW_SERVER_ADDR");
if(hostname == nullptr) hostname = "";
this->server_settings_address = hostname;
this->default_server_settings_address = hostname;

char const* port_cstr = ::getenv("RAINBOW_SERVER_PORT");
if(port_cstr == nullptr) port_cstr = "0";
std::istringstream port_iss(port_cstr);
port_iss >> this->server_settings_port;
this->default_server_settings_port = this->server_settings_port;
#endif

// Init fake registered networks
this->networks = { {
Expand Down Expand Up @@ -749,16 +765,7 @@ void BrokeStudioFirmware::processBufferedMessage()
}

// special case just for emulation
if(filename == "/web/") {
//if(::getenv("RAINBOW_WWW_ROOT") != NULL) i = 1;
char* value = nullptr;
size_t len;
if(_dupenv_s(&value, &len, "RAINBOW_WWW_ROOT") == 0 && value != nullptr) {
i = 1;
free(value);
}

} else {
if(filename != "/web/") {
if(filename.find_last_of("/") == filename.length() - 1) {
i = findPath(file_config.drive, filename);
} else {
Expand Down Expand Up @@ -1313,6 +1320,7 @@ void BrokeStudioFirmware::writeFile(I data_begin, I data_end)

void BrokeStudioFirmware::saveFiles()
{
#ifdef _WIN32
char* value = nullptr;
size_t len;

Expand All @@ -1331,22 +1339,21 @@ void BrokeStudioFirmware::saveFiles()
}

free(value);
#else
char const* esp_filesystem_file_path = ::getenv("RAINBOW_ESP_FILESYSTEM_FILE");
if(esp_filesystem_file_path == NULL) {
MessageManager::Log("[Rainbow] RAINBOW_ESP_FILESYSTEM_FILE environment variable is not set");
} else {
saveFile(0, esp_filesystem_file_path);
}

/*
char const* esp_filesystem_file_path = ::getenv("RAINBOW_ESP_FILESYSTEM_FILE");
if(esp_filesystem_file_path == NULL) {
MessageManager::Log("[Rainbow] RAINBOW_ESP_FILESYSTEM_FILE environment variable is not set");
} else {
saveFile(0, esp_filesystem_file_path);
}
char const* sd_filesystem_file_path = ::getenv("RAINBOW_SD_FILESYSTEM_FILE");
if(sd_filesystem_file_path == NULL) {
MessageManager::Log("[Rainbow] RAINBOW_SD_FILESYSTEM_FILE environment variable is not set");
} else {
saveFile(2, sd_filesystem_file_path);
}
*/
char const* sd_filesystem_file_path = ::getenv("RAINBOW_SD_FILESYSTEM_FILE");
if(sd_filesystem_file_path == NULL) {
MessageManager::Log("[Rainbow] RAINBOW_SD_FILESYSTEM_FILE environment variable is not set");
} else {
saveFile(2, sd_filesystem_file_path);
}
#endif
}

void BrokeStudioFirmware::saveFile(uint8_t drive, char const* filename)
Expand Down Expand Up @@ -1399,6 +1406,7 @@ void BrokeStudioFirmware::saveFile(uint8_t drive, char const* filename)

void BrokeStudioFirmware::loadFiles()
{
#ifdef _WIN32
char* value = nullptr;
size_t len;

Expand All @@ -1421,26 +1429,25 @@ void BrokeStudioFirmware::loadFiles()
}

free(value);
#else
char const* esp_filesystem_file_path = ::getenv("RAINBOW_ESP_FILESYSTEM_FILE");
if(esp_filesystem_file_path == NULL) {
isEspFlashFilePresent = false;
MessageManager::Log("[Rainbow] RAINBOW_ESP_FILESYSTEM_FILE environment variable is not set");
} else {
isEspFlashFilePresent = true;
loadFile(0, esp_filesystem_file_path);
}

/*
char const* esp_filesystem_file_path = ::getenv("RAINBOW_ESP_FILESYSTEM_FILE");
if(esp_filesystem_file_path == NULL) {
isEspFlashFilePresent = false;
MessageManager::Log("[Rainbow] RAINBOW_ESP_FILESYSTEM_FILE environment variable is not set");
} else {
isEspFlashFilePresent = true;
loadFile(0, esp_filesystem_file_path);
}
char const* sd_filesystem_file_path = ::getenv("RAINBOW_SD_FILESYSTEM_FILE");
if(sd_filesystem_file_path == NULL) {
isSdCardFilePresent = false;
MessageManager::Log("[Rainbow] RAINBOW_SD_FILESYSTEM_FILE environment variable is not set");
} else {
isSdCardFilePresent = true;
loadFile(2, sd_filesystem_file_path);
}
*/
char const* sd_filesystem_file_path = ::getenv("RAINBOW_SD_FILESYSTEM_FILE");
if(sd_filesystem_file_path == NULL) {
isSdCardFilePresent = false;
MessageManager::Log("[Rainbow] RAINBOW_SD_FILESYSTEM_FILE environment variable is not set");
} else {
isSdCardFilePresent = true;
loadFile(2, sd_filesystem_file_path);
}
#endif
}

void BrokeStudioFirmware::loadFile(uint8_t drive, char const* filename)
Expand Down Expand Up @@ -1541,8 +1548,6 @@ void BrokeStudioFirmware::clearFiles(uint8_t drive)
template<class I>
void BrokeStudioFirmware::sendUdpDatagramToServer(I begin, I end)
{
char errmsg[ERR_MSG_SIZE];

#if RAINBOW_DEBUG_ESP >= 1
UDBG("[Rainbow] " + std::to_string(wall_clock_milli()) + " udp datagram to send");
# if RAINBOW_DEBUG_ESP >= 2
Expand All @@ -1566,8 +1571,7 @@ void BrokeStudioFirmware::sendUdpDatagramToServer(I begin, I end)
);

if(n == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] UDP send failed: " + string(errmsg));
UDBG("[Rainbow] UDP send failed: " + strerror(errno));
} else if(static_cast<size_t>(n) != message_size) {
UDBG("[Rainbow] UDP sent partial message");
}
Expand Down Expand Up @@ -1602,8 +1606,7 @@ void BrokeStudioFirmware::sendTcpDataToServer(I begin, I end)
0
);
if(n == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] TCP send failed: " + string(errmsg));
UDBG("[Rainbow] TCP send failed: " + strerror(errno));
} else if(static_cast<size_t>(n) != message_size) {
UDBG("[Rainbow] TCP sent partial message");
}
Expand All @@ -1620,29 +1623,25 @@ std::deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
tv.tv_sec = 0;
tv.tv_usec = 0;

char errmsg[ERR_MSG_SIZE];

int n_readable = ::select(socket + 1, &rfds, NULL, NULL, &tv);
if(n_readable == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] failed to check sockets for data: " + string(errmsg));
UDBG("[Rainbow] failed to check sockets for data: " + std::string(strerror(errno)));
} else if(n_readable > 0) {
if(FD_ISSET(socket, &rfds)) {
size_t const MAX_MSG_SIZE = 254;
std::vector<uint8_t> data;
data.resize(MAX_MSG_SIZE);

sockaddr_in addr_from;
//socklen_t addr_from_len = sizeof(addr_from);
int addr_from_len = sizeof(addr_from);
socklen_t addr_from_len = sizeof(addr_from);
//int addr_from_len = sizeof(addr_from);
ssize_t msg_len = ::recvfrom(
socket, cast_network_payload(data.data()), MAX_MSG_SIZE, 0,
reinterpret_cast<sockaddr*>(&addr_from), &addr_from_len
);

if(msg_len == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] failed to read socket: " + string(errmsg));
UDBG("[Rainbow] failed to read socket: " + std::string(strerror(errno)));
} else if(msg_len <= static_cast<ssize_t>(MAX_MSG_SIZE)) {
UDBG("[Rainbow] " + std::to_string(wall_clock_milli()) + " received message of size " + std::to_string(msg_len));
#if RAINBOW_DEBUG_ESP >= 2
Expand Down Expand Up @@ -1712,15 +1711,13 @@ void BrokeStudioFirmware::openConnection()
// Create socket
this->tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
if(this->tcp_socket == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] unable to create TCP socket: " + string(errmsg));
UDBG("[Rainbow] unable to create TCP socket: " + std::string(strerror(errno)));
}

// Connect to server
int connect_res = connect(this->tcp_socket, &server_addr.second, sizeof(sockaddr));
if(connect_res == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] unable to connect to TCP server: " + string(errmsg));
UDBG("[Rainbow] unable to connect to TCP server: " + std::string(strerror(errno)));
this->tcp_socket = -1;
}
} else if(this->active_protocol == server_protocol_t::TCP_SECURED) {
Expand All @@ -1736,8 +1733,7 @@ void BrokeStudioFirmware::openConnection()

this->udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
if(this->udp_socket == -1) {
strerror_s(errmsg, ERR_MSG_SIZE, errno);
UDBG("[Rainbow] unable to connect to UDP server: " + string(errmsg));
UDBG("[Rainbow] unable to connect to UDP server: " + std::string(strerror(errno)));
}

sockaddr_in bind_addr;
Expand Down

0 comments on commit d166c96

Please sign in to comment.