From a90f143af02407c07a995ee4a811227e2ab53665 Mon Sep 17 00:00:00 2001 From: Nesbox Date: Mon, 7 Dec 2020 15:27:54 +0300 Subject: [PATCH] #1264: network functions moved to system folder --- CMakeLists.txt | 107 ++++++++++++++++------------------ src/studio/http.h | 61 +++++++++++++++++++ src/studio/system.h | 77 ++++++++++-------------- src/system/n3ds/main.c | 32 ++++------ src/{ext => system/net}/net.c | 47 +++++---------- src/{ext => system/net}/net.h | 4 +- src/system/sdl/main.c | 4 +- src/system/sokol/sokol.c | 6 +- 8 files changed, 174 insertions(+), 164 deletions(-) create mode 100644 src/studio/http.h rename src/{ext => system/net}/net.c (93%) rename src/{ext => system/net}/net.h (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd2502091..43c2a0017 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,18 +65,11 @@ message("BUILD_TOUCH_INPUT: ${BUILD_TOUCH_INPUT}") if (N3DS) set(BUILD_SDL off) - # disable CURL linking - use HTTPC instead - set(DISABLE_NETWORKING TRUE) endif() if (BAREMETALPI) set(BUILD_SDL OFF) set(BUILD_DEMO_CARTS OFF) - set(DISABLE_NETWORKING TRUE) -endif() - -if (DISABLE_NETWORKING) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDISABLE_NETWORKING") endif() if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN AND NOT ANDROID AND NOT N3DS) @@ -465,41 +458,6 @@ if(BUILD_LIBRETRO) set_target_properties(tic80_libretro PROPERTIES PREFIX "") endif() -################################ -# CURL -################################ - -if (NOT DISABLE_NETWORKING) - - if (NOT N3DS AND NOT EMSCRIPTEN) - - set(BUILD_SHARED_LIBS OFF CACHE BOOL "") - - if(WIN32) - set(CURL_STATIC_CRT ON CACHE BOOL "") - endif() - - set(CMAKE_USE_OPENSSL OFF CACHE BOOL "" ) - set(CMAKE_USE_LIBSSH2 OFF CACHE BOOL "") - set(HTTP_ONLY ON CACHE BOOL "") - set(BUILD_CURL_EXE OFF CACHE BOOL "") - set(CURL_CA_BUNDLE "none" CACHE STRING "") - set(CURL_CA_PATH "none" CACHE STRING "") - - if(RPI) - set(CURL_ZLIB OFF CACHE BOOL "" ) - endif() - - add_subdirectory(${THIRDPARTY_DIR}/curl) - - if(RPI) - target_compile_definitions(libcurl PRIVATE NOT_USE_FILE_OFFSET64) - endif() - - endif() - -endif() - ################################ # ZLIB ################################ @@ -623,7 +581,6 @@ set(TIC80STUDIO_SRC ${TIC80LIB_DIR}/ext/md5.c ${TIC80LIB_DIR}/ext/gif.c ${TIC80LIB_DIR}/ext/history.c - ${TIC80LIB_DIR}/ext/net.c ) set(TIC80_OUTPUT tic80) @@ -643,14 +600,6 @@ if(N3DS) target_link_directories(tic80studio PUBLIC ${DEVKITPRO}/portlibs/3ds/lib) endif() -if(NOT DISABLE_NETWORKING) -if(N3DS) - target_link_libraries(tic80studio curl mbedtls mbedx509 mbedcrypto) -elseif(NOT EMSCRIPTEN) - target_link_libraries(tic80studio libcurl) -endif() -endif() - if(BUILD_PRO) target_compile_definitions(tic80studio PRIVATE TIC80_PRO) endif() @@ -737,6 +686,50 @@ if(MINGW) set(CMAKE_EXE_LINKER_FLAGS "-static") endif() +################################ +# CURL +################################ + +if (NOT N3DS AND NOT EMSCRIPTEN AND NOT BAREMETALPI) + + set(BUILD_SHARED_LIBS OFF CACHE BOOL "") + + if(WIN32) + set(CURL_STATIC_CRT ON CACHE BOOL "") + endif() + + set(CMAKE_USE_OPENSSL OFF CACHE BOOL "" ) + set(CMAKE_USE_LIBSSH2 OFF CACHE BOOL "") + set(HTTP_ONLY ON CACHE BOOL "") + set(BUILD_CURL_EXE OFF CACHE BOOL "") + set(CURL_CA_BUNDLE "none" CACHE STRING "") + set(CURL_CA_PATH "none" CACHE STRING "") + + if(RPI) + set(CURL_ZLIB OFF CACHE BOOL "" ) + endif() + + add_subdirectory(${THIRDPARTY_DIR}/curl) + + if(RPI) + target_compile_definitions(libcurl PRIVATE NOT_USE_FILE_OFFSET64) + endif() + +endif() + +################################ +# Network library +################################ + +add_library(net STATIC ${CMAKE_SOURCE_DIR}/src/system/net/net.c) +target_include_directories(net + PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include + INTERFACE ${CMAKE_SOURCE_DIR}/src/system) + +if(NOT EMSCRIPTEN) + target_link_libraries(net libcurl) +endif() + ################################ # TIC-80 app ################################ @@ -786,7 +779,7 @@ if(BUILD_SDL) target_link_libraries(tic80 SDL2main) endif() - target_link_libraries(tic80 tic80studio) + target_link_libraries(tic80 tic80studio net) if(BUILD_TOUCH_INPUT) target_compile_definitions(tic80 PRIVATE TOUCH_INPUT_SUPPORT) @@ -851,7 +844,7 @@ if(BUILD_SOKOL) target_link_libraries(tic80-sokol mingw32) endif() - target_link_libraries(tic80-sokol tic80studio sokol) + target_link_libraries(tic80-sokol tic80studio sokol net) endif() @@ -867,19 +860,17 @@ if(N3DS) ${CMAKE_SOURCE_DIR}/src/system/n3ds/main.c ) - if (DISABLE_NETWORKING) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_HTTPC") - endif() - add_executable(tic80_n3ds ${TIC80_SRC}) + target_compile_definitions(tic80_n3ds PRIVATE ENABLE_HTTPC) + target_include_directories(tic80_n3ds PRIVATE ${DEVKITPRO}/portlibs/3ds/include ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) target_link_directories(tic80_n3ds PRIVATE ${DEVKITPRO}/libctru/lib) - target_link_libraries(tic80_n3ds tic80studio png citro3d) + target_link_libraries(tic80_n3ds tic80studio png citro3d curl mbedtls mbedx509 mbedcrypto) add_custom_command(TARGET tic80_n3ds POST_BUILD diff --git a/src/studio/http.h b/src/studio/http.h new file mode 100644 index 000000000..2bcf74d10 --- /dev/null +++ b/src/studio/http.h @@ -0,0 +1,61 @@ +// MIT License + +// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include "tic80_types.h" + +typedef struct +{ + enum + { + HttpGetProgress, + HttpGetDone, + HttpGetError, + } type; + + union + { + struct + { + s32 size; + s32 total; + } progress; + + struct + { + s32 size; + u8* data; + } done; + + struct + { + s32 code; + } error; + }; + + void* calldata; + const char* url; + +} HttpGetData; + +typedef void(*HttpGetCallback)(const HttpGetData*); diff --git a/src/studio/system.h b/src/studio/system.h index e6b34503c..beb05b360 100644 --- a/src/studio/system.h +++ b/src/studio/system.h @@ -1,7 +1,30 @@ +// MIT License + +// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #pragma once #include "api.h" #include "version.h" +#include "http.h" #if defined(TIC80_PRO) #define TIC_VERSION_POST " Pro" @@ -30,42 +53,6 @@ #define KEYBOARD_HOLD 20 #define KEYBOARD_PERIOD 3 -typedef struct -{ - enum - { - HttpGetProgress, - HttpGetDone, - HttpGetError, - } type; - - union - { - struct - { - s32 size; - s32 total; - } progress; - - struct - { - s32 size; - u8* data; - } done; - - struct - { - s32 code; - } error; - }; - - void* calldata; - const char* url; - -} HttpGetData; - -typedef void(*HttpGetCallback)(const HttpGetData*); - typedef struct { void (*setClipboardText)(const char* text); @@ -76,18 +63,18 @@ typedef struct u64 (*getPerformanceCounter)(); u64 (*getPerformanceFrequency)(); - void* (*httpGetSync)(const char* url, s32* size); - void (*httpGet)(const char* url, HttpGetCallback callback, void* userdata); + void* (*httpGetSync)(const char* url, s32* size); + void (*httpGet)(const char* url, HttpGetCallback callback, void* userdata); - void (*goFullscreen)(); - void (*showMessageBox)(const char* title, const char* message); - void (*setWindowTitle)(const char* title); + void (*goFullscreen)(); + void (*showMessageBox)(const char* title, const char* message); + void (*setWindowTitle)(const char* title); - void (*openSystemPath)(const char* path); + void (*openSystemPath)(const char* path); - void (*preseed)(); - void (*poll)(); - char (*text)(); + void (*preseed)(); + void (*poll)(); + char (*text)(); void (*updateConfig)(); diff --git a/src/system/n3ds/main.c b/src/system/n3ds/main.c index 9a6908a73..d0570cfbe 100644 --- a/src/system/n3ds/main.c +++ b/src/system/n3ds/main.c @@ -31,7 +31,6 @@ #include #include "studio/system.h" -#include "ext/net.h" #include "keyboard.h" #include "utils.h" @@ -50,9 +49,6 @@ static struct { Studio* studio; char* clipboard; -#ifndef DISABLE_NETWORKING - Net* net; -#endif struct { @@ -198,23 +194,25 @@ static u64 getPerformanceFrequency() static void* httpGetSync(const char* url, s32* size) { -#ifndef DISABLE_NETWORKING - return netGetSync(platform.net, url, size); -#else #ifdef ENABLE_HTTPC return n3ds_net_get_sync(&platform.httpc, url, size); -#endif +#else + return NULL; #endif } static void httpGet(const char* url, HttpGetCallback callback, void* calldata) { -#ifndef DISABLE_NETWORKING - netGet(platform.net, url, callback, calldata); -#else #ifdef ENABLE_HTTPC n3ds_net_get(&platform.httpc, url, callback, calldata); -#endif +#else + HttpGetData getData = + { + .type = HttpGetError, + .calldata = calldata, + .url = path, + }; + callback(&getData); #endif } @@ -612,9 +610,6 @@ int main(int argc, char **argv) { n3ds_draw_init(); n3ds_keyboard_init(&platform.keyboard); -#ifndef DISABLE_NETWORKING - platform.net = createNet(); -#endif platform.studio = studioInit(argc_used, (const char**)argv_used, AUDIO_FREQ, "./", &systemInterface); platform.studio->tic->screen_format = TIC80_PIXEL_COLOR_ABGR8888; @@ -624,9 +619,7 @@ int main(int argc, char **argv) { u32 start_frame = C3D_FrameCounter(0); LightLock_Lock(&platform.tick_lock); -#ifndef DISABLE_NETWORKING - netTick(platform.net); -#endif + keyboard_update(); platform.studio->tick(); @@ -648,9 +641,6 @@ int main(int argc, char **argv) { n3ds_sound_exit(); platform.studio->close(); -#ifndef DISABLE_NETWORKING - closeNet(platform.net); -#endif n3ds_keyboard_free(&platform.keyboard); n3ds_draw_exit(); diff --git a/src/ext/net.c b/src/system/net/net.c similarity index 93% rename from src/ext/net.c rename to src/system/net/net.c index 6bdd2e0bb..79b3dc7af 100644 --- a/src/ext/net.c +++ b/src/system/net/net.c @@ -26,8 +26,9 @@ #include #include -#ifndef DISABLE_NETWORKING - #if defined(__EMSCRIPTEN__) +#define URL_SIZE 2048 + +#if defined(__EMSCRIPTEN__) #include @@ -106,7 +107,7 @@ static void downloadProgress(emscripten_fetch_t *fetch) data->callback(&getData); } - #else +#else #include @@ -118,11 +119,12 @@ typedef struct struct Curl_easy* async; HttpGetCallback callback; void* calldata; - char url[TICNAME_MAX]; + char url[URL_SIZE]; } CurlData; struct Net { + const char* host; CURLM* multi; struct Curl_easy* sync; }; @@ -183,23 +185,11 @@ static size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata return total; } - #endif #endif void netGet(Net* net, const char* path, HttpGetCallback callback, void* calldata) { -#ifdef DISABLE_NETWORKING - - HttpGetData getData = - { - .type = HttpGetError, - .calldata = calldata, - .url = path, - }; - callback(&getData); - -#else - #if defined(__EMSCRIPTEN__) +#if defined(__EMSCRIPTEN__) FetchData* data = calloc(1, sizeof(FetchData)); *data = (FetchData) @@ -228,7 +218,8 @@ void netGet(Net* net, const char* path, HttpGetCallback callback, void* calldata curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); { - char url[TICNAME_MAX] = TIC_WEBSITE; + char url[URL_SIZE]; + strcpy(url, net->host); strcat(url, path); curl_easy_setopt(curl, CURLOPT_URL, url); @@ -238,7 +229,6 @@ void netGet(Net* net, const char* path, HttpGetCallback callback, void* calldata curl_multi_add_handle(net->multi, curl); } - #endif #endif } @@ -259,7 +249,8 @@ void* netGetSync(Net* net, const char* path, s32* size) if(net->sync) { - char url[TICNAME_MAX] = TIC_WEBSITE; + char url[URL_SIZE]; + strcpy(url, net->host); strcat(url, path); curl_easy_setopt(net->sync, CURLOPT_URL, url); @@ -284,9 +275,7 @@ void* netGetSync(Net* net, const char* path, s32* size) void netTick(Net *net) { -#ifndef DISABLE_NETWORKING - - #if !defined(__EMSCRIPTEN__) +#if !defined(__EMSCRIPTEN__) { s32 running = 0; @@ -346,16 +335,11 @@ void netTick(Net *net) curl_easy_cleanup(msg->easy_handle); } } - #endif #endif } -Net* createNet() +Net* createNet(const char* host) { -#ifdef DISABLE_NETWORKING - return NULL; -#else - Net* net = (Net*)malloc(sizeof(Net)); #if defined(__EMSCRIPTEN__) @@ -375,6 +359,7 @@ Net* createNet() { .sync = curl_easy_init(), .multi = curl_multi_init(), + .host = host, }; curl_easy_setopt(net->sync, CURLOPT_WRITEFUNCTION, writeCallbackSync); @@ -383,13 +368,10 @@ Net* createNet() #endif return net; -#endif } void closeNet(Net* net) { -#ifndef DISABLE_NETWORKING - #if !defined(__EMSCRIPTEN__) if(net->sync) curl_easy_cleanup(net->sync); @@ -400,5 +382,4 @@ void closeNet(Net* net) #endif free(net); -#endif } diff --git a/src/ext/net.h b/src/system/net/net.h similarity index 95% rename from src/ext/net.h rename to src/system/net/net.h index 19292c647..e3d54e62c 100644 --- a/src/ext/net.h +++ b/src/system/net/net.h @@ -22,11 +22,11 @@ #pragma once -#include "studio/system.h" +#include "studio/http.h" typedef struct Net Net; -Net* createNet(); +Net* createNet(const char* host); void* netGetSync(Net* net, const char* path, s32* size); void netGet(Net* net, const char* url, HttpGetCallback callback, void* calldata); void closeNet(Net* net); diff --git a/src/system/sdl/main.c b/src/system/sdl/main.c index 4d4220d83..ed560d4c6 100644 --- a/src/system/sdl/main.c +++ b/src/system/sdl/main.c @@ -22,7 +22,7 @@ #include "studio/system.h" #include "tools.h" -#include "ext/net.h" +#include "net/net.h" #include #include @@ -1604,7 +1604,7 @@ static s32 start(s32 argc, const char **argv, const char* folder) initSound(); - platform.net = createNet(); + platform.net = createNet(TIC_WEBSITE); platform.studio = studioInit(argc, argv, platform.audio.spec.freq, folder, &systemInterface); diff --git a/src/system/sokol/sokol.c b/src/system/sokol/sokol.c index cf1b2a9d3..478fe7479 100644 --- a/src/system/sokol/sokol.c +++ b/src/system/sokol/sokol.c @@ -28,7 +28,7 @@ #include "studio/system.h" #include "system/sokol/sokol.h" -#include "ext/net.h" +#include "net/net.h" static struct { @@ -424,9 +424,9 @@ sapp_desc sokol_main(s32 argc, char* argv[]) platform.audio.desc.num_channels = TIC_STEREO_CHANNELS; saudio_setup(&platform.audio.desc); - platform.net = createNet(); + platform.net = createNet(TIC_WEBSITE); - platform.studio = studioInit(argc, argv, saudio_sample_rate(), "./", &systemInterface); + platform.studio = studioInit(argc, (const char**)argv, saudio_sample_rate(), "./", &systemInterface); const s32 Width = TIC80_FULLWIDTH * platform.studio->config()->uiScale; const s32 Height = TIC80_FULLHEIGHT * platform.studio->config()->uiScale;