win32 build fix. fix crash when 2 or more slaves

This commit is contained in:
Flyinghead 2020-04-14 20:23:17 +02:00
parent c86fd8498f
commit 17c7e0b3ed
4 changed files with 61 additions and 19 deletions

View File

@ -34,7 +34,7 @@ sock_t NaomiNetwork::createAndBind(int protocol)
return INVALID_SOCKET;
}
int option = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&option, sizeof(option));
struct sockaddr_in serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
@ -55,6 +55,14 @@ sock_t NaomiNetwork::createAndBind(int protocol)
bool NaomiNetwork::init()
{
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
ERROR_LOG(NETWORK, "WSAStartup failed. errno=%d", get_last_error());
return false;
}
#endif
if (settings.network.ActAsServer)
return createBeaconSocket() && createServerSocket();
else
@ -124,7 +132,7 @@ bool NaomiNetwork::findServer()
// Allow broadcast packets to be sent
int broadcast = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) == -1)
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast)) == -1)
{
ERROR_LOG(NETWORK, "setsockopt(SO_BROADCAST) failed. errno=%d", get_last_error());
closesocket(sockfd);
@ -248,7 +256,7 @@ bool NaomiNetwork::startNetwork()
{
buf[1] = { (u8)slot_num };
slot_num++;
write(socket, buf, 2);
::send(socket, (const char *)buf, 2, 0);
set_non_blocking(socket);
set_tcp_nodelay(socket);
}
@ -304,7 +312,7 @@ bool NaomiNetwork::startNetwork()
gui_display_notification("Waiting for server to start", 10000);
set_recv_timeout(client_sock, (int)std::chrono::milliseconds(timeout * 2).count());
u8 buf[2];
if (read(client_sock, buf, 2) < 2)
if (::recv(client_sock, (char *)buf, 2, 0) < 2)
{
ERROR_LOG(NETWORK, "Connection failed: errno=%d", get_last_error());
closesocket(client_sock);
@ -330,14 +338,14 @@ bool NaomiNetwork::startNetwork()
void NaomiNetwork::pipeSlaves()
{
if (isMaster() || slot_count < 3)
if (!isMaster() || slot_count < 3)
return;
u8 buf[16384];
char buf[16384];
for (auto it = slaves.begin(); it != slaves.end() - 1; it++)
{
ssize_t l = read(*it, buf, sizeof(buf));
ssize_t l = ::recv(*it, buf, sizeof(buf), 0);
if (l > 0)
write(*(it + 1), buf, l);
::send(*(it + 1), buf, l, 0);
// TODO handle errors
}
}
@ -353,7 +361,7 @@ bool NaomiNetwork::receive(u8 *data, u32 size)
return false;
u16 pktnum;
ssize_t l = read(sockfd, &pktnum, sizeof(pktnum));
ssize_t l = ::recv(sockfd, (char *)&pktnum, sizeof(pktnum), 0);
if (l <= 0)
{
if (get_last_error() != L_EAGAIN && get_last_error() != L_EWOULDBLOCK)
@ -373,7 +381,7 @@ bool NaomiNetwork::receive(u8 *data, u32 size)
ssize_t received = 0;
while (received != size && !network_stopping)
{
l = read(sockfd, data + received, size - received);
l = ::recv(sockfd, (char*)(data + received), size - received, 0);
if (l <= 0)
{
if (get_last_error() != L_EAGAIN && get_last_error() != L_EWOULDBLOCK)
@ -410,21 +418,27 @@ void NaomiNetwork::send(u8 *data, u32 size)
return;
u16 pktnum = packet_number + 1;
struct iovec iov[] = { { &pktnum, sizeof(pktnum) }, { data, size } };
struct msghdr msg{};
msg.msg_iov = iov;
msg.msg_iovlen = ARRAY_SIZE(iov);
if (sendmsg(sockfd, &msg, 0) < 0)
if (::send(sockfd, (const char *)&pktnum, sizeof(pktnum), 0) < 2)
{
if (errno != L_EAGAIN && errno != L_EWOULDBLOCK)
{
WARN_LOG(NETWORK, "sendmsg failed. errno=%d", get_last_error());
WARN_LOG(NETWORK, "send failed. errno=%d", get_last_error());
if (isMaster())
{
slaves.front() = -1;
closesocket(sockfd);
}
}
return;
}
if (::send(sockfd, (const char *)data, size, 0) < size)
{
WARN_LOG(NETWORK, "send failed. errno=%d", get_last_error());
if (isMaster())
{
slaves.front() = -1;
closesocket(sockfd);
}
}
else
{

View File

@ -29,6 +29,13 @@
class NaomiNetwork
{
public:
NaomiNetwork() {
#ifdef _WIN32
server_ip.S_un.S_addr = INADDR_NONE;
#else
server_ip.s_addr = INADDR_NONE;
#endif
}
~NaomiNetwork() { terminate(); }
bool init();
bool startNetwork();
@ -47,10 +54,10 @@ private:
bool createBeaconSocket();
void processBeacon();
bool findServer();
int createAndBind(int protocol);
sock_t createAndBind(int protocol);
bool isMaster() const { return slot_id == 0; }
struct in_addr server_ip{ INADDR_NONE };
struct in_addr server_ip;
std::string server_name;
// server stuff
sock_t server_sock = INVALID_SOCKET;

View File

@ -80,8 +80,29 @@ static inline void set_tcp_nodelay(sock_t fd)
static inline bool set_recv_timeout(sock_t fd, int delayms)
{
#ifdef _WIN32
const DWORD dwDelay = delayms;
return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&dwDelay, sizeof(DWORD)) == 0;
#else
struct timeval tv;
tv.tv_sec = delayms / 1000;
tv.tv_usec = (delayms % 1000) * 1000;
return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == 0;
#endif
}
#if defined(_WIN32) && _WIN32_WINNT < 0x0600
static inline const char *inet_ntop(int af, const void* src, char* dst, int cnt)
{
struct sockaddr_in srcaddr;
memset(&srcaddr, 0, sizeof(struct sockaddr_in));
memcpy(&srcaddr.sin_addr, src, sizeof(srcaddr.sin_addr));
srcaddr.sin_family = af;
if (WSAAddressToString((struct sockaddr *)&srcaddr, sizeof(struct sockaddr_in), 0, dst, (LPDWORD)&cnt) != 0)
return nullptr;
else
return dst;
}
#endif

View File

@ -264,7 +264,7 @@ else ifneq (,$(findstring win32,$(platform)))
NOT_ARM := 1
CFLAGS += -fno-builtin-sqrtf -funroll-loops -DHAVE_FSEEKO -I /mingw64/include
LDFLAGS += -static-libgcc -static-libstdc++ -Wl,-subsystem,windows
LIBS := -lopengl32 -lwinmm -lgdi32 -lwsock32 -ldsound -lcomctl32 -lcomdlg32 -lxinput -liphlpapi -Wl,-Bstatic -lgomp
LIBS := -lopengl32 -lwinmm -lgdi32 -lwsock32 -lws2_32 -ldsound -lcomctl32 -lcomdlg32 -lxinput -liphlpapi -Wl,-Bstatic -lgomp
PLATFORM_EXT := exe
CC = gcc
CXX = g++