Skip to content

Commit

Permalink
Make progress to Windows build of remote connection code
Browse files Browse the repository at this point in the history
  • Loading branch information
streetpea committed Apr 23, 2024
1 parent 2c4007c commit 6c01c7b
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-msys2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ jobs:
ffmpeg:p
fftw:p
hidapi:p
json-c:p
lcms2:p
libdovi:p
meson:p
miniupnpc:p
nasm:p
ninja:p
openssl:p
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ jobs:
-DCHIAKI_ENABLE_STEAM_SHORTCUT=OFF `
-DCHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER=ON `
-DCHIAKI_ENABLE_STEAMDECK_NATIVE=OFF `
-DCHIAKI_USE_SYSTEM_CURL `
-DPYTHON_EXECUTABLE="${{ env.pythonLocation }}\python.exe" `
-DCMAKE_PREFIX_PATH="${{ github.workspace }}\${{ env.dep_folder }};${{ env.VULKAN_SDK }}"
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ add_dependencies(chiaki-lib chiaki-pb)
set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki)

if(WIN32)
target_link_libraries(chiaki-lib wsock32 ws2_32 bcrypt)
target_link_libraries(chiaki-lib wsock32 ws2_32 bcrypt iphlpapi)
endif()

target_include_directories(chiaki-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
Expand Down
2 changes: 2 additions & 0 deletions lib/include/chiaki/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef SOCKET chiaki_socket_t;
#define CHIAKI_SOCKET_ERROR_FMT "%d"
#define CHIAKI_SOCKET_ERROR_VALUE (WSAGetLastError())
#define CHIAKI_SOCKET_EINPROGRESS (WSAGetLastError() == WSAEWOULDBLOCK)
#define SOCKET_BUF_TYPE (char*)
#else
#include <unistd.h>
#include <errno.h>
Expand All @@ -30,6 +31,7 @@ typedef int chiaki_socket_t;
#define CHIAKI_SOCKET_ERROR_FMT "%s"
#define CHIAKI_SOCKET_ERROR_VALUE (strerror(errno))
#define CHIAKI_SOCKET_EINPROGRESS (errno == EINPROGRESS)
#define SOCKET_BUF_TYPE (void *)
#endif

CHIAKI_EXPORT ChiakiErrorCode chiaki_socket_set_nonblock(chiaki_socket_t sock, bool nonblock);
Expand Down
144 changes: 102 additions & 42 deletions lib/src/remote/holepunch.c

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions lib/src/remote/rudp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <chiaki/remote/rudpsendbuffer.h>

#include <math.h>
#include <arpa/inet.h>
#include <string.h>
#include <assert.h>

Expand Down Expand Up @@ -337,7 +336,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_send_raw(RudpInstance *rudp, uint8_t *
{
return CHIAKI_ERR_DISCONNECTED;
}
int sent = send(rudp->sock, buf, buf_size, 0);
int sent = send(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0);
if(sent < 0)
{
#ifdef _WIN32
Expand All @@ -362,7 +361,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_recv(RudpInstance *rudp, size_t buf_si
return err;
}

int received_sz = recv(rudp->sock, buf, buf_size, 0);
int received_sz = recv(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0);
if(received_sz <= 8)
{
if(received_sz < 0)
Expand Down
1 change: 0 additions & 1 deletion lib/src/remote/rudpsendbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <arpa/inet.h>

#define RUDP_DATA_RESEND_TIMEOUT_MS 200
#define RUDP_DATA_RESEND_WAKEUP_TIMEOUT_MS (RUDP_DATA_RESEND_TIMEOUT_MS/2)
Expand Down
38 changes: 25 additions & 13 deletions lib/src/remote/stun.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef _WIN32
#include <ws2tcpip.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif

#include <chiaki/log.h>
#include <chiaki/sock.h>
Expand Down Expand Up @@ -88,7 +94,7 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se
{
chiaki_socket_t sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to create socket, error was '%s'", strerror(errno));
CHIAKI_LOGE(log, "remote/stun.h: Failed to create socket, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
return false;
}

Expand All @@ -99,8 +105,8 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to bind socket to local address, error was '%s'", strerror(errno));
close(sock);
CHIAKI_LOGE(log, "remote/stun.h: Failed to bind socket to local address, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

Expand All @@ -111,8 +117,8 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo(server->host, NULL, &hints, &resolved) != 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to resolve STUN server '%s', error was '%s'", server->host, strerror(errno));
close(sock);
CHIAKI_LOGE(log, "remote/stun.h: Failed to resolve STUN server '%s', error was " CHIAKI_SOCKET_ERROR_FMT, server->host, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

Expand All @@ -128,27 +134,33 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se

//uint8_t* transaction_id = &binding_req[8];

ssize_t sent = sendto(sock, binding_req, sizeof(binding_req), 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in));
ssize_t sent = sendto(sock, SOCKET_BUF_TYPE binding_req, sizeof(binding_req), 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in));
if (sent != sizeof(binding_req)) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to send STUN request, error was '%s'", strerror(errno));
close(sock);
CHIAKI_LOGE(log, "remote/stun.h: Failed to send STUN request, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

#ifdef _WIN32
char timeout[20] = {0};
sprintf(timeout, "%d", STUN_REPLY_TIMEOUT_SEC * 1000);
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, timeout, sizeof(timeout)) < 0) {
#else
struct timeval timeout;
timeout.tv_sec = STUN_REPLY_TIMEOUT_SEC;
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to set socket timeout, error was '%s'", strerror(errno));
close(sock);
#endif
CHIAKI_LOGE(log, "remote/stun.h: Failed to set socket timeout, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

uint8_t binding_resp[256];
ssize_t received = recvfrom(sock, binding_resp, sizeof(binding_resp), 0, NULL, NULL);
close(sock);
ssize_t received = recvfrom(sock, SOCKET_BUF_TYPE binding_resp, sizeof(binding_resp), 0, NULL, NULL);
CHIAKI_SOCKET_CLOSE(sock);
if (received < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to receive STUN response, error was '%s'", strerror(errno));
CHIAKI_LOGE(log, "remote/stun.h: Failed to receive STUN response, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static inline int sendto_broadcast(ChiakiLog *log, chiaki_socket_t s, const void
return r;
}
#endif
return sendto(s, msg, len, flags, to, tolen);
return sendto(s, SOCKET_BUF_TYPE msg, len, flags, to, tolen);
}

static inline void xor_bytes(uint8_t *dst, uint8_t *src, size_t sz)
Expand Down
11 changes: 10 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{
"name": "chiaki4deck",
"version": "1.6.3",
"version": "1.7.0",
"dependencies": [
"pkgconf",
{
"name": "curl",
"features": [
"ssl",
"websockets"
]
},
"sdl2",
"protobuf",
"openssl",
"hidapi",
"json-c",
"miniupnpc",
"opus",
"fftw3",
"lcms",
Expand Down

0 comments on commit 6c01c7b

Please sign in to comment.