mirror of
https://github.com/open-goal/jak-project
synced 2026-05-30 17:06:23 -04:00
69 lines
1.4 KiB
C++
69 lines
1.4 KiB
C++
#ifdef __linux
|
|
#include <sys/socket.h>
|
|
#include <netinet/tcp.h>
|
|
#include <unistd.h>
|
|
#elif _WIN32
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <WinSock2.h>
|
|
#include <WS2tcpip.h>
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
|
|
int open_socket(int af, int type, int protocol) {
|
|
#ifdef __linux
|
|
return socket(af, type, protocol);
|
|
#elif _WIN32
|
|
WSADATA wsaData = {0};
|
|
int iResult = 0;
|
|
// Initialize Winsock
|
|
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
if (iResult != 0) {
|
|
printf("WSAStartup failed: %d\n", iResult);
|
|
return 1;
|
|
}
|
|
return socket(af, type, protocol);
|
|
#endif
|
|
}
|
|
|
|
void close_socket(int sock) {
|
|
if (sock < 0) {
|
|
return;
|
|
}
|
|
#ifdef __linux
|
|
close(sock);
|
|
#elif _WIN32
|
|
closesocket(sock);
|
|
WSACleanup();
|
|
#endif
|
|
}
|
|
|
|
int set_socket_option(int socket, int level, int optname, const char* optval, int optlen) {
|
|
#ifdef __linux
|
|
return setsockopt(socket, level, optname, optval, optlen);
|
|
#elif _WIN32
|
|
int ret = setsockopt(socket, level, optname, optval, optlen);
|
|
if (ret < 0) {
|
|
int err = WSAGetLastError();
|
|
printf("Failed to setsockopt - Err: %d\n", err);
|
|
}
|
|
return ret;
|
|
#endif
|
|
}
|
|
|
|
int write_to_socket(int socket, const char* buf, int len) {
|
|
#ifdef __linux
|
|
return write(socket, buf, len);
|
|
#elif _WIN32
|
|
return send(socket, buf, len, 0);
|
|
#endif
|
|
}
|
|
|
|
int read_from_socket(int socket, char* buf, int len) {
|
|
#ifdef __linux
|
|
return read(socket, buf, len);
|
|
#elif _WIN32
|
|
return recv(socket, buf, len, 0);
|
|
#endif
|
|
}
|