VRay SDK for C++
Loading...
Searching...
No Matches
licensing.hpp
1#if !defined(VRAY_INTERNAL_LICENSING) || defined(VRAY_INTERNAL_GENERATE_IMPORT_CODE)
2
3#include <ctime>
4
5#ifndef VRAY_INTERNAL_LICENSING
6#define VRAY_INTERNAL_LICENSING
7#include "vraysdk.hpp"
8#endif
9
10#if !defined(VRAY_RUNTIME_LOAD_PRIMARY) && !defined(VRAY_RUNTIME_LOAD_SECONDARY)
11# define VRAYSDK_FUNC(ret, name, ...) extern ret name(__VA_ARGS__)
12# define VRAYSDK_FUNCT VRAYSDK_FUNC
13#else
14# ifdef VRAY_RUNTIME_LOAD_SECONDARY
15# define VRAYSDK_FUNC(ret, name, ...) extern ret (*name)(__VA_ARGS__)
16# define VRAYSDK_FUNCT VRAYSDK_FUNC
17# else
18# ifndef VRAY_INTERNAL_GENERATE_IMPORT_CODE
19# define VRAYSDK_FUNC(ret, name, ...) ret (*name)(__VA_ARGS__) = nullptr
20# define VRAYSDK_FUNCT VRAYSDK_FUNC
21# else
22# define VRAYSDK_FUNC(ret, name, ...) \
23 if (!(Internal::name = (decltype(Internal::name))Internal::GetProcAddress(hLib, #name))) \
24 PROC_IMPORT_FAIL(#name)
25# define VRAYSDK_FUNCT(ret, name, ...) \
26 Internal::name = (decltype(Internal::name))Internal::GetProcAddress(hLib, #name)
27# endif
28# endif
29#endif
30
31#ifndef VRAY_INTERNAL_GENERATE_IMPORT_CODE
32
33namespace VRay {
34
38 const char* token;
41 long long expiry;
42
44 int isValid() const {
45 return getRelativeExpiry() > 0;
46 }
47
49 long long getRelativeExpiry() const {
50 if (!token || !*token || expiry <= 0) {
51 return -1;
52 }
53 const time_t currentTime = std::time(nullptr);
54 return expiry - static_cast<long long>(currentTime);
55 }
56};
57
60 const char* email;
61 const char* uuid;
62 const char* firstName;
63 const char* lastName;
65};
66
70 std::string token;
73 long long expiry;
74
76 int isValid() const {
77 return getRelativeExpiry() > 0;
78 }
79
81 long long getRelativeExpiry() const {
82 if (token.empty() || expiry <= 0) {
83 return -1;
84 }
85 const time_t currentTime = std::time(nullptr);
86 return expiry - static_cast<long long>(currentTime);
87 }
88
89 AccessToken() : expiry() {}
90
91 AccessToken(const AccessTokenView& view) :
92 token(view.token ? view.token : ""), expiry(view.expiry) {}
93};
94
97 std::string email;
98 std::string uuid;
99 std::string firstName;
100 std::string lastName;
102
104 email(view.email ? view.email : ""),
105 uuid(view.uuid ? view.uuid : ""),
106 firstName(view.firstName ? view.firstName : ""),
107 lastName(view.lastName ? view.lastName : ""),
109
110 ActiveUserDetails() = default;
111};
112
113namespace Internal {
114
115#if !defined(VRAY_RUNTIME_LOAD_PRIMARY) && !defined(VRAY_RUNTIME_LOAD_SECONDARY)
116extern "C" {
117#endif
118
119typedef void (*UnifiedLoginClient_onLogin)(const ActiveUserDetailsView* userDetails, void* userData);
120typedef void (*UnifiedLoginClient_onLogout)(void* userData);
121
122#endif // !VRAY_INTERNAL_GENERATE_IMPORT_CODE
123
124VRAYSDK_FUNC(void*, UnifiedLoginClient_init, const char* newApplicationId, unsigned initFlags, void** errStrHandle);
125VRAYSDK_FUNC(Bool, UnifiedLoginClient_isInitialized);
126VRAYSDK_FUNC(int, UnifiedLoginClient_startEventServer, void** errStrHandle);
127VRAYSDK_FUNC(Bool, UnifiedLoginClient_isEventServerStarted);
128VRAYSDK_FUNC(int, UnifiedLoginClient_getEventServerPort);
129VRAYSDK_FUNC(void, UnifiedLoginClient_done);
130VRAYSDK_FUNC(int, UnifiedLoginClient_setOnLogInOut, UnifiedLoginClient_onLogin loginCallback, UnifiedLoginClient_onLogout logoutCallback, const void* userData);
131VRAYSDK_FUNC(int, UnifiedLoginClient_getActiveUserDetails, ActiveUserDetailsView* details, void** errStrHandle);
132VRAYSDK_FUNC(int, UnifiedLoginClient_loginSync, ActiveUserDetailsView* details, void** errStrHandle);
133VRAYSDK_FUNC(int, UnifiedLoginClient_login, void** errStrHandle);
134VRAYSDK_FUNC(int, UnifiedLoginClient_logoutSync, void** errStrHandle);
135VRAYSDK_FUNC(int, UnifiedLoginClient_logout, void** errStrHandle);
136VRAYSDK_FUNC(void, UnifiedLoginClient_setSyncTimeout, int timeoutMs);
137VRAYSDK_FUNC(int, UnifiedLoginClient_setCustomPrompt, const char *loginPrompt, void** errHandle);
138
139#if VRAY_AUTH_MORE_STUFF
140VRAYSDK_FUNC(int, iota_omega);
141VRAYSDK_FUNC(void, iota_zeta);
142VRAYSDK_FUNC(void, iota_rho, unsigned, unsigned);
143VRAYSDK_FUNC(void, xi, void(*)(const char**, const char**, const char**, const char**));
144
145VRAYSDK_FUNCT(void, nu_lambda, int type, const char* typeDetails);
146VRAYSDK_FUNCT(int, nu_rho, int updateParameters);
147VRAYSDK_FUNCT(int, nu_alpha);
148#endif
149
150#ifndef VRAY_INTERNAL_GENERATE_IMPORT_CODE
151
152#if !defined(VRAY_RUNTIME_LOAD_PRIMARY) && !defined(VRAY_RUNTIME_LOAD_SECONDARY)
153} // extern "C"
154#endif
155
156} // namespace Internal
157
158#ifdef VRAY_RUNTIME_LOAD_PRIMARY
159class ULAClient : private VRayInit {
160#else
162#endif // VRAY_RUNTIME_LOAD_PRIMARY
163 ULAClient() = delete;
164 ~ULAClient() = delete;
165
166public:
168 typedef void (*OnLogin)(const ActiveUserDetailsView* activeUserDetails, void* userData);
169 typedef void (*OnLogout)(void* userData);
170
172 enum Flags : unsigned {
174 flag_noEventServer = 1u << 0,
177 };
178
179#ifdef VRAY_RUNTIME_LOAD_PRIMARY
180
181#ifdef _MSC_VER
182# pragma warning(push)
183# pragma warning(disable: 4706) // "assignment within conditional expression" - for ULAClient::loadFunctions()
184#endif // _MSC_VER
185
186private:
187 void loadFunctions() {
188# define VRAY_INTERNAL_GENERATE_IMPORT_CODE
189# undef VRAYSDK_FUNCT
190# undef VRAYSDK_FUNC
191# include "licensing.hpp"
192# undef VRAY_INTERNAL_GENERATE_IMPORT_CODE
193 }
194
195#ifdef _MSC_VER
196# pragma warning(pop)
197#endif // _MSC_VER
198
199public:
200 static void load(VRayInit& vrayinit) {
201 static_cast<ULAClient&>(vrayinit).loadFunctions();
202 }
203#endif // VRAY_RUNTIME_LOAD_PRIMARY
204
212 static void* init(const char* newApplicationId, unsigned initFlags, std::string& error) {
213 void* errHandle;
214 void* handle = Internal::UnifiedLoginClient_init(newApplicationId, initFlags, &errHandle);
215 error = Internal::getCharStringContent(&errHandle);
216 Internal::releaseCharString(&errHandle);
217 return handle;
218 }
219
221 static bool isInitialized() {
222 return !!Internal::UnifiedLoginClient_isInitialized();
223 }
224
226 static bool startEventServer(std::string& error) {
227 void* errHandle;
228 int err = Internal::UnifiedLoginClient_startEventServer(&errHandle);
229 return returnErrorResult(err, &errHandle, error);
230 }
231
233 static bool isEventServerStarted() {
234 return !!Internal::UnifiedLoginClient_isEventServerStarted();
235 }
236
238 static void done() {
239 Internal::UnifiedLoginClient_done();
240 }
241
243 static int getEventServerPort() {
244 return Internal::UnifiedLoginClient_getEventServerPort();
245 }
246
249 static bool setOnLogInLogOut(OnLogin loginCallback, OnLogout logoutCallback, const void* userData = nullptr) {
250 return !Internal::UnifiedLoginClient_setOnLogInOut(loginCallback, logoutCallback, userData);
251 }
252
256 static bool getActiveUserDetails(ActiveUserDetails& userDetails, std::string& error) {
257 return returnActiveUserDetailsResult(
258 Internal::UnifiedLoginClient_getActiveUserDetails,
259 userDetails, error);
260 }
261
265 static bool loginSync(ActiveUserDetails& userDetails, std::string& error) {
266 return returnActiveUserDetailsResult(
267 Internal::UnifiedLoginClient_loginSync,
268 userDetails, error);
269 }
270
273 static bool login(std::string& error) {
274 void* errHandle;
275 int err = Internal::UnifiedLoginClient_login(&errHandle);
276 return returnErrorResult(err, &errHandle, error);
277 }
278
282 static bool logoutSync(std::string& error) {
283 void* errHandle;
284 int err = Internal::UnifiedLoginClient_logoutSync(&errHandle);
285 return returnErrorResult(err, &errHandle, error);
286 }
287
291 static bool logout(std::string& error) {
292 void* errHandle;
293 int err = Internal::UnifiedLoginClient_logout(&errHandle);
294 return returnErrorResult(err, &errHandle, error);
295 }
296
300 static void setSyncTimeout(int timeoutMs) {
301 Internal::UnifiedLoginClient_setSyncTimeout(timeoutMs);
302 }
303
307 static bool setCustomPrompt(const char *loginPrompt, std::string &error) {
308 void *errHandle;
309 const int err = Internal::UnifiedLoginClient_setCustomPrompt(loginPrompt, &errHandle);
310 return returnErrorResult(err, &errHandle, error);
311 }
312
313private:
314 static bool returnActiveUserDetailsResult(
315 int (*activeUserDetailsFunc)(ActiveUserDetailsView* details, void** errStrHandle),
316 ActiveUserDetails& userDetails, std::string& error)
317 {
319 void* errHandle;
320 int err = activeUserDetailsFunc(&view, &errHandle);
321
322 userDetails.email = Internal::getCharStringContent(&view.email);
323 Internal::releaseCharString(&view.email);
324 userDetails.uuid = Internal::getCharStringContent(&view.uuid);
325 Internal::releaseCharString(&view.uuid);
326 userDetails.firstName = Internal::getCharStringContent(&view.firstName);
327 Internal::releaseCharString(&view.firstName);
328 userDetails.lastName = Internal::getCharStringContent(&view.lastName);
329 Internal::releaseCharString(&view.lastName);
330 userDetails.userAccessToken.token = Internal::getCharStringContent(&view.userAccessToken.token);
331 Internal::releaseCharString(&view.userAccessToken.token);
332 userDetails.userAccessToken.expiry = view.userAccessToken.expiry;
333
334 return returnErrorResult(err, &errHandle, error);
335 }
336
337 static bool returnErrorResult(int err, void** errStrHandle, std::string& error) {
338 error = Internal::getCharStringContent(errStrHandle);
339 Internal::releaseCharString(errStrHandle);
340 return !err;
341 }
342};
343
344#undef VRAYSDK_FUNCT
345#undef VRAYSDK_FUNC
346
347} // namespace VRay
348
349#endif // !VRAY_INTERNAL_GENERATE_IMPORT_CODE
350
351#endif
Definition: licensing.hpp:161
void(* OnLogin)(const ActiveUserDetailsView *activeUserDetails, void *userData)
activeUserDetails will never be nullptr
Definition: licensing.hpp:168
static bool login(std::string &error)
Definition: licensing.hpp:273
static void setSyncTimeout(int timeoutMs)
Definition: licensing.hpp:300
static void * init(const char *newApplicationId, unsigned initFlags, std::string &error)
Definition: licensing.hpp:212
static bool loginSync(ActiveUserDetails &userDetails, std::string &error)
Definition: licensing.hpp:265
static bool setOnLogInLogOut(OnLogin loginCallback, OnLogout logoutCallback, const void *userData=nullptr)
Definition: licensing.hpp:249
static bool isInitialized()
Returns true if the main part of the client is initialized (the event server is not checked).
Definition: licensing.hpp:221
static bool setCustomPrompt(const char *loginPrompt, std::string &error)
Definition: licensing.hpp:307
static bool isEventServerStarted()
Returns true if the event server has been started and is listening on a valid port.
Definition: licensing.hpp:233
static void done()
Stops the event server and clears other internally held resources.
Definition: licensing.hpp:238
static bool logout(std::string &error)
Definition: licensing.hpp:291
static bool startEventServer(std::string &error)
A function for dedicated start of the event server, in case the delayetServer flag was used during in...
Definition: licensing.hpp:226
Flags
A collection of flags that may be used to modify the way the client is used and what resources it wil...
Definition: licensing.hpp:172
@ flag_delayedServer
Disables the usage of a listening event server. A client with disabled server may be used for active ...
Definition: licensing.hpp:175
@ flag_default
Default initialization. Suitable for a single global instance, because it starts a dedicated listenin...
Definition: licensing.hpp:173
@ flag_uniqueProcess
Delays the start of the event server, either until explicitly started or until a synchronous login or...
Definition: licensing.hpp:176
static bool getActiveUserDetails(ActiveUserDetails &userDetails, std::string &error)
Definition: licensing.hpp:256
static int getEventServerPort()
Returns the port number on which the event server is listening. Returns 0 if the server is not starte...
Definition: licensing.hpp:243
static bool logoutSync(std::string &error)
Definition: licensing.hpp:282
Basic structure storing a string-based access token with its expiry information.
Definition: licensing.hpp:68
std::string token
The actual access token. Can be an arbitrarily long string in any format. Currently used mostly for J...
Definition: licensing.hpp:70
int isValid() const
Returns true if the token is still valid. This includes non-empty check for the token contents and ex...
Definition: licensing.hpp:76
long long getRelativeExpiry() const
Returns the number of seconds until the token would expire. A negative value is returned if the token...
Definition: licensing.hpp:81
long long expiry
Definition: licensing.hpp:73
Basic structure storing a string-based access token with its expiry information.
Definition: licensing.hpp:36
const char * token
The actual access token. Can be an arbitrarily long string in any format. Currently used mostly for J...
Definition: licensing.hpp:38
int isValid() const
Returns true if the token is still valid. This includes non-empty check for the token contents and ex...
Definition: licensing.hpp:44
long long getRelativeExpiry() const
Returns the number of seconds until the token would expire. A negative value is returned if the token...
Definition: licensing.hpp:49
long long expiry
Definition: licensing.hpp:41
Struct with the most relevant details for the actively logged in user.
Definition: licensing.hpp:96
ActiveUserDetails(const ActiveUserDetailsView &view)
An access token that may be used for communication with other Chaos services.
Definition: licensing.hpp:103
std::string firstName
The user UUID (unique universal identifier). Should always be a valid non-empty string (for valid det...
Definition: licensing.hpp:99
AccessToken userAccessToken
The registered last name of the user. May be an empty string.
Definition: licensing.hpp:101
std::string uuid
Contains the user email. Should always be a valid non-empty string (for valid details).
Definition: licensing.hpp:98
std::string lastName
The registered first name of the user. May be an empty string.
Definition: licensing.hpp:100
Struct with the most relevant details for the actively logged in user.
Definition: licensing.hpp:59
const char * lastName
The registered first name of the user. May be an empty string.
Definition: licensing.hpp:63
const char * firstName
The user UUID (unique universal identifier). Should always be a valid non-empty string (for valid det...
Definition: licensing.hpp:62
const char * uuid
Contains the user email. Should always be a valid non-empty string (for valid details).
Definition: licensing.hpp:61
AccessTokenView userAccessToken
The registered last name of the user. May be an empty string.
Definition: licensing.hpp:64