mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2024-11-25 13:35:12 +00:00
Made sockets platform-independent, supporting Linux
This commit is contained in:
parent
d7a5a151d0
commit
a896713510
12 changed files with 211 additions and 74 deletions
7
Makefile
7
Makefile
|
@ -286,7 +286,7 @@ LEVEL_DIRS := $(patsubst levels/%,%,$(dir $(wildcard levels/*/header.h)))
|
||||||
# Directories containing source files
|
# Directories containing source files
|
||||||
|
|
||||||
# Hi, I'm a PC
|
# Hi, I'm a PC
|
||||||
SRC_DIRS := src src/engine src/game src/audio src/menu src/buffers actors levels bin data assets src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/pc/network src/pc/network/packets
|
SRC_DIRS := src src/engine src/game src/audio src/menu src/buffers actors levels bin data assets src/pc src/pc/gfx src/pc/audio src/pc/controller src/pc/fs src/pc/fs/packtypes src/pc/network src/pc/network/packets src/pc/network/socket
|
||||||
ASM_DIRS :=
|
ASM_DIRS :=
|
||||||
|
|
||||||
ifeq ($(DISCORDRPC),1)
|
ifeq ($(DISCORDRPC),1)
|
||||||
|
@ -526,9 +526,8 @@ ifeq ($(SDL_USED),2)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(WINDOWS_BUILD),1)
|
ifeq ($(WINDOWS_BUILD),1)
|
||||||
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS)
|
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -DWINSOCK
|
||||||
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv
|
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv -DWINSOCK
|
||||||
|
|
||||||
else ifeq ($(TARGET_WEB),1)
|
else ifeq ($(TARGET_WEB),1)
|
||||||
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -s USE_SDL=2
|
CC_CHECK := $(CC) -fsyntax-only -fsigned-char $(BACKEND_CFLAGS) $(INCLUDE_CFLAGS) -Wall -Wextra -Wno-format-security $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -s USE_SDL=2
|
||||||
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv -s USE_SDL=2
|
CFLAGS := $(OPT_FLAGS) $(INCLUDE_CFLAGS) $(BACKEND_CFLAGS) $(VERSION_CFLAGS) $(GRUCODE_CFLAGS) -fno-strict-aliasing -fwrapv -s USE_SDL=2
|
||||||
|
|
18
network.sh
Normal file → Executable file
18
network.sh
Normal file → Executable file
|
@ -1,5 +1,17 @@
|
||||||
set -e
|
set -e
|
||||||
make BETTERCAMERA=1 NODRAWINGDISTANCE=1 DEBUG=1 IMMEDIATELOAD=1
|
make BETTERCAMERA=1 NODRAWINGDISTANCE=1 DEBUG=1 IMMEDIATELOAD=1
|
||||||
./build/us_pc/sm64.us.f3dex2e.exe --server --configfile sm64config_server.txt &
|
|
||||||
#./build/us_pc/sm64.us.f3dex2e.exe --client --configfile sm64config_client.txt &
|
# find file
|
||||||
winpty cgdb ./build/us_pc/sm64.us.f3dex2e.exe -ex 'break debug_breakpoint_here' -ex 'run --client --configfile sm64config_client.txt' -ex 'quit'
|
FILE=./build/us_pc/sm64.us.f3dex2e.exe
|
||||||
|
if [ ! -f "$FILE" ]; then
|
||||||
|
FILE=./build/us_pc/sm64.us.f3dex2e
|
||||||
|
fi
|
||||||
|
|
||||||
|
$FILE --server --configfile sm64config_server.txt &
|
||||||
|
|
||||||
|
# debug if cgdb exists
|
||||||
|
if ! [ -x "$(command -v cgdb)" ]; then
|
||||||
|
$FILE --client --configfile sm64config_client.txt &
|
||||||
|
else
|
||||||
|
winpty cgdb $FILE -ex 'break debug_breakpoint_here' -ex 'run --client --configfile sm64config_client.txt' -ex 'quit'
|
||||||
|
fi
|
||||||
|
|
|
@ -2765,7 +2765,10 @@ s16 render_sync_level_screen(void) {
|
||||||
u8 colorFade = sins(gDialogColorFadeTimer) * 50.0f + 200.0f;
|
u8 colorFade = sins(gDialogColorFadeTimer) * 50.0f + 200.0f;
|
||||||
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_begin);
|
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_begin);
|
||||||
gDPSetEnvColor(gDisplayListHead++, colorFade, colorFade, colorFade, 255);
|
gDPSetEnvColor(gDisplayListHead++, colorFade, colorFade, colorFade, 255);
|
||||||
|
|
||||||
|
#define TEXT_SYNCHRONIZING 0x1C,0x22,0x17,0x0C,0x11,0x1B,0x18,0x17,0x12,0x02,0x12,0x17,0x10,0xFF
|
||||||
u8 synchronizing[] = { TEXT_SYNCHRONIZING };
|
u8 synchronizing[] = { TEXT_SYNCHRONIZING };
|
||||||
|
|
||||||
print_hud_lut_string(HUD_LUT_GLOBAL, 80, 200, synchronizing);
|
print_hud_lut_string(HUD_LUT_GLOBAL, 80, 200, synchronizing);
|
||||||
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_end);
|
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_end);
|
||||||
|
|
||||||
|
|
|
@ -263,7 +263,10 @@ void print_act_selector_strings(void) {
|
||||||
u8 colorFade = sin(fadeTimer++ * 0.2f) * 50.0f + 200.0f;
|
u8 colorFade = sin(fadeTimer++ * 0.2f) * 50.0f + 200.0f;
|
||||||
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_begin);
|
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_begin);
|
||||||
gDPSetEnvColor(gDisplayListHead++, colorFade, colorFade, colorFade, 255);
|
gDPSetEnvColor(gDisplayListHead++, colorFade, colorFade, colorFade, 255);
|
||||||
|
|
||||||
|
#define TEXT_SYNCHRONIZING 0x1C,0x22,0x17,0x0C,0x11,0x1B,0x18,0x17,0x12,0x02,0x12,0x17,0x10,0xFF
|
||||||
u8 synchronizing[] = { TEXT_SYNCHRONIZING };
|
u8 synchronizing[] = { TEXT_SYNCHRONIZING };
|
||||||
|
|
||||||
print_hud_lut_string(HUD_LUT_GLOBAL, 80, 8, synchronizing);
|
print_hud_lut_string(HUD_LUT_GLOBAL, 80, 8, synchronizing);
|
||||||
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_end);
|
gSPDisplayList(gDisplayListHead++, dl_rgba16_text_end);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,15 +2,7 @@
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "object_fields.h"
|
#include "object_fields.h"
|
||||||
#include "object_constants.h"
|
#include "object_constants.h"
|
||||||
|
#include "socket/socket.h"
|
||||||
// Winsock includes
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <Ws2tcpip.h>
|
|
||||||
//#pragma comment(lib, "Ws2_32.lib")
|
|
||||||
|
|
||||||
//////////////////////////
|
|
||||||
// TODO: port to linux! //
|
|
||||||
//////////////////////////
|
|
||||||
|
|
||||||
enum NetworkType networkType;
|
enum NetworkType networkType;
|
||||||
SOCKET gSocket;
|
SOCKET gSocket;
|
||||||
|
@ -20,62 +12,34 @@ void network_init(enum NetworkType inNetworkType) {
|
||||||
networkType = inNetworkType;
|
networkType = inNetworkType;
|
||||||
if (networkType == NT_NONE) { return; }
|
if (networkType == NT_NONE) { return; }
|
||||||
|
|
||||||
//-----------------------------------------------
|
|
||||||
// Initialize Winsock
|
|
||||||
WSADATA wsaData;
|
|
||||||
int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
||||||
if (rc != NO_ERROR) {
|
|
||||||
wprintf(L"%s WSAStartup failed with error %d\n", NETWORKTYPESTR, rc);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//-----------------------------------------------
|
|
||||||
// Create a receiver socket to receive datagrams
|
// Create a receiver socket to receive datagrams
|
||||||
gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
gSocket = socket_initialize();
|
||||||
if (gSocket == INVALID_SOCKET) {
|
if (gSocket == INVALID_SOCKET) { return; }
|
||||||
wprintf(L"%s socket failed with error %d\n", NETWORKTYPESTR, WSAGetLastError());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set non-blocking mode
|
|
||||||
u_long iMode = 1;
|
|
||||||
rc = ioctlsocket(gSocket, FIONBIO, &iMode);
|
|
||||||
if (rc != NO_ERROR) {
|
|
||||||
printf("%s ioctlsocket failed with error: %ld\n", NETWORKTYPESTR, rc);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bind the socket to any address and the specified port.
|
// Bind the socket to any address and the specified port.
|
||||||
struct sockaddr_in rxAddr;
|
unsigned int port = (networkType == NT_SERVER) ? 27015 : 27016;
|
||||||
rxAddr.sin_family = AF_INET;
|
int rc = socket_bind(gSocket, port);
|
||||||
rxAddr.sin_port = htons(networkType == NT_SERVER ? 27015 : 27016);
|
if (rc != NO_ERROR) { return; }
|
||||||
rxAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
rc = bind(gSocket, (SOCKADDR *)& rxAddr, sizeof(rxAddr));
|
|
||||||
if (rc != 0) {
|
|
||||||
wprintf(L"%s bind failed with error %d\n", NETWORKTYPESTR, WSAGetLastError());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the port to send to
|
// Save the port to send to
|
||||||
txPort = htons(networkType == NT_SERVER ? 27016 : 27015);
|
txPort = (networkType == NT_SERVER) ? 27016 : 27015;
|
||||||
}
|
}
|
||||||
|
|
||||||
void network_send(struct Packet* p) {
|
void network_send(struct Packet* p) {
|
||||||
|
// sanity checks
|
||||||
if (networkType == NT_NONE) { return; }
|
if (networkType == NT_NONE) { return; }
|
||||||
if (p->error) { printf("%s packet error!\n", NETWORKTYPESTR); return; }
|
if (p->error) { printf("%s packet error!\n", NETWORKTYPESTR); return; }
|
||||||
struct sockaddr_in txAddr;
|
|
||||||
txAddr.sin_family = AF_INET;
|
|
||||||
txAddr.sin_port = txPort;
|
|
||||||
txAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
||||||
|
|
||||||
|
// remember reliable packets
|
||||||
network_remember_reliable(p);
|
network_remember_reliable(p);
|
||||||
|
|
||||||
|
// save inside packet buffer
|
||||||
u32 hash = packet_hash(p);
|
u32 hash = packet_hash(p);
|
||||||
memcpy(&p->buffer[p->dataLength], &hash, sizeof(u32));
|
memcpy(&p->buffer[p->dataLength], &hash, sizeof(u32));
|
||||||
int rc = sendto(gSocket, p->buffer, p->cursor + sizeof(u32), 0, (SOCKADDR *)& txAddr, sizeof(txAddr));
|
|
||||||
if (rc == SOCKET_ERROR) {
|
|
||||||
wprintf(L"%s sendto failed with error: %d\n", NETWORKTYPESTR, WSAGetLastError());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// send
|
||||||
|
int rc = socket_send(gSocket, "127.0.0.1", txPort, p->buffer, p->cursor + sizeof(u32));
|
||||||
|
if (rc != NO_ERROR) { return; }
|
||||||
p->sent = true;
|
p->sent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,24 +57,19 @@ void network_update(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
struct sockaddr_in rxAddr;
|
// receive packet
|
||||||
int rxAddrSize = sizeof(rxAddr);
|
|
||||||
struct Packet p = { .cursor = 3 };
|
struct Packet p = { .cursor = 3 };
|
||||||
int rc = recvfrom(gSocket, p.buffer, PACKET_LENGTH, 0, (SOCKADDR *)&rxAddr, &rxAddrSize);
|
int rc = socket_receive(gSocket, p.buffer, PACKET_LENGTH, &p.dataLength);
|
||||||
if (rc == SOCKET_ERROR) {
|
if (rc != NO_ERROR) { break; }
|
||||||
int error = WSAGetLastError();
|
|
||||||
if (error != WSAEWOULDBLOCK && error != WSAECONNRESET) {
|
|
||||||
wprintf(L"%s recvfrom failed with error %d\n", NETWORKTYPESTR, WSAGetLastError());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (rc == 0) { break; }
|
|
||||||
|
|
||||||
p.dataLength = rc - sizeof(u32);
|
// subtract and check hash
|
||||||
|
p.dataLength -= sizeof(u32);
|
||||||
if (!packet_check_hash(&p)) {
|
if (!packet_check_hash(&p)) {
|
||||||
printf("Invalid packet!\n");
|
printf("Invalid packet!\n");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// execute packet
|
||||||
switch (p.buffer[0]) {
|
switch (p.buffer[0]) {
|
||||||
case PACKET_ACK: network_receive_ack(&p); break;
|
case PACKET_ACK: network_receive_ack(&p); break;
|
||||||
case PACKET_PLAYER: network_receive_player(&p); break;
|
case PACKET_PLAYER: network_receive_player(&p); break;
|
||||||
|
@ -134,9 +93,5 @@ void network_update(void) {
|
||||||
|
|
||||||
void network_shutdown(void) {
|
void network_shutdown(void) {
|
||||||
if (networkType == NT_NONE) { return; }
|
if (networkType == NT_NONE) { return; }
|
||||||
int rc = closesocket(gSocket);
|
socket_close(gSocket);
|
||||||
if (rc == SOCKET_ERROR) {
|
|
||||||
wprintf(L"%s closesocket failed with error %d\n", NETWORKTYPESTR, WSAGetLastError());
|
|
||||||
}
|
|
||||||
WSACleanup();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "../network.h"
|
#include "../network.h"
|
||||||
#include "object_fields.h"
|
#include "object_fields.h"
|
||||||
#include "object_constants.h"
|
#include "object_constants.h"
|
||||||
|
|
49
src/pc/network/socket/socket.c
Normal file
49
src/pc/network/socket/socket.c
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#include "socket.h"
|
||||||
|
#include "../network.h"
|
||||||
|
|
||||||
|
int socket_bind(SOCKET sock, unsigned int port) {
|
||||||
|
struct sockaddr_in rxAddr;
|
||||||
|
rxAddr.sin_family = AF_INET;
|
||||||
|
rxAddr.sin_port = htons(port);
|
||||||
|
rxAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
|
||||||
|
int rc = bind(sock, (SOCKADDR*)&rxAddr, sizeof(rxAddr));
|
||||||
|
if (rc != 0) {
|
||||||
|
printf("%s bind failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int socket_send(SOCKET sock, char* ip, unsigned int port, char* buffer, int bufferLength) {
|
||||||
|
struct sockaddr_in txAddr;
|
||||||
|
txAddr.sin_family = AF_INET;
|
||||||
|
txAddr.sin_port = htons(port);
|
||||||
|
txAddr.sin_addr.s_addr = inet_addr(ip);
|
||||||
|
|
||||||
|
int rc = sendto(sock, buffer, bufferLength, 0, (SOCKADDR*)&txAddr, sizeof(txAddr));
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
printf("%s sendto failed with error: %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
int socket_receive(SOCKET sock, char* buffer, int bufferLength, int* receiveLength) {
|
||||||
|
*receiveLength = 0;
|
||||||
|
|
||||||
|
struct sockaddr_in rxAddr;
|
||||||
|
int rxAddrSize = sizeof(rxAddr);
|
||||||
|
|
||||||
|
int rc = recvfrom(sock, buffer, bufferLength, 0, (SOCKADDR*)&rxAddr, &rxAddrSize);
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
int error = SOCKET_LAST_ERROR;
|
||||||
|
if (error != EWOULDBLOCK && error != ECONNRESET) {
|
||||||
|
printf("%s recvfrom failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
}
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
*receiveLength = rc;
|
||||||
|
return NO_ERROR;
|
||||||
|
}
|
16
src/pc/network/socket/socket.h
Normal file
16
src/pc/network/socket/socket.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef SOCKET_H
|
||||||
|
#define SOCKET_H
|
||||||
|
|
||||||
|
#ifdef WINSOCK
|
||||||
|
#include "socket_windows.h"
|
||||||
|
#else
|
||||||
|
#include "socket_linux.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SOCKET socket_initialize(void);
|
||||||
|
int socket_bind(SOCKET sock, unsigned int port);
|
||||||
|
int socket_send(SOCKET sock, char* ip, unsigned int port, char* buffer, int bufferLength);
|
||||||
|
int socket_receive(SOCKET sock, char* buffer, int bufferLength, int* receiveLength);
|
||||||
|
void socket_close(SOCKET sock);
|
||||||
|
|
||||||
|
#endif
|
30
src/pc/network/socket/socket_linux.c
Normal file
30
src/pc/network/socket/socket_linux.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef WINSOCK
|
||||||
|
#include "socket_linux.h"
|
||||||
|
#include "../network.h"
|
||||||
|
|
||||||
|
SOCKET socket_initialize(void) {
|
||||||
|
// initialize socket
|
||||||
|
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
if (sock == INVALID_SOCKET) {
|
||||||
|
printf("%s socket failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set non-blocking mode
|
||||||
|
int rc = fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
|
||||||
|
if (rc == INVALID_SOCKET) {
|
||||||
|
printf("%s fcntl failed with error: %d\n", NETWORKTYPESTR, rc);
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
void socket_close(SOCKET sock) {
|
||||||
|
int rc = closesocket(sock);
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
printf("%s closesocket failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
18
src/pc/network/socket/socket_linux.h
Normal file
18
src/pc/network/socket/socket_linux.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef SOCKET_LINUX_H
|
||||||
|
#define SOCKET_LINUX_H
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#define SOCKET unsigned int
|
||||||
|
#define INVALID_SOCKET (unsigned int)(-1)
|
||||||
|
#define SOCKET_LAST_ERROR errno
|
||||||
|
#define NO_ERROR (0)
|
||||||
|
#define SOCKADDR struct sockaddr
|
||||||
|
#define SOCKET_ERROR (-1)
|
||||||
|
#define closesocket(fd) close(fd)
|
||||||
|
|
||||||
|
#endif
|
40
src/pc/network/socket/socket_windows.c
Normal file
40
src/pc/network/socket/socket_windows.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#ifdef WINSOCK
|
||||||
|
#include "socket_windows.h"
|
||||||
|
#include "../network.h"
|
||||||
|
|
||||||
|
SOCKET socket_initialize(void) {
|
||||||
|
// start up winsock
|
||||||
|
WSADATA wsaData;
|
||||||
|
int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
if (rc != NO_ERROR) {
|
||||||
|
printf("%s WSAStartup failed with error %d\n", NETWORKTYPESTR, rc);
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize socket
|
||||||
|
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
if (sock == INVALID_SOCKET) {
|
||||||
|
printf("%s socket failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set non-blocking mode
|
||||||
|
u_long iMode = 1;
|
||||||
|
rc = ioctlsocket(sock, FIONBIO, &iMode);
|
||||||
|
if (rc != NO_ERROR) {
|
||||||
|
printf("%s ioctlsocket failed with error: %d\n", NETWORKTYPESTR, rc);
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
void socket_close(SOCKET sock) {
|
||||||
|
int rc = closesocket(sock);
|
||||||
|
if (rc == SOCKET_ERROR) {
|
||||||
|
printf("%s closesocket failed with error %d\n", NETWORKTYPESTR, SOCKET_LAST_ERROR);
|
||||||
|
}
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
11
src/pc/network/socket/socket_windows.h
Normal file
11
src/pc/network/socket/socket_windows.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef SOCKET_WINDOWS_H
|
||||||
|
#define SOCKET_WINDOWS_H
|
||||||
|
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <Ws2tcpip.h>
|
||||||
|
|
||||||
|
#define SOCKET_LAST_ERROR WSAGetLastError()
|
||||||
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
|
#define ECONNRESET WSAECONNRESET
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue