mirror of
https://github.com/open-goal/jak-project.git
synced 2024-10-20 11:26:18 -04:00
Created a cross-platform socket shim
This commit is contained in:
parent
2075dd66b6
commit
1ab8329e3c
12
common/cross_sockets/CMakeLists.txt
Normal file
12
common/cross_sockets/CMakeLists.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
add_library(cross_sockets
|
||||
SHARED
|
||||
"xsocket.h"
|
||||
"xsocket.cpp")
|
||||
|
||||
IF (WIN32)
|
||||
# set stuff for windows
|
||||
target_link_libraries(cross_sockets wsock32 ws2_32)
|
||||
ELSE()
|
||||
# set stuff for other systems
|
||||
target_link_libraries(cross_sockets)
|
||||
ENDIF()
|
45
common/cross_sockets/xsocket.cpp
Normal file
45
common/cross_sockets/xsocket.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
#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
|
||||
|
||||
void close_socket(int sock) {
|
||||
if (sock < 0) {
|
||||
return;
|
||||
}
|
||||
#ifdef __linux
|
||||
close(sock);
|
||||
#elif _WIN32
|
||||
closesocket(sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
int set_socket_option(int socket, int level, int optname, int optval, int optlen) {
|
||||
#ifdef __linux
|
||||
return setsockopt(socket, level, optname, &optval, optlen);
|
||||
#elif _WIN32
|
||||
const char optVal = optval;
|
||||
return setsockopt(socket, level, optname, &optVal, optlen);
|
||||
#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
|
||||
}
|
18
common/cross_sockets/xsocket.h
Normal file
18
common/cross_sockets/xsocket.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifdef __linux
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <unistd.h>
|
||||
#elif _WIN32
|
||||
#include <WinSock2.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux
|
||||
const int TCP_SOCKET_LEVEL = SOL_TCP;
|
||||
#elif _WIN32
|
||||
const int TCP_SOCKET_LEVEL = IPPROTO_IP;
|
||||
#endif
|
||||
|
||||
void close_socket(int sock);
|
||||
int set_socket_option(int socket, int level, int optname, int optval, int optlen);
|
||||
int write_to_socket(int socket, const char* buf, int len);
|
||||
int read_from_socket(int socket, char* buf, int len);
|
Loading…
Reference in a new issue