mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-03-12 05:25:23 -07:00
Port to Windows
This commit is contained in:
parent
43ddf619ed
commit
5faa801331
.appveyor.yml.gitignore
gui
CMakeLists.txt
include
avopenglwidget.hdiscoverymanager.hexception.hhost.hregistdialog.hserveritemwidget.hsettings.hstreamsession.hvideodecoder.h
res
src
lib
scripts
test
third-party
15
.appveyor.yml
Normal file
15
.appveyor.yml
Normal file
@ -0,0 +1,15 @@
|
||||
image: 'Visual Studio 2017'
|
||||
|
||||
configuration:
|
||||
- Release
|
||||
|
||||
install:
|
||||
- git submodule update --init --recursive
|
||||
|
||||
build_script:
|
||||
- call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
|
||||
- C:\msys64\usr\bin\bash -lc "cd \"%APPVEYOR_BUILD_FOLDER%\" && scripts/appveyor.sh"
|
||||
|
||||
artifacts:
|
||||
- path: Chiaki
|
||||
name: Chiaki
|
7
.gitignore
vendored
7
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
*~
|
||||
*.swp
|
||||
.idea
|
||||
build
|
||||
@ -6,3 +7,9 @@ cmake-build-*
|
||||
*.AppImage
|
||||
appdir
|
||||
/ffmpeg*
|
||||
/Chiaki
|
||||
/SDL2-*
|
||||
/opus*
|
||||
/ffmpeg*
|
||||
/protoc*
|
||||
/openssl*
|
||||
|
@ -13,6 +13,10 @@ if(CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER)
|
||||
find_package(SDL2 MODULE REQUIRED)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
add_definitions(-DWIN32_LEAN_AND_MEAN)
|
||||
endif()
|
||||
|
||||
find_package(FFMPEG REQUIRED COMPONENTS avcodec avutil)
|
||||
|
||||
set(RESOURCE_FILES "")
|
||||
@ -21,7 +25,7 @@ if(APPLE)
|
||||
list(APPEND RESOURCE_FILES "chiaki.icns")
|
||||
endif()
|
||||
|
||||
add_executable(chiaki
|
||||
add_executable(chiaki WIN32
|
||||
${RESOURCE_FILES}
|
||||
include/exception.h
|
||||
src/main.cpp
|
||||
@ -93,4 +97,4 @@ install(TARGETS chiaki
|
||||
RUNTIME DESTINATION bin
|
||||
BUNDLE DESTINATION bin)
|
||||
install(FILES chiaki.desktop DESTINATION share/applications)
|
||||
install(FILES chiaki.png DESTINATION share/icons/hicolor/512x512)
|
||||
install(FILES chiaki.png DESTINATION share/icons/hicolor/512x512)
|
||||
|
@ -18,11 +18,11 @@
|
||||
#ifndef CHIAKI_AVOPENGLWIDGET_H
|
||||
#define CHIAKI_AVOPENGLWIDGET_H
|
||||
|
||||
#include <chiaki/log.h>
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
#include <QMutex>
|
||||
|
||||
#include <chiaki/log.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <libavcodec/avcodec.h>
|
||||
|
@ -18,13 +18,13 @@
|
||||
#ifndef CHIAKI_DISCOVERYMANAGER_H
|
||||
#define CHIAKI_DISCOVERYMANAGER_H
|
||||
|
||||
#include <chiaki/discoveryservice.h>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
|
||||
#include <chiaki/discoveryservice.h>
|
||||
|
||||
struct DiscoveryHost
|
||||
{
|
||||
ChiakiDiscoveryHostState state;
|
||||
|
@ -25,11 +25,11 @@
|
||||
class Exception : public std::exception
|
||||
{
|
||||
private:
|
||||
QString msg;
|
||||
QByteArray msg;
|
||||
|
||||
public:
|
||||
explicit Exception(const QString &msg) : msg(msg) {}
|
||||
const char *what() const noexcept override { return msg.toLocal8Bit().constData(); }
|
||||
explicit Exception(const QString &msg) : msg(msg.toLocal8Bit()) {}
|
||||
const char *what() const noexcept override { return msg.constData(); }
|
||||
};
|
||||
|
||||
#endif // CHIAKI_EXCEPTION_H
|
||||
|
@ -18,11 +18,11 @@
|
||||
#ifndef CHIAKI_HOST_H
|
||||
#define CHIAKI_HOST_H
|
||||
|
||||
#include <chiaki/regist.h>
|
||||
|
||||
#include <QMetaType>
|
||||
#include <QString>
|
||||
|
||||
#include <chiaki/regist.h>
|
||||
|
||||
class QSettings;
|
||||
|
||||
class HostMAC
|
||||
|
@ -18,10 +18,10 @@
|
||||
#ifndef CHIAKI_REGISTDIALOG_H
|
||||
#define CHIAKI_REGISTDIALOG_H
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#include <chiaki/regist.h>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
class Settings;
|
||||
|
@ -23,7 +23,7 @@
|
||||
class QLabel;
|
||||
|
||||
class ServerIconWidget;
|
||||
class DisplayServer;
|
||||
struct DisplayServer;
|
||||
|
||||
class ServerItemWidget : public QFrame
|
||||
{
|
||||
|
@ -18,10 +18,10 @@
|
||||
#ifndef CHIAKI_SETTINGS_H
|
||||
#define CHIAKI_SETTINGS_H
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#include <chiaki/session.h>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
class Settings : public QObject
|
||||
|
@ -18,6 +18,8 @@
|
||||
#ifndef CHIAKI_STREAMSESSION_H
|
||||
#define CHIAKI_STREAMSESSION_H
|
||||
|
||||
#include <chiaki/session.h>
|
||||
|
||||
#include "videodecoder.h"
|
||||
#include "exception.h"
|
||||
#include "sessionlog.h"
|
||||
@ -26,8 +28,6 @@
|
||||
#include <QObject>
|
||||
#include <QImage>
|
||||
|
||||
#include <chiaki/session.h>
|
||||
|
||||
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD
|
||||
class QGamepad;
|
||||
#endif
|
||||
|
@ -18,10 +18,10 @@
|
||||
#ifndef CHIAKI_VIDEODECODER_H
|
||||
#define CHIAKI_VIDEODECODER_H
|
||||
|
||||
#include "exception.h"
|
||||
|
||||
#include <chiaki/log.h>
|
||||
|
||||
#include "exception.h"
|
||||
|
||||
#include <QMutex>
|
||||
#include <QObject>
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
<RCC>
|
||||
<qresource prefix="icons">
|
||||
<qresource prefix="/icons">
|
||||
<file>settings-20px.svg</file>
|
||||
<file>add-24px.svg</file>
|
||||
<file>discover-24px.svg</file>
|
||||
|
@ -19,7 +19,15 @@
|
||||
#include <exception.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define PING_MS 500
|
||||
#define HOSTS_MAX 16
|
||||
@ -101,8 +109,8 @@ void DiscoveryManager::SendWakeup(const QString &host, const QByteArray ®ist_
|
||||
{
|
||||
if(ai->ai_family != AF_INET)
|
||||
continue;
|
||||
if(ai->ai_protocol != IPPROTO_UDP)
|
||||
continue;
|
||||
//if(ai->ai_protocol != IPPROTO_UDP)
|
||||
// continue;
|
||||
if(ai->ai_addrlen > sizeof(addr))
|
||||
continue;
|
||||
std::memcpy(&addr, ai->ai_addr, ai->ai_addrlen);
|
||||
|
@ -1,4 +1,8 @@
|
||||
|
||||
// ugly workaround because Windows does weird things and ENOTIME
|
||||
int real_main(int argc, char *argv[]);
|
||||
int main(int argc, char *argv[]) { return real_main(argc, argv); }
|
||||
|
||||
#include <streamwindow.h>
|
||||
#include <videodecoder.h>
|
||||
#include <mainwindow.h>
|
||||
@ -43,7 +47,7 @@ static const QMap<QString, CLICommand> cli_commands = {
|
||||
int RunStream(QApplication &app, const StreamSessionConnectInfo &connect_info);
|
||||
int RunMain(QApplication &app, Settings *settings);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int real_main(int argc, char *argv[])
|
||||
{
|
||||
qRegisterMetaType<DiscoveryHost>();
|
||||
qRegisterMetaType<RegisteredHost>();
|
||||
@ -67,6 +71,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
Q_INIT_RESOURCE(resources);
|
||||
|
||||
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
||||
|
||||
Settings settings;
|
||||
|
@ -327,4 +327,4 @@ void MainWindow::UpdateServerWidgets()
|
||||
|
||||
for(size_t i=0; i<server_item_widgets.count(); i++)
|
||||
server_item_widgets[i]->Update(display_servers[i]);
|
||||
}
|
||||
}
|
||||
|
@ -89,8 +89,6 @@ send_packet:
|
||||
emit FramesAvailable();
|
||||
}
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
AVFrame *VideoDecoder::PullFrame()
|
||||
{
|
||||
QMutexLocker locker(&mutex);
|
||||
|
@ -81,6 +81,10 @@ add_library(chiaki-lib ${HEADER_FILES} ${SOURCE_FILES} ${CHIAKI_LIB_PROTO_SOURCE
|
||||
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)
|
||||
endif()
|
||||
|
||||
target_include_directories(chiaki-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
|
||||
find_package(Threads)
|
||||
@ -92,4 +96,4 @@ target_link_libraries(chiaki-lib OpenSSL::Crypto)
|
||||
target_link_libraries(chiaki-lib protobuf-nanopb-static)
|
||||
target_link_libraries(chiaki-lib jerasure)
|
||||
|
||||
target_link_libraries(chiaki-lib ${Opus_LIBRARIES})
|
||||
target_link_libraries(chiaki-lib ${Opus_LIBRARIES})
|
||||
|
@ -19,7 +19,9 @@
|
||||
#define CHIAKI_AUDIO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
@ -18,7 +18,22 @@
|
||||
#ifndef CHIAKI_COMMON_H
|
||||
#define CHIAKI_COMMON_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#ifdef _WIN32
|
||||
#define chiaki_socket_t SOCKET
|
||||
#define CHIAKI_SOCKET_IS_INVALID(s) ((s) == INVALID_SOCKET)
|
||||
#define CHIAKI_INVALID_SOCKET INVALID_SOCKET
|
||||
#define CHIAKI_SOCKET_CLOSE(s) closesocket(s)
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#define chiaki_socket_t int
|
||||
#define CHIAKI_SOCKET_IS_INVALID(s) ((s) < 0)
|
||||
#define CHIAKI_INVALID_SOCKET (-1)
|
||||
#define CHIAKI_SOCKET_CLOSE(s) close(s)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -52,6 +67,7 @@ typedef enum
|
||||
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);
|
||||
|
||||
CHIAKI_EXPORT void *chiaki_aligned_alloc(size_t alignment, size_t size);
|
||||
CHIAKI_EXPORT void chiaki_aligned_free(void *ptr);
|
||||
|
||||
/**
|
||||
* Perform initialization of global state needed for using the Chiaki lib
|
||||
|
@ -25,6 +25,10 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -34,7 +38,7 @@ typedef struct chiaki_ctrl_t
|
||||
struct chiaki_session_t *session;
|
||||
ChiakiThread thread;
|
||||
ChiakiStopPipe stop_pipe;
|
||||
int sock;
|
||||
chiaki_socket_t sock;
|
||||
uint8_t recv_buf[512];
|
||||
size_t recv_buf_size;
|
||||
uint64_t crypt_counter_remote;
|
||||
|
@ -23,8 +23,14 @@
|
||||
#include "stoppipe.h"
|
||||
#include "log.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
typedef unsigned short sa_family_t;
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -73,7 +79,7 @@ typedef struct chiaki_discovery_host_t
|
||||
// All string members here must be in sync with CHIAKI_DISCOVERY_HOST_STRING_FOREACH
|
||||
ChiakiDiscoveryHostState state;
|
||||
uint16_t host_request_port;
|
||||
#define STRING_MEMBER(name) const char *name;
|
||||
#define STRING_MEMBER(name) const char *name
|
||||
CHIAKI_DISCOVERY_HOST_STRING_FOREACH(STRING_MEMBER)
|
||||
#undef STRING_MEMBER
|
||||
} ChiakiDiscoveryHost;
|
||||
@ -84,7 +90,7 @@ CHIAKI_EXPORT int chiaki_discovery_packet_fmt(char *buf, size_t buf_size, Chiaki
|
||||
typedef struct chiaki_discovery_t
|
||||
{
|
||||
ChiakiLog *log;
|
||||
int socket;
|
||||
chiaki_socket_t socket;
|
||||
struct sockaddr local_addr;
|
||||
} ChiakiDiscovery;
|
||||
|
||||
|
@ -21,7 +21,9 @@
|
||||
#include "common.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -90,7 +90,7 @@ static inline void chiaki_reorder_queue_set_drop_cb(ChiakiReorderQueue *queue, C
|
||||
|
||||
static inline size_t chiaki_reorder_queue_size(ChiakiReorderQueue *queue)
|
||||
{
|
||||
return 1 << queue->size_exp;
|
||||
return ((size_t)1) << queue->size_exp;
|
||||
}
|
||||
|
||||
static inline uint64_t chiaki_reorder_queue_count(ChiakiReorderQueue *queue)
|
||||
|
@ -33,7 +33,6 @@
|
||||
#include "stoppipe.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -22,7 +22,10 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -30,14 +33,18 @@ extern "C" {
|
||||
|
||||
typedef struct chiaki_stop_pipe_t
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSAEVENT event;
|
||||
#else
|
||||
int fds[2];
|
||||
#endif
|
||||
} ChiakiStopPipe;
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_init(ChiakiStopPipe *stop_pipe);
|
||||
CHIAKI_EXPORT void chiaki_stop_pipe_fini(ChiakiStopPipe *stop_pipe);
|
||||
CHIAKI_EXPORT void chiaki_stop_pipe_stop(ChiakiStopPipe *stop_pipe);
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *stop_pipe, int fd, uint64_t timeout_ms);
|
||||
static inline ChiakiErrorCode chiaki_stop_pipe_sleep(ChiakiStopPipe *stop_pipe, uint64_t timeout_ms) { return chiaki_stop_pipe_select_single(stop_pipe, -1, timeout_ms); }
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *stop_pipe, chiaki_socket_t fd, uint64_t timeout_ms);
|
||||
static inline ChiakiErrorCode chiaki_stop_pipe_sleep(ChiakiStopPipe *stop_pipe, uint64_t timeout_ms) { return chiaki_stop_pipe_select_single(stop_pipe, CHIAKI_INVALID_SOCKET, timeout_ms); }
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -28,9 +28,12 @@
|
||||
#include "feedback.h"
|
||||
#include "takionsendbuffer.h"
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -112,7 +115,7 @@ typedef struct chiaki_takion_connect_info_t
|
||||
{
|
||||
ChiakiLog *log;
|
||||
struct sockaddr *sa;
|
||||
socklen_t sa_len;
|
||||
size_t sa_len;
|
||||
ChiakiTakionCallback cb;
|
||||
void *cb_user;
|
||||
bool enable_crypt;
|
||||
@ -154,7 +157,7 @@ typedef struct chiaki_takion_t
|
||||
|
||||
ChiakiTakionCallback cb;
|
||||
void *cb_user;
|
||||
int sock;
|
||||
chiaki_socket_t sock;
|
||||
ChiakiThread thread;
|
||||
ChiakiStopPipe stop_pipe;
|
||||
uint32_t tag_local;
|
||||
|
@ -26,15 +26,27 @@ extern "C" {
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
typedef void *(*ChiakiThreadFunc)(void *);
|
||||
|
||||
typedef struct chiaki_thread_t
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HANDLE thread;
|
||||
ChiakiThreadFunc func;
|
||||
void *arg;
|
||||
void *ret;
|
||||
#else
|
||||
pthread_t thread;
|
||||
#endif
|
||||
} ChiakiThread;
|
||||
|
||||
typedef void *(*ChiakiThreadFunc)(void *);
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_create(ChiakiThread *thread, ChiakiThreadFunc func, void *arg);
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_join(ChiakiThread *thread, void **retval);
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_set_name(ChiakiThread *thread, const char *name);
|
||||
@ -42,7 +54,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_set_name(ChiakiThread *thread, const
|
||||
|
||||
typedef struct chiaki_mutex_t
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CRITICAL_SECTION cs;
|
||||
#else
|
||||
pthread_mutex_t mutex;
|
||||
#endif
|
||||
} ChiakiMutex;
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex, bool rec);
|
||||
@ -54,7 +70,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_unlock(ChiakiMutex *mutex);
|
||||
|
||||
typedef struct chiaki_cond_t
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CONDITION_VARIABLE cond;
|
||||
#else
|
||||
pthread_cond_t cond;
|
||||
#endif
|
||||
} ChiakiCond;
|
||||
|
||||
typedef bool (*ChiakiCheckPred)(void *);
|
||||
|
@ -1,8 +1,11 @@
|
||||
|
||||
find_package(Protobuf REQUIRED)
|
||||
find_program(PROTOC protoc)
|
||||
if(NOT PROTOC)
|
||||
message(FATAL_ERROR "Could not find protoc")
|
||||
endif()
|
||||
|
||||
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/takion.pb"
|
||||
COMMAND protobuf::protoc "-o${CMAKE_CURRENT_BINARY_DIR}/takion.pb" "${CMAKE_CURRENT_SOURCE_DIR}/takion.proto" "-I${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
COMMAND "${PROTOC}" "-o${CMAKE_CURRENT_BINARY_DIR}/takion.pb" "${CMAKE_CURRENT_SOURCE_DIR}/takion.proto" "-I${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/takion.proto")
|
||||
|
||||
set(SOURCE_FILES "${CMAKE_CURRENT_BINARY_DIR}/takion.pb.c")
|
||||
@ -16,4 +19,4 @@ set(CHIAKI_LIB_PROTO_SOURCE_FILES "${SOURCE_FILES}" PARENT_SCOPE)
|
||||
set(CHIAKI_LIB_PROTO_HEADER_FILES "${HEADER_FILES}" PARENT_SCOPE)
|
||||
set(CHIAKI_LIB_PROTO_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE)
|
||||
|
||||
add_custom_target(chiaki-pb DEPENDS ${SOURCE_FILES} ${HEADER_FILES})
|
||||
add_custom_target(chiaki-pb DEPENDS ${SOURCE_FILES} ${HEADER_FILES})
|
||||
|
@ -17,7 +17,11 @@
|
||||
|
||||
#include <chiaki/audio.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
|
||||
void chiaki_audio_header_load(ChiakiAudioHeader *audio_header, const uint8_t *buf)
|
||||
@ -36,4 +40,4 @@ void chiaki_audio_header_save(ChiakiAudioHeader *audio_header, uint8_t *buf)
|
||||
*((uint32_t *)(buf + 2)) = htonl(audio_header->rate);
|
||||
*((uint32_t *)(buf + 6)) = htonl(audio_header->frame_size);
|
||||
*((uint32_t *)(buf + 0xa)) = htonl(audio_header->unknown);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,10 @@
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
|
||||
{
|
||||
switch(code)
|
||||
@ -64,9 +68,11 @@ CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
|
||||
}
|
||||
}
|
||||
|
||||
void *chiaki_aligned_alloc(size_t alignment, size_t size)
|
||||
CHIAKI_EXPORT void *chiaki_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
#if __APPLE__
|
||||
#if defined(_WIN32)
|
||||
return _aligned_malloc(size, alignment);
|
||||
#elif __APPLE__
|
||||
void *r;
|
||||
if(posix_memalign(&r, alignment, size) == 0)
|
||||
return r;
|
||||
@ -77,6 +83,15 @@ void *chiaki_aligned_alloc(size_t alignment, size_t size)
|
||||
#endif
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT void chiaki_aligned_free(void *ptr)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
_aligned_free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_lib_init()
|
||||
{
|
||||
unsigned int seed;
|
||||
@ -87,5 +102,15 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_lib_init()
|
||||
if(galois_r != 0)
|
||||
return galois_r == ENOMEM ? CHIAKI_ERR_MEMORY : CHIAKI_ERR_UNKNOWN;
|
||||
|
||||
#if _WIN32
|
||||
{
|
||||
WORD wsa_version = MAKEWORD(2, 2);
|
||||
WSADATA wsa_data;
|
||||
int err = WSAStartup(wsa_version, &wsa_data);
|
||||
if (err != 0)
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
}
|
||||
#endif
|
||||
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
@ -22,11 +22,20 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define SESSION_OSTYPE "Win10.0.0"
|
||||
|
||||
@ -139,7 +148,7 @@ static void *ctrl_thread_func(void *user)
|
||||
break;
|
||||
}
|
||||
|
||||
ssize_t received = recv(ctrl->sock, ctrl->recv_buf + ctrl->recv_buf_size, sizeof(ctrl->recv_buf) - ctrl->recv_buf_size, 0);
|
||||
int received = recv(ctrl->sock, ctrl->recv_buf + ctrl->recv_buf_size, sizeof(ctrl->recv_buf) - ctrl->recv_buf_size, 0);
|
||||
if(received <= 0)
|
||||
{
|
||||
if(received < 0)
|
||||
@ -150,7 +159,7 @@ static void *ctrl_thread_func(void *user)
|
||||
ctrl->recv_buf_size += received;
|
||||
}
|
||||
|
||||
close(ctrl->sock);
|
||||
CHIAKI_SOCKET_CLOSE(ctrl->sock);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@ -164,7 +173,7 @@ static ChiakiErrorCode ctrl_message_send(ChiakiCtrl *ctrl, CtrlMessageType type,
|
||||
*((uint16_t *)(header + 4)) = htons(type);
|
||||
*((uint16_t *)(header + 6)) = 0;
|
||||
|
||||
ssize_t sent = send(ctrl->sock, header, sizeof(header), 0);
|
||||
int sent = send(ctrl->sock, header, sizeof(header), 0);
|
||||
if(sent < 0)
|
||||
{
|
||||
CHIAKI_LOGE(ctrl->session->log, "Failed to send Ctrl Message Header");
|
||||
@ -321,7 +330,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
|
||||
else
|
||||
return CHIAKI_ERR_INVALID_DATA;
|
||||
|
||||
int sock = socket(sa->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
chiaki_socket_t sock = socket(sa->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(sock < 0)
|
||||
{
|
||||
CHIAKI_LOGE(session->log, "Session ctrl socket creation failed.");
|
||||
@ -336,7 +345,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
|
||||
int errsv = errno;
|
||||
CHIAKI_LOGE(session->log, "Ctrl connect failed: %s", strerror(errsv));
|
||||
ctrl_failed(ctrl, errsv == ECONNREFUSED ? CHIAKI_QUIT_REASON_CTRL_CONNECTION_REFUSED : CHIAKI_QUIT_REASON_CTRL_UNKNOWN);
|
||||
close(sock);
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
}
|
||||
|
||||
@ -396,7 +405,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
|
||||
|
||||
CHIAKI_LOGI(session->log, "Sending ctrl request");
|
||||
|
||||
ssize_t sent = send(sock, buf, (size_t)request_len, 0);
|
||||
int sent = send(sock, buf, (size_t)request_len, 0);
|
||||
if(sent < 0)
|
||||
{
|
||||
CHIAKI_LOGE(session->log, "Failed to send ctrl request");
|
||||
@ -454,6 +463,6 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
|
||||
error:
|
||||
close(sock);
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
@ -22,12 +22,17 @@
|
||||
#include <chiaki/log.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
const char *chiaki_discovery_host_state_string(ChiakiDiscoveryHostState state)
|
||||
{
|
||||
@ -123,7 +128,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
|
||||
discovery->log = log;
|
||||
|
||||
discovery->socket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if(discovery->socket < 0)
|
||||
if(CHIAKI_SOCKET_IS_INVALID(discovery->socket))
|
||||
{
|
||||
CHIAKI_LOGE(discovery->log, "Discovery failed to create socket");
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
@ -149,7 +154,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
|
||||
if(r < 0)
|
||||
{
|
||||
CHIAKI_LOGE(discovery->log, "Discovery failed to bind");
|
||||
close(discovery->socket);
|
||||
CHIAKI_SOCKET_CLOSE(discovery->socket);
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
}
|
||||
|
||||
@ -163,7 +168,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_init(ChiakiDiscovery *discovery,
|
||||
|
||||
CHIAKI_EXPORT void chiaki_discovery_fini(ChiakiDiscovery *discovery)
|
||||
{
|
||||
close(discovery->socket);
|
||||
CHIAKI_SOCKET_CLOSE(discovery->socket);
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery, ChiakiDiscoveryPacket *packet, struct sockaddr *addr, size_t addr_size)
|
||||
@ -178,7 +183,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_discovery_send(ChiakiDiscovery *discovery,
|
||||
if((size_t)len >= sizeof(buf))
|
||||
return CHIAKI_ERR_BUF_TOO_SMALL;
|
||||
|
||||
ssize_t rc = sendto(discovery->socket, buf, (size_t)len + 1, 0, addr, addr_size);
|
||||
int rc = sendto(discovery->socket, buf, (size_t)len + 1, 0, addr, addr_size);
|
||||
if(rc < 0)
|
||||
{
|
||||
CHIAKI_LOGE(discovery->log, "Discovery failed to send: %s", strerror(errno));
|
||||
@ -245,7 +250,7 @@ static void *discovery_thread_func(void *user)
|
||||
char buf[512];
|
||||
struct sockaddr client_addr;
|
||||
socklen_t client_addr_size = sizeof(client_addr);
|
||||
ssize_t n = recvfrom(discovery->socket, buf, sizeof(buf) - 1, 0, &client_addr, &client_addr_size);
|
||||
int n = recvfrom(discovery->socket, buf, sizeof(buf) - 1, 0, &client_addr, &client_addr_size);
|
||||
if(n < 0)
|
||||
{
|
||||
CHIAKI_LOGE(discovery->log, "Discovery thread failed to read from socket");
|
||||
@ -277,4 +282,4 @@ static void *discovery_thread_func(void *user)
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/bn.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@ -128,4 +129,4 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_ecdh_derive_secret(ChiakiECDH *ecdh, uint8_
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,11 @@
|
||||
#include <chiaki/feedback.h>
|
||||
#include <chiaki/controller.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
|
||||
CHIAKI_EXPORT void chiaki_feedback_state_format(uint8_t *buf, ChiakiFeedbackState *state)
|
||||
@ -156,4 +160,4 @@ CHIAKI_EXPORT void chiaki_feedback_history_buffer_push(ChiakiFeedbackHistoryBuff
|
||||
if(feedback_history_buffer->len >= feedback_history_buffer->size)
|
||||
feedback_history_buffer->len = feedback_history_buffer->size;
|
||||
feedback_history_buffer->events[feedback_history_buffer->begin] = *event;
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,10 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#define UNIT_SLOTS_MAX 256
|
||||
|
||||
|
||||
|
@ -99,7 +99,7 @@ error_key_buf_mutex:
|
||||
if(gkcrypt->key_buf)
|
||||
chiaki_mutex_fini(&gkcrypt->key_buf_mutex);
|
||||
error_key_buf:
|
||||
free(gkcrypt->key_buf);
|
||||
chiaki_aligned_free(gkcrypt->key_buf);
|
||||
error:
|
||||
return err;
|
||||
}
|
||||
@ -113,7 +113,7 @@ CHIAKI_EXPORT void chiaki_gkcrypt_fini(ChiakiGKCrypt *gkcrypt)
|
||||
chiaki_mutex_unlock(&gkcrypt->key_buf_mutex);
|
||||
chiaki_cond_signal(&gkcrypt->key_buf_cond);
|
||||
chiaki_thread_join(&gkcrypt->key_buf_thread, NULL);
|
||||
free(gkcrypt->key_buf);
|
||||
chiaki_aligned_free(gkcrypt->key_buf);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,12 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#if _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
|
||||
CHIAKI_EXPORT void chiaki_http_header_free(ChiakiHttpHeader *header)
|
||||
@ -157,7 +162,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_recv_http_header(int sock, char *buf, size_
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t received = recv(sock, buf, buf_size, 0);
|
||||
int received = (int)recv(sock, buf, (int)buf_size, 0);
|
||||
if(received <= 0)
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
|
||||
@ -191,4 +196,4 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_recv_http_header(int sock, char *buf, size_
|
||||
}
|
||||
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,13 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
typedef uint32_t in_addr_t;
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#define REGIST_PORT 9295
|
||||
|
||||
@ -37,9 +43,9 @@
|
||||
|
||||
static void *regist_thread_func(void *user);
|
||||
static ChiakiErrorCode regist_search(ChiakiRegist *regist, struct addrinfo *addrinfos, struct sockaddr *recv_addr, socklen_t *recv_addr_size);
|
||||
static int regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfos, struct sockaddr *send_addr, socklen_t *send_addr_len);
|
||||
static int regist_request_connect(ChiakiRegist *regist, const struct sockaddr *addr, size_t addr_len);
|
||||
static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegisteredHost *host, int sock, ChiakiRPCrypt *rpcrypt);
|
||||
static chiaki_socket_t regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfos, struct sockaddr *send_addr, socklen_t *send_addr_len);
|
||||
static chiaki_socket_t regist_request_connect(ChiakiRegist *regist, const struct sockaddr *addr, size_t addr_len);
|
||||
static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegisteredHost *host, chiaki_socket_t sock, ChiakiRPCrypt *rpcrypt);
|
||||
static ChiakiErrorCode regist_parse_response_payload(ChiakiRegist *regist, ChiakiRegisteredHost *host, char *buf, size_t buf_size);
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_regist_start(ChiakiRegist *regist, ChiakiLog *log, const ChiakiRegistInfo *info, ChiakiRegistCb cb, void *cb_user)
|
||||
@ -164,7 +170,7 @@ static void *regist_thread_func(void *user)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
struct sockaddr recv_addr = {};
|
||||
struct sockaddr recv_addr = { 0 };
|
||||
socklen_t recv_addr_size;
|
||||
recv_addr_size = sizeof(recv_addr);
|
||||
err = regist_search(regist, addrinfos, &recv_addr, &recv_addr_size);
|
||||
@ -184,8 +190,8 @@ static void *regist_thread_func(void *user)
|
||||
goto fail_addrinfos;
|
||||
}
|
||||
|
||||
int sock = regist_request_connect(regist, &recv_addr, recv_addr_size);
|
||||
if(sock < 0)
|
||||
chiaki_socket_t sock = regist_request_connect(regist, &recv_addr, recv_addr_size);
|
||||
if(CHIAKI_SOCKET_IS_INVALID(sock))
|
||||
{
|
||||
CHIAKI_LOGE(regist->log, "Regist eventually failed to connect for request");
|
||||
goto fail_addrinfos;
|
||||
@ -196,14 +202,22 @@ static void *regist_thread_func(void *user)
|
||||
int s = send(sock, request_header, request_header_size, 0);
|
||||
if(s < 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to send request header: %u", WSAGetLastError());
|
||||
#else
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to send request header: %s", strerror(errno));
|
||||
#endif
|
||||
goto fail_socket;
|
||||
}
|
||||
|
||||
s = send(sock, payload, payload_size, 0);
|
||||
if(s < 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to send payload: %u", WSAGetLastError());
|
||||
#else
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to send payload: %s", strerror(errno));
|
||||
#endif
|
||||
goto fail_socket;
|
||||
}
|
||||
|
||||
@ -225,7 +239,7 @@ static void *regist_thread_func(void *user)
|
||||
success = true;
|
||||
|
||||
fail_socket:
|
||||
close(sock);
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
fail_addrinfos:
|
||||
freeaddrinfo(addrinfos);
|
||||
fail:
|
||||
@ -251,8 +265,8 @@ static ChiakiErrorCode regist_search(ChiakiRegist *regist, struct addrinfo *addr
|
||||
CHIAKI_LOGI(regist->log, "Regist starting search");
|
||||
struct sockaddr send_addr;
|
||||
socklen_t send_addr_len = sizeof(send_addr);
|
||||
int sock = regist_search_connect(regist, addrinfos, &send_addr, &send_addr_len);
|
||||
if(sock < 0)
|
||||
chiaki_socket_t sock = regist_search_connect(regist, addrinfos, &send_addr, &send_addr_len);
|
||||
if(CHIAKI_SOCKET_IS_INVALID(sock))
|
||||
{
|
||||
CHIAKI_LOGE(regist->log, "Regist eventually failed to connect for search");
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
@ -285,7 +299,7 @@ static ChiakiErrorCode regist_search(ChiakiRegist *regist, struct addrinfo *addr
|
||||
}
|
||||
|
||||
uint8_t buf[0x100];
|
||||
ssize_t n = recvfrom(sock, buf, sizeof(buf) - 1, 0, recv_addr, recv_addr_size);
|
||||
int n = recvfrom(sock, buf, sizeof(buf) - 1, 0, recv_addr, recv_addr_size);
|
||||
if(n <= 0)
|
||||
{
|
||||
if(n < 0)
|
||||
@ -309,17 +323,17 @@ static ChiakiErrorCode regist_search(ChiakiRegist *regist, struct addrinfo *addr
|
||||
}
|
||||
|
||||
done:
|
||||
close(sock);
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfos, struct sockaddr *send_addr, socklen_t *send_addr_len)
|
||||
static chiaki_socket_t regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfos, struct sockaddr *send_addr, socklen_t *send_addr_len)
|
||||
{
|
||||
int sock = -1;
|
||||
chiaki_socket_t sock = CHIAKI_INVALID_SOCKET;
|
||||
for(struct addrinfo *ai=addrinfos; ai; ai=ai->ai_next)
|
||||
{
|
||||
if(ai->ai_protocol != IPPROTO_UDP)
|
||||
continue;
|
||||
//if(ai->ai_protocol != IPPROTO_UDP)
|
||||
// continue;
|
||||
|
||||
if(ai->ai_addr->sa_family != AF_INET) // TODO: support IPv6
|
||||
continue;
|
||||
@ -331,17 +345,24 @@ static int regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfo
|
||||
|
||||
set_port(send_addr, htons(REGIST_PORT));
|
||||
|
||||
sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
||||
if(sock < 0)
|
||||
sock = socket(ai->ai_family, SOCK_DGRAM, 0);
|
||||
if(CHIAKI_SOCKET_IS_INVALID(sock))
|
||||
{
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to create socket for search");
|
||||
continue;
|
||||
}
|
||||
|
||||
if(regist->info.broadcast)
|
||||
{
|
||||
const int broadcast = 1;
|
||||
int r = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
||||
int r = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast));
|
||||
if(r < 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to setsockopt SO_BROADCAST, error %u", WSAGetLastError());
|
||||
#else
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to setsockopt SO_BROADCAST");
|
||||
#endif
|
||||
goto connect_fail;
|
||||
}
|
||||
|
||||
@ -360,41 +381,50 @@ static int regist_search_connect(ChiakiRegist *regist, struct addrinfo *addrinfo
|
||||
int r = connect(sock, send_addr, *send_addr_len);
|
||||
if(r < 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CHIAKI_LOGE(regist->log, "Regist connect failed, error %u", WSAGetLastError());
|
||||
#else
|
||||
int errsv = errno;
|
||||
CHIAKI_LOGE(regist->log, "Regist connect failed: %s", strerror(errsv));
|
||||
#endif
|
||||
goto connect_fail;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
connect_fail:
|
||||
close(sock);
|
||||
sock = -1;
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
sock = CHIAKI_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
static int regist_request_connect(ChiakiRegist *regist, const struct sockaddr *addr, size_t addr_len)
|
||||
static chiaki_socket_t regist_request_connect(ChiakiRegist *regist, const struct sockaddr *addr, size_t addr_len)
|
||||
{
|
||||
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(sock < 0)
|
||||
chiaki_socket_t sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(CHIAKI_SOCKET_IS_INVALID(sock))
|
||||
{
|
||||
return -1;
|
||||
return CHIAKI_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
int r = connect(sock, addr, addr_len);
|
||||
if(r < 0)
|
||||
{
|
||||
int errsv = errno;
|
||||
#ifdef _WIN32
|
||||
CHIAKI_LOGE(regist->log, "Regist connect failed: %u", WSAGetLastError());
|
||||
#else
|
||||
CHIAKI_LOGE(regist->log, "Regist connect failed: %s", strerror(errsv));
|
||||
close(sock);
|
||||
#endif
|
||||
CHIAKI_SOCKET_CLOSE(sock);
|
||||
sock = CHIAKI_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegisteredHost *host, int sock, ChiakiRPCrypt *rpcrypt)
|
||||
static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegisteredHost *host, chiaki_socket_t sock, ChiakiRPCrypt *rpcrypt)
|
||||
{
|
||||
uint8_t buf[0x200];
|
||||
size_t buf_filled_size;
|
||||
@ -459,7 +489,7 @@ static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegister
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t received = recv(sock, buf + buf_filled_size, (content_size + header_size) - buf_filled_size, 0);
|
||||
int received = recv(sock, buf + buf_filled_size, (content_size + header_size) - buf_filled_size, 0);
|
||||
if(received <= 0)
|
||||
{
|
||||
CHIAKI_LOGE(regist->log, "Regist failed to receive response content");
|
||||
|
@ -32,6 +32,12 @@
|
||||
#include <pb.h>
|
||||
#include <chiaki/takion.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define SENKUSHA_PORT 9297
|
||||
|
||||
|
@ -25,13 +25,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
@ -411,6 +415,8 @@ typedef struct session_response_t {
|
||||
bool success;
|
||||
} SessionResponse;
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static void parse_session_response(SessionResponse *response, ChiakiHttpResponse *http_response)
|
||||
{
|
||||
memset(response, 0, sizeof(SessionResponse));
|
||||
@ -431,7 +437,9 @@ static void parse_session_response(SessionResponse *response, ChiakiHttpResponse
|
||||
for(ChiakiHttpHeader *header=http_response->headers; header; header=header->next)
|
||||
{
|
||||
if(strcmp(header->key, "RP-Application-Reason") == 0)
|
||||
response->error_code = (uint32_t)strtol(header->value, NULL, 0x10);
|
||||
{
|
||||
response->error_code = (uint32_t)strtoul(header->value, NULL, 0x10);
|
||||
}
|
||||
}
|
||||
response->success = false;
|
||||
}
|
||||
@ -440,11 +448,11 @@ static void parse_session_response(SessionResponse *response, ChiakiHttpResponse
|
||||
|
||||
static bool session_thread_request_session(ChiakiSession *session)
|
||||
{
|
||||
int session_sock = -1;
|
||||
chiaki_socket_t session_sock = CHIAKI_INVALID_SOCKET;
|
||||
for(struct addrinfo *ai=session->connect_info.host_addrinfos; ai; ai=ai->ai_next)
|
||||
{
|
||||
if(ai->ai_protocol != IPPROTO_TCP)
|
||||
continue;
|
||||
//if(ai->ai_protocol != IPPROTO_TCP)
|
||||
// continue;
|
||||
|
||||
struct sockaddr *sa = malloc(ai->ai_addrlen);
|
||||
if(!sa)
|
||||
@ -460,7 +468,7 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
set_port(sa, htons(SESSION_PORT));
|
||||
|
||||
// TODO: this can block, make cancelable somehow
|
||||
int r = getnameinfo(sa, ai->ai_addrlen, session->connect_info.hostname, sizeof(session->connect_info.hostname), NULL, 0, 0);
|
||||
int r = getnameinfo(sa, (socklen_t)ai->ai_addrlen, session->connect_info.hostname, sizeof(session->connect_info.hostname), NULL, 0, 0);
|
||||
if(r != 0)
|
||||
{
|
||||
free(sa);
|
||||
@ -469,8 +477,8 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
|
||||
CHIAKI_LOGI(session->log, "Trying to request session from %s:%d", session->connect_info.hostname, SESSION_PORT);
|
||||
|
||||
session_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
|
||||
if(session_sock < 0)
|
||||
session_sock = socket(ai->ai_family, SOCK_STREAM, 0);
|
||||
if(CHIAKI_SOCKET_IS_INVALID(session_sock))
|
||||
{
|
||||
free(sa);
|
||||
continue;
|
||||
@ -478,13 +486,22 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
r = connect(session_sock, sa, ai->ai_addrlen);
|
||||
if(r < 0)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
int err = WSAGetLastError();
|
||||
CHIAKI_LOGE(session->log, "Session request connect failed: %u", err);
|
||||
if(err == WSAECONNREFUSED)
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_CONNECTION_REFUSED;
|
||||
else
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_NONE;
|
||||
#else
|
||||
int errsv = errno;
|
||||
CHIAKI_LOGE(session->log, "Session request connect failed: %s", strerror(errsv));
|
||||
if(errsv == ECONNREFUSED)
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_CONNECTION_REFUSED;
|
||||
else
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_NONE;
|
||||
close(session_sock);
|
||||
#endif
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
session_sock = -1;
|
||||
free(sa);
|
||||
continue;
|
||||
@ -496,7 +513,7 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
}
|
||||
|
||||
|
||||
if(session_sock < 0)
|
||||
if(CHIAKI_SOCKET_IS_INVALID(session_sock))
|
||||
{
|
||||
CHIAKI_LOGE(session->log, "Session request connect failed eventually.");
|
||||
if(session->quit_reason == CHIAKI_QUIT_REASON_NONE)
|
||||
@ -529,7 +546,7 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
ChiakiErrorCode err = format_hex(regist_key_hex, sizeof(regist_key_hex), (uint8_t *)session->connect_info.regist_key, regist_key_len);
|
||||
if(err != CHIAKI_ERR_SUCCESS)
|
||||
{
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
@ -539,18 +556,18 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
session->connect_info.hostname, SESSION_PORT, regist_key_hex);
|
||||
if(request_len < 0 || request_len >= sizeof(buf))
|
||||
{
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
|
||||
CHIAKI_LOGI(session->log, "Sending session request");
|
||||
|
||||
ssize_t sent = send(session_sock, buf, (size_t)request_len, 0);
|
||||
int sent = send(session_sock, buf, (size_t)request_len, 0);
|
||||
if(sent < 0)
|
||||
{
|
||||
CHIAKI_LOGE(session->log, "Failed to send session request");
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
@ -572,16 +589,18 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
CHIAKI_LOGE(session->log, "Failed to receive session request response");
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
|
||||
}
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
return false;
|
||||
}
|
||||
|
||||
ChiakiHttpResponse http_response;
|
||||
CHIAKI_LOGV(session->log, "Session Response Header:");
|
||||
chiaki_log_hexdump(session->log, CHIAKI_LOG_VERBOSE, buf, header_size);
|
||||
err = chiaki_http_response_parse(&http_response, buf, header_size);
|
||||
if(err != CHIAKI_ERR_SUCCESS)
|
||||
{
|
||||
CHIAKI_LOGE(session->log, "Failed to parse session request response");
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
@ -619,6 +638,6 @@ static bool session_thread_request_session(ChiakiSession *session)
|
||||
}
|
||||
|
||||
chiaki_http_response_fini(&http_response);
|
||||
close(session_sock);
|
||||
CHIAKI_SOCKET_CLOSE(session_sock);
|
||||
return response.success;
|
||||
}
|
||||
|
@ -17,13 +17,22 @@
|
||||
|
||||
#include <chiaki/stoppipe.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_init(ChiakiStopPipe *stop_pipe)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
stop_pipe->event = WSACreateEvent();
|
||||
if(stop_pipe->event == WSA_INVALID_EVENT)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#else
|
||||
int r = pipe(stop_pipe->fds);
|
||||
if(r < 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
@ -35,23 +44,63 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_init(ChiakiStopPipe *stop_pipe)
|
||||
close(stop_pipe->fds[1]);
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
}
|
||||
#endif
|
||||
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT void chiaki_stop_pipe_fini(ChiakiStopPipe *stop_pipe)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSACloseEvent(stop_pipe->event);
|
||||
#else
|
||||
close(stop_pipe->fds[0]);
|
||||
close(stop_pipe->fds[1]);
|
||||
#endif
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT void chiaki_stop_pipe_stop(ChiakiStopPipe *stop_pipe)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSASetEvent(stop_pipe->event);
|
||||
#else
|
||||
write(stop_pipe->fds[1], "\x00", 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *stop_pipe, int fd, uint64_t timeout_ms)
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *stop_pipe, chiaki_socket_t fd, uint64_t timeout_ms)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSAEVENT events[2];
|
||||
DWORD events_count = 1;
|
||||
events[0] = stop_pipe->event;
|
||||
|
||||
if(!CHIAKI_SOCKET_IS_INVALID(fd))
|
||||
{
|
||||
events_count = 2;
|
||||
events[1] = WSACreateEvent();
|
||||
if(events[1] == WSA_INVALID_EVENT)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
WSAEventSelect(fd, events[1], FD_READ);
|
||||
}
|
||||
|
||||
DWORD r = WSAWaitForMultipleEvents(events_count, events, FALSE, timeout_ms == UINT64_MAX ? WSA_INFINITE : (DWORD)timeout_ms, FALSE);
|
||||
|
||||
if(events_count == 2)
|
||||
WSACloseEvent(events[1]);
|
||||
|
||||
switch(r)
|
||||
{
|
||||
case WSA_WAIT_EVENT_0:
|
||||
return CHIAKI_ERR_CANCELED;
|
||||
case WSA_WAIT_EVENT_0+1:
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
case WSA_WAIT_TIMEOUT:
|
||||
return CHIAKI_ERR_TIMEOUT;
|
||||
default:
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
}
|
||||
#else
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(stop_pipe->fds[0], &fds);
|
||||
@ -85,4 +134,5 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *sto
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
|
||||
return CHIAKI_ERR_TIMEOUT;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -25,7 +25,12 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#include <takion.pb.h>
|
||||
#include <pb_encode.h>
|
||||
@ -659,7 +664,7 @@ static ChiakiErrorCode stream_connection_send_big(ChiakiStreamConnection *stream
|
||||
char json[LAUNCH_SPEC_JSON_BUF_SIZE];
|
||||
char b64[LAUNCH_SPEC_JSON_BUF_SIZE * 2];
|
||||
} launch_spec_buf;
|
||||
ssize_t launch_spec_json_size = chiaki_launchspec_format(launch_spec_buf.json, sizeof(launch_spec_buf.json), &launch_spec);
|
||||
int launch_spec_json_size = chiaki_launchspec_format(launch_spec_buf.json, sizeof(launch_spec_buf.json), &launch_spec);
|
||||
if(launch_spec_json_size < 0)
|
||||
{
|
||||
CHIAKI_LOGE(stream_connection->log, "StreamConnection failed to format LaunchSpec json");
|
||||
|
@ -19,7 +19,6 @@
|
||||
#include <chiaki/congestioncontrol.h>
|
||||
#include <chiaki/random.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
@ -27,7 +26,12 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <netinet/ip.h>
|
||||
#endif
|
||||
|
||||
|
||||
// VERY similar to SCTP, see RFC 4960
|
||||
@ -66,7 +70,7 @@ typedef enum takion_packet_type_t {
|
||||
/**
|
||||
* @return The offset of the mac of size CHIAKI_GKCRYPT_GMAC_SIZE inside a packet of type or -1 if unknown.
|
||||
*/
|
||||
ssize_t takion_packet_type_mac_offset(TakionPacketType type)
|
||||
int takion_packet_type_mac_offset(TakionPacketType type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
@ -83,7 +87,7 @@ ssize_t takion_packet_type_mac_offset(TakionPacketType type)
|
||||
/**
|
||||
* @return The offset of the 4-byte key_pos inside a packet of type or -1 if unknown.
|
||||
*/
|
||||
ssize_t takion_packet_type_key_pos_offset(TakionPacketType type)
|
||||
int takion_packet_type_key_pos_offset(TakionPacketType type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
@ -225,7 +229,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
|
||||
}
|
||||
|
||||
takion->sock = socket(info->sa->sa_family, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if(takion->sock < 0)
|
||||
if(CHIAKI_SOCKET_IS_INVALID(takion->sock))
|
||||
{
|
||||
CHIAKI_LOGE(takion->log, "Takion failed to create socket");
|
||||
ret = CHIAKI_ERR_NETWORK;
|
||||
@ -233,7 +237,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
|
||||
}
|
||||
|
||||
const int rcvbuf_val = takion->a_rwnd;
|
||||
int r = setsockopt(takion->sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_val, sizeof(rcvbuf_val));
|
||||
int r = setsockopt(takion->sock, SOL_SOCKET, SO_RCVBUF, (const void *)&rcvbuf_val, sizeof(rcvbuf_val));
|
||||
if(r < 0)
|
||||
{
|
||||
CHIAKI_LOGE(takion->log, "Takion failed to setsockopt SO_RCVBUF: %s", strerror(errno));
|
||||
@ -243,9 +247,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
|
||||
|
||||
#if __APPLE__
|
||||
CHIAKI_LOGW(takion->log, "Don't fragment is not supported on macOS, MTU values may be incorrect.");
|
||||
#else
|
||||
#if defined(_WIN32)
|
||||
const DWORD dontfragment_val = 1;
|
||||
r = setsockopt(takion->sock, IPPROTO_IP, IP_DONTFRAGMENT, (const void *)&dontfragment_val, sizeof(dontfragment_val));
|
||||
#else
|
||||
const int mtu_discover_val = IP_PMTUDISC_DO;
|
||||
r = setsockopt(takion->sock, IPPROTO_IP, IP_MTU_DISCOVER, &mtu_discover_val, sizeof(mtu_discover_val));
|
||||
r = setsockopt(takion->sock, IPPROTO_IP, IP_MTU_DISCOVER, (const void *)&mtu_discover_val, sizeof(mtu_discover_val));
|
||||
#endif
|
||||
if(r < 0)
|
||||
{
|
||||
CHIAKI_LOGE(takion->log, "Takion failed to setsockopt IP_MTU_DISCOVER: %s", strerror(errno));
|
||||
@ -274,7 +283,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
|
||||
error_sock:
|
||||
close(takion->sock);
|
||||
CHIAKI_SOCKET_CLOSE(takion->sock);
|
||||
error_pipe:
|
||||
chiaki_stop_pipe_fini(&takion->stop_pipe);
|
||||
error_seq_num_local_mutex:
|
||||
@ -318,7 +327,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_crypt_advance_key_pos(ChiakiTakion *
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_raw(ChiakiTakion *takion, const uint8_t *buf, size_t buf_size)
|
||||
{
|
||||
ssize_t r = send(takion->sock, buf, buf_size, 0);
|
||||
int r = send(takion->sock, buf, buf_size, 0);
|
||||
if(r < 0)
|
||||
return CHIAKI_ERR_NETWORK;
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
@ -331,8 +340,8 @@ static ChiakiErrorCode chiaki_takion_packet_mac(ChiakiGKCrypt *crypt, uint8_t *b
|
||||
return CHIAKI_ERR_BUF_TOO_SMALL;
|
||||
|
||||
TakionPacketType base_type = buf[0] & TAKION_PACKET_BASE_TYPE_MASK;
|
||||
ssize_t mac_offset = takion_packet_type_mac_offset(base_type);
|
||||
ssize_t key_pos_offset = takion_packet_type_key_pos_offset(base_type);
|
||||
int mac_offset = takion_packet_type_mac_offset(base_type);
|
||||
int key_pos_offset = takion_packet_type_key_pos_offset(base_type);
|
||||
if(mac_offset < 0 || key_pos_offset < 0)
|
||||
return CHIAKI_ERR_INVALID_DATA;
|
||||
|
||||
@ -730,7 +739,7 @@ beach:
|
||||
event.type = CHIAKI_TAKION_EVENT_TYPE_DISCONNECT;
|
||||
takion->cb(&event, takion->cb_user);
|
||||
}
|
||||
close(takion->sock);
|
||||
CHIAKI_SOCKET_CLOSE(takion->sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -746,7 +755,7 @@ static ChiakiErrorCode takion_recv(ChiakiTakion *takion, uint8_t *buf, size_t *b
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t received_sz = recv(takion->sock, buf, *buf_size, 0);
|
||||
int received_sz = recv(takion->sock, buf, *buf_size, 0);
|
||||
if(received_sz <= 0)
|
||||
{
|
||||
if(received_sz < 0)
|
||||
@ -1373,4 +1382,4 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_v7_av_packet_parse(ChiakiTakionAVPac
|
||||
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
|
||||
}
|
||||
}
|
||||
|
100
lib/src/thread.c
100
lib/src/thread.c
@ -21,34 +21,69 @@
|
||||
#include <chiaki/time.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#if _WIN32
|
||||
static DWORD WINAPI win32_thread_func(LPVOID param)
|
||||
{
|
||||
ChiakiThread *thread = (ChiakiThread *)param;
|
||||
thread->ret = thread->func(thread->arg);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_create(ChiakiThread *thread, ChiakiThreadFunc func, void *arg)
|
||||
{
|
||||
#if _WIN32
|
||||
thread->func = func;
|
||||
thread->arg = arg;
|
||||
thread->ret = NULL;
|
||||
thread->thread = CreateThread(NULL, 0, win32_thread_func, thread, 0, 0);
|
||||
if(!thread->thread)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
#else
|
||||
int r = pthread_create(&thread->thread, NULL, func, arg);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_join(ChiakiThread *thread, void **retval)
|
||||
{
|
||||
#if _WIN32
|
||||
int r = WaitForSingleObject(thread->thread, INFINITE);
|
||||
if(r != WAIT_OBJECT_0)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
if(retval)
|
||||
*retval = thread->ret;
|
||||
#else
|
||||
int r = pthread_join(thread->thread, retval);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_set_name(ChiakiThread *thread, const char *name)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
|
||||
wchar_t *wstr = calloc(sizeof(wchar_t), len+1);
|
||||
if(!wstr)
|
||||
return CHIAKI_ERR_MEMORY;
|
||||
MultiByteToWideChar(CP_UTF8, 0, name, -1, wstr, len);
|
||||
SetThreadDescription(thread->thread, wstr);
|
||||
free(wstr);
|
||||
#else
|
||||
#ifdef __GLIBC__
|
||||
int r = pthread_setname_np(thread->thread, name);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
#else
|
||||
(void)thread; (void)name;
|
||||
#endif
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
@ -56,6 +91,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_thread_set_name(ChiakiThread *thread, const
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex, bool rec)
|
||||
{
|
||||
#if _WIN32
|
||||
InitializeCriticalSection(&mutex->cs);
|
||||
(void)rec; // always recursive
|
||||
#else
|
||||
pthread_mutexattr_t attr;
|
||||
int r = pthread_mutexattr_init(&attr);
|
||||
if(r != 0)
|
||||
@ -69,40 +108,59 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_init(ChiakiMutex *mutex, bool rec)
|
||||
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_fini(ChiakiMutex *mutex)
|
||||
{
|
||||
#if _WIN32
|
||||
DeleteCriticalSection(&mutex->cs);
|
||||
#else
|
||||
int r = pthread_mutex_destroy(&mutex->mutex);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_lock(ChiakiMutex *mutex)
|
||||
{
|
||||
#if _WIN32
|
||||
EnterCriticalSection(&mutex->cs);
|
||||
#else
|
||||
int r = pthread_mutex_lock(&mutex->mutex);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_trylock(ChiakiMutex *mutex)
|
||||
{
|
||||
#if _WIN32
|
||||
int r = TryEnterCriticalSection(&mutex->cs);
|
||||
if(!r)
|
||||
return CHIAKI_ERR_MUTEX_LOCKED;
|
||||
#else
|
||||
int r = pthread_mutex_trylock(&mutex->mutex);
|
||||
if(r == EBUSY)
|
||||
return CHIAKI_ERR_MUTEX_LOCKED;
|
||||
else if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_unlock(ChiakiMutex *mutex)
|
||||
{
|
||||
#if _WIN32
|
||||
LeaveCriticalSection(&mutex->cs);
|
||||
#else
|
||||
int r = pthread_mutex_unlock(&mutex->mutex);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
@ -111,6 +169,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_mutex_unlock(ChiakiMutex *mutex)
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_init(ChiakiCond *cond)
|
||||
{
|
||||
#if _WIN32
|
||||
InitializeConditionVariable(&cond->cond);
|
||||
#else
|
||||
pthread_condattr_t attr;
|
||||
int r = pthread_condattr_init(&attr);
|
||||
if(r != 0)
|
||||
@ -130,14 +191,18 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_init(ChiakiCond *cond)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
}
|
||||
pthread_condattr_destroy(&attr);
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_fini(ChiakiCond *cond)
|
||||
{
|
||||
#if _WIN32
|
||||
#else
|
||||
int r = pthread_cond_destroy(&cond->cond);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
@ -145,13 +210,19 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_fini(ChiakiCond *cond)
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait(ChiakiCond *cond, ChiakiMutex *mutex)
|
||||
{
|
||||
#if _WIN32
|
||||
int r = SleepConditionVariableCS(&cond->cond, &mutex->cs, INFINITE);
|
||||
if(!r)
|
||||
return CHIAKI_ERR_THREAD;
|
||||
#else
|
||||
int r = pthread_cond_wait(&cond->cond, &mutex->mutex);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
#if !__APPLE__
|
||||
#if !__APPLE__ && !defined(_WIN32)
|
||||
static ChiakiErrorCode chiaki_cond_timedwait_abs(ChiakiCond *cond, ChiakiMutex *mutex, struct timespec *timeout)
|
||||
{
|
||||
int r = pthread_cond_timedwait(&cond->cond, &mutex->mutex, timeout);
|
||||
@ -179,6 +250,16 @@ static void set_timeout(struct timespec *timeout, uint64_t ms_from_now)
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms)
|
||||
{
|
||||
#if _WIN32
|
||||
int r = SleepConditionVariableCS(&cond->cond, &mutex->cs, (DWORD)timeout_ms);
|
||||
if(!r)
|
||||
{
|
||||
if(GetLastError() == ERROR_TIMEOUT)
|
||||
return CHIAKI_ERR_TIMEOUT;
|
||||
return CHIAKI_ERR_THREAD;
|
||||
}
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
#else
|
||||
struct timespec timeout;
|
||||
#if __APPLE__
|
||||
timeout.tv_sec = (__darwin_time_t)(timeout_ms / 1000);
|
||||
@ -195,6 +276,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait(ChiakiCond *cond, ChiakiMute
|
||||
set_timeout(&timeout, timeout_ms);
|
||||
return chiaki_cond_timedwait_abs(cond, mutex, &timeout);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMutex *mutex, ChiakiCheckPred check_pred, void *check_pred_user)
|
||||
@ -210,7 +292,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_wait_pred(ChiakiCond *cond, ChiakiMute
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, ChiakiMutex *mutex, uint64_t timeout_ms, ChiakiCheckPred check_pred, void *check_pred_user)
|
||||
{
|
||||
#if __APPLE__
|
||||
#if __APPLE__ || defined(_WIN32)
|
||||
uint64_t start_time = chiaki_time_now_monotonic_ms();
|
||||
uint64_t elapsed = 0;
|
||||
#else
|
||||
@ -219,14 +301,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, Chiak
|
||||
#endif
|
||||
while(!check_pred(check_pred_user))
|
||||
{
|
||||
#if __APPLE__
|
||||
#if __APPLE__ || defined(_WIN32)
|
||||
ChiakiErrorCode err = chiaki_cond_timedwait(cond, mutex, timeout_ms - elapsed);
|
||||
#else
|
||||
ChiakiErrorCode err = chiaki_cond_timedwait_abs(cond, mutex, &timeout);
|
||||
#endif
|
||||
if(err != CHIAKI_ERR_SUCCESS)
|
||||
return err;
|
||||
#if __APPLE__
|
||||
#if __APPLE__ || defined(_WIN32)
|
||||
elapsed = chiaki_time_now_monotonic_ms() - start_time;
|
||||
if(elapsed >= timeout_ms)
|
||||
return CHIAKI_ERR_TIMEOUT;
|
||||
@ -238,17 +320,25 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_timedwait_pred(ChiakiCond *cond, Chiak
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_signal(ChiakiCond *cond)
|
||||
{
|
||||
#if _WIN32
|
||||
WakeConditionVariable(&cond->cond);
|
||||
#else
|
||||
int r = pthread_cond_signal(&cond->cond);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_cond_broadcast(ChiakiCond *cond)
|
||||
{
|
||||
#if _WIN32
|
||||
WakeAllConditionVariable(&cond->cond);
|
||||
#else
|
||||
int r = pthread_cond_broadcast(&cond->cond);
|
||||
if(r != 0)
|
||||
return CHIAKI_ERR_UNKNOWN;
|
||||
#endif
|
||||
return CHIAKI_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -19,9 +19,25 @@
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
CHIAKI_EXPORT uint64_t chiaki_time_now_monotonic_us()
|
||||
{
|
||||
#if _WIN32
|
||||
LARGE_INTEGER f;
|
||||
if(!QueryPerformanceFrequency(&f))
|
||||
return 0;
|
||||
LARGE_INTEGER v;
|
||||
if(!QueryPerformanceCounter(&v))
|
||||
return 0;
|
||||
v.QuadPart *= 1000000;
|
||||
v.QuadPart /= f.QuadPart;
|
||||
return v.QuadPart;
|
||||
#else
|
||||
struct timespec time;
|
||||
clock_gettime(CLOCK_MONOTONIC, &time);
|
||||
return time.tv_sec * 1000000 + time.tv_nsec / 1000;
|
||||
#endif
|
||||
}
|
||||
|
@ -19,10 +19,16 @@
|
||||
#define CHIAKI_UTILS_H
|
||||
|
||||
#include <chiaki/common.h>
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
static inline ChiakiErrorCode set_port(struct sockaddr *sa, in_port_t port)
|
||||
#include <stdint.h>
|
||||
|
||||
static inline ChiakiErrorCode set_port(struct sockaddr *sa, uint16_t port)
|
||||
{
|
||||
if(sa->sa_family == AF_INET)
|
||||
((struct sockaddr_in *)sa)->sin_port = port;
|
||||
|
80
scripts/appveyor.sh
Executable file
80
scripts/appveyor.sh
Executable file
@ -0,0 +1,80 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "APPVEYOR_BUILD_FOLDER=$APPVEYOR_BUILD_FOLDER"
|
||||
|
||||
mkdir ninja && cd ninja || exit 1
|
||||
wget https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-win.zip && 7z x ninja-win.zip || exit 1
|
||||
cd .. || exit 1
|
||||
|
||||
mkdir yasm && cd yasm || exit 1
|
||||
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0-win64.exe && mv yasm-1.3.0-win64.exe yasm.exe || exit 1
|
||||
cd .. || exit 1
|
||||
|
||||
export PATH="$PWD/ninja:$PWD/yasm:/c/Qt/5.12/msvc2017_64/bin:$PATH"
|
||||
|
||||
scripts/build-ffmpeg.sh --target-os=win64 --arch=x86_64 --toolchain=msvc || exit 1
|
||||
|
||||
git clone https://github.com/xiph/opus.git && cd opus && git checkout ad8fe90db79b7d2a135e3dfd2ed6631b0c5662ab || exit 1
|
||||
mkdir build && cd build || exit 1
|
||||
cmake \
|
||||
-G Ninja \
|
||||
-DCMAKE_C_COMPILER=cl \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_INSTALL_PREFIX="$APPVEYOR_BUILD_FOLDER/opus-prefix" \
|
||||
.. || exit 1
|
||||
ninja || exit 1
|
||||
ninja install || exit 1
|
||||
cd ../.. || exit 1
|
||||
|
||||
wget https://mirror.firedaemon.com/OpenSSL/openssl-1.1.1d-dev.zip && 7z x openssl-1.1.1d-dev.zip || exit 1
|
||||
|
||||
wget https://www.libsdl.org/release/SDL2-devel-2.0.10-VC.zip && 7z x SDL2-devel-2.0.10-VC.zip || exit 1
|
||||
export SDL_ROOT="$APPVEYOR_BUILD_FOLDER/SDL2-2.0.10" || exit 1
|
||||
export SDL_ROOT=${SDL_ROOT//[\\]//} || exit 1
|
||||
echo "set(SDL2_INCLUDE_DIRS \"$SDL_ROOT/include\")
|
||||
set(SDL2_LIBRARIES \"$SDL_ROOT/lib/x64/SDL2.lib\")
|
||||
set(SDL2_LIBDIR \"$SDL_ROOT/lib/x64\")" > "$SDL_ROOT/SDL2Config.cmake" || exit 1
|
||||
|
||||
mkdir protoc && cd protoc || exit 1
|
||||
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-win64.zip && 7z x protoc-3.9.1-win64.zip || exit 1
|
||||
cd .. || exit 1
|
||||
export PATH="$PWD/protoc/bin:$PATH" || exit 1
|
||||
|
||||
PYTHON="C:/Python37/python.exe"
|
||||
"$PYTHON" -m pip install protobuf || exit 1
|
||||
|
||||
mkdir build && cd build || exit 1
|
||||
|
||||
QT_PATH="C:/Qt/5.12.4/msvc2017_64"
|
||||
|
||||
cmake \
|
||||
-G Ninja \
|
||||
-DCMAKE_C_COMPILER=cl \
|
||||
-DCMAKE_C_FLAGS="-we4013" \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_PREFIX_PATH="$APPVEYOR_BUILD_FOLDER/ffmpeg-prefix;$APPVEYOR_BUILD_FOLDER/opus-prefix;$APPVEYOR_BUILD_FOLDER/openssl-1.1/x64;$QT_PATH;$SDL_ROOT" \
|
||||
-DPYTHON_EXECUTABLE="$PYTHON" \
|
||||
-DCHIAKI_ENABLE_TESTS=ON \
|
||||
-DCHIAKI_ENABLE_CLI=OFF \
|
||||
-DCHIAKI_GUI_ENABLE_QT_GAMEPAD=OFF \
|
||||
-DCHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER=ON \
|
||||
.. || exit 1
|
||||
|
||||
ninja || exit 1
|
||||
cat gui/chiaki_autogen/*/qrc_resources.cpp
|
||||
cd .. || exit 1
|
||||
|
||||
|
||||
# Deploy
|
||||
|
||||
mkdir Chiaki && cp build/gui/chiaki.exe Chiaki || exit 1
|
||||
|
||||
# set CMAKE_BUILD_TYPE=RelWithDebInfo for pdbs
|
||||
# cp build/gui/chiaki.pdb Chiaki
|
||||
|
||||
"$QT_PATH/bin/windeployqt.exe" Chiaki/chiaki.exe || exit 1
|
||||
cp \
|
||||
openssl-1.1/x64/bin/libcrypto-1_1-x64.dll \
|
||||
openssl-1.1/x64/bin/libssl-1_1-x64.dll \
|
||||
"$SDL_ROOT/lib/x64/SDL2.dll" \
|
||||
Chiaki
|
@ -7,6 +7,6 @@ TAG=n4.2
|
||||
|
||||
git clone https://git.ffmpeg.org/ffmpeg.git --depth 1 -b $TAG && cd ffmpeg || exit 1
|
||||
|
||||
./configure --disable-all --enable-avcodec --enable-decoder=h264 --prefix="$ROOT/ffmpeg-prefix" || exit 1
|
||||
./configure --disable-all --enable-avcodec --enable-decoder=h264 --prefix="$ROOT/ffmpeg-prefix" "$@" || exit 1
|
||||
make -j4 || exit 1
|
||||
make install || exit 1
|
@ -138,7 +138,7 @@ static void seqnums_ack(ChiakiSeqNum32 *nums, size_t *nums_count, ChiakiSeqNum32
|
||||
|
||||
static MunitResult test_takion_send_buffer(const MunitParameter params[], void *user)
|
||||
{
|
||||
static const size_t nums_count = 0x30;
|
||||
#define nums_count 0x30
|
||||
ChiakiTakionSendBuffer send_buffer;
|
||||
ChiakiErrorCode err = chiaki_takion_send_buffer_init(&send_buffer, NULL, nums_count);
|
||||
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
|
||||
@ -169,6 +169,7 @@ static MunitResult test_takion_send_buffer(const MunitParameter params[], void *
|
||||
|
||||
chiaki_takion_send_buffer_fini(&send_buffer);
|
||||
return MUNIT_OK;
|
||||
#undef nums_count
|
||||
}
|
||||
|
||||
|
||||
@ -199,4 +200,4 @@ MunitTest tests_takion[] = {
|
||||
NULL
|
||||
},
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
};
|
||||
};
|
||||
|
2
third-party/gf-complete
vendored
2
third-party/gf-complete
vendored
@ -1 +1 @@
|
||||
Subproject commit a6862d10c9db467148f20eef2c6445ac9afd94d8
|
||||
Subproject commit fa54a4670a5705c84abf6c24b92b0cd479625478
|
2
third-party/jerasure
vendored
2
third-party/jerasure
vendored
@ -1 +1 @@
|
||||
Subproject commit de1739cc8483696506829b52e7fda4f6bb195e6a
|
||||
Subproject commit 505ccb4bc69eee8d7ea40a6089a056b99671134f
|
Loading…
x
Reference in New Issue
Block a user