Skip to content

Commit de97d0a

Browse files
RicardRCredbaron
andauthored
Private native ws implementation (#179)
* Private native ws implementation * Re-enable all tests. Update private * Move resources to a commonly accessible place. * Fix compilation after cpp 20 brought upon us the ut8 mess. * Fix new resource move constructor bug. * Submodules updates * devkit update * Support force exporting symbols using visibility attribute method. Useful on NDAed platforms * Move Resource from public headers into implementation * Update private * Change resource init. --------- Co-authored-by: Maxim Ivanov <maxim@heroiclabs.com>
1 parent ea97e8d commit de97d0a

File tree

15 files changed

+100
-31
lines changed

15 files changed

+100
-31
lines changed

CMakeLists.txt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ endif()
6868
set(CMAKE_CONFIGURATION_TYPES "Debug;MinSizeRel")
6969
set(CMAKE_SUPPRESS_REGENERATION true) ## for Xcode
7070

71-
option(FORCE_DLL_IMPORT_EXPORT "Force exporting and importing of symbols regardless of the platform." OFF)
71+
option(FORCE_DLL_IMPORT_EXPORT "Force exporting and importing of symbols using __declspec mechanism regardless of the platform." OFF)
72+
option(FORCE_DLL_VISIBILITY "Force exporting symbols using visibility attribute regardless of the platform." OFF)
7273
option(BUILD_APPLE_FRAMEWORK "Build frameworks instead of .dylib files for Apple." ON)
7374

7475
set(NAKAMA_SDK_DEPS)
@@ -99,12 +100,18 @@ if(NOT MSVC)
99100
string(APPEND CMAKE_CXX_FLAGS " -fexceptions")
100101
endif()
101102

103+
option(UNDEFINED_SANITIZER "Enable undefined sanitizer (broken on windows)" OFF)
102104
option(ADDRESS_SANITIZER "Enable address sanitizer (broken on windows)" OFF)
103105

104106
if(LOGS_ENABLED)
105107
add_compile_definitions(NLOGS_ENABLED)
106108
endif(LOGS_ENABLED)
107109

110+
if (UNDEFINED_SANITIZER)
111+
add_compile_options(-fsanitize=undefined -O1)
112+
string(REPLACE /RTC1 "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
113+
endif()
114+
108115
if (ADDRESS_SANITIZER)
109116
add_compile_options(-fsanitize=address -O1 -fno-optimize-sibling-calls -fno-omit-frame-pointer)
110117
string(REPLACE /RTC1 "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
@@ -155,8 +162,6 @@ add_subdirectory(factory EXCLUDE_FROM_ALL)
155162

156163
add_subdirectory(satori-cpp)
157164

158-
include("submodules/private/cmake/target-${VCPKG_TARGET_TRIPLET}.cmake" OPTIONAL)
159-
160165
if (WITH_HTTP_CPPREST)
161166
set(HTTP_IMPL_LIB nakama::impl-http-cppRest)
162167
add_subdirectory(impl/httpCppRest EXCLUDE_FROM_ALL)
@@ -239,6 +244,8 @@ endif()
239244
# defined target and not to fallback to '-lname' when target doesn't exist,
240245
# thus failing at configure time, rather than at link time should we make mistake
241246
# in build scripts
247+
248+
242249
list(APPEND NAKAMA_SDK_DEPS
243250
nakama::sdk-core-misc
244251
nakama::sdk-core-rest
@@ -248,12 +255,20 @@ list(APPEND NAKAMA_SDK_DEPS
248255
nakama::sdk-rtclient-factory
249256
satori::client
250257
)
258+
251259
if (WITH_GRPC_CLIENT)
252260
list(APPEND NAKAMA_SDK_DEPS nakama::sdk-core-grpc)
253261
endif()
254262

255263
add_library(nakama-sdk SHARED ${DUMMY_CPP} ${DUMMY_H})
256264
add_library(nakama::sdk ALIAS nakama-sdk)
265+
266+
if (BUILD_TESTING)
267+
add_subdirectory(test)
268+
endif ()
269+
270+
include("submodules/private/cmake/target-${VCPKG_TARGET_TRIPLET}.cmake" OPTIONAL)
271+
257272
target_link_libraries(nakama-sdk
258273
PUBLIC
259274
nakama::sdk-interface
@@ -281,12 +296,6 @@ if (APPLE)
281296
)
282297
endif()
283298

284-
if (BUILD_TESTING)
285-
# Make tests produce .exe in the same dir as our nakama-sdk.dll
286-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:nakama-sdk>)
287-
add_subdirectory(test)
288-
endif ()
289-
290299

291300
if (ADDRESS_SANITIZER)
292301
if (MSVC)

cmake/vcpkg-ports/wslay/msvc_compat2.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ index 7ab8501..82be717 100644
55
@@ -79,6 +79,14 @@ enum wslay_io_flags {
66
WSLAY_MSG_MORE = 1
77
};
8-
8+
99
+#ifdef _MSC_VER
1010
+#ifdef _WIN64
1111
+typedef int64_t ssize_t;

core/common/BaseClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
namespace Nakama {
2929

30-
#if defined(HAVE_DEFAULT_RT_TRANSPORT_FACTORY)
30+
#ifdef HAVE_DEFAULT_RT_TRANSPORT_FACTORY
3131
NRtClientPtr BaseClient::createRtClient() { return createRtClient(createDefaultWebsocket(_platformParams)); }
3232
#endif
3333

core/common/Resource.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
namespace Nakama {
4+
struct disarmed_t {};
5+
6+
inline constexpr disarmed_t disarmed{};
7+
8+
// like unique_ptr but for non-pointer types
9+
template <typename T, int (*d)(T)> class Resource {
10+
public:
11+
explicit Resource(T v) : r(v), armed(true) {}
12+
explicit Resource(disarmed_t) : r(), armed(false) {}
13+
~Resource() { armed ? d(r) : 0; }
14+
15+
Resource(Resource&) = delete;
16+
Resource(const Resource&) = delete;
17+
Resource& operator=(const Resource&) = delete;
18+
Resource& operator=(Resource&&) = delete;
19+
20+
Resource(Resource&& o) noexcept : r(o.r), armed(o.armed) {
21+
o.armed = false;
22+
o.r = T();
23+
}
24+
25+
explicit operator bool() const { return armed; }
26+
T& get() { return r; }
27+
28+
void reset(T v) {
29+
armed ? d(r) : 0;
30+
armed = true;
31+
r = v;
32+
}
33+
34+
void reset(disarmed_t) {
35+
armed ? d(r) : 0;
36+
armed = false;
37+
r = T();
38+
}
39+
40+
// Call this only after making sure the resource is invalid or you handle destruction manually. This will not call the
41+
// delete function on the resource!
42+
void disarm() { armed = false; }
43+
44+
private:
45+
T r;
46+
bool armed;
47+
};
48+
} // namespace Nakama

interface/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
#cmakedefine HAVE_DEFAULT_TRANSPORT_FACTORY
55
#cmakedefine HAVE_DEFAULT_RT_TRANSPORT_FACTORY
66
#cmakedefine FORCE_DLL_IMPORT_EXPORT
7+
#cmakedefine FORCE_DLL_VISIBILITY

interface/include/nakama-cpp/NClientInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class NAKAMA_API NClientInterface {
9393
*/
9494
virtual void tick() = 0;
9595

96-
#if defined(HAVE_DEFAULT_RT_TRANSPORT_FACTORY)
96+
#ifdef HAVE_DEFAULT_RT_TRANSPORT_FACTORY
9797
/**
9898
* Create a new real-time client with parameters from client.
9999
* @return a new NRtClient instance.

interface/include/nakama-cpp/NExport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#else
2828
#define NAKAMA_API __declspec(dllimport)
2929
#endif
30-
#elif __GNUC__ >= 4
30+
#elif __GNUC__ >= 4 || defined FORCE_DLL_VISIBILITY
3131
#ifdef NAKAMA_SHARED_LIBRARY_EXPORTS
3232
#define NAKAMA_API __attribute__((visibility("default")))
3333
#else

interface/include/nakama-cpp/NUtils.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616

1717
#pragma once
1818

19-
#include <nakama-cpp/NTypes.h>
2019
#include <nakama-cpp/NExport.h>
20+
#include <nakama-cpp/NTypes.h>
2121

2222
NAKAMA_NAMESPACE_BEGIN
2323

24-
/**
25-
* Get current UNIX time in milliseconds.
26-
*
27-
* Returns number of milliseconds that have elapsed since 00:00:00 Thursday, 1 January 1970.
28-
*
29-
* @return UNIX time in milliseconds.
30-
*/
31-
NAKAMA_API NTimestamp getUnixTimestampMs();
24+
/**
25+
* Get current UNIX time in milliseconds.
26+
*
27+
* Returns number of milliseconds that have elapsed since 00:00:00 Thursday, 1 January 1970.
28+
*
29+
* @return UNIX time in milliseconds.
30+
*/
31+
NAKAMA_API NTimestamp getUnixTimestampMs();
3232

3333
NAKAMA_NAMESPACE_END

submodules/devkits

submodules/private

0 commit comments

Comments
 (0)