1
1
use std:: ffi:: * ;
2
2
use serde:: Deserialize ;
3
+ use std:: sync:: { LazyLock , Mutex } ;
3
4
4
5
mod extern_functions;
5
6
use extern_functions:: * ;
@@ -10,16 +11,16 @@ pub use error_codes::*;
10
11
mod string_utils;
11
12
use string_utils:: * ;
12
13
13
- static mut CALLBACK_FUNCTION : Option < CallbackType > = None ;
14
+ type LicenseCallback = dyn Fn ( LexActivatorCode ) + Send + ' static ;
14
15
15
- pub type CallbackType = extern "C" fn ( LexActivatorCode ) ;
16
+ static CALLBACK_FUNCTION : LazyLock < Mutex < Option < Box < LicenseCallback > > > > =
17
+ LazyLock :: new ( || Mutex :: new ( None ) ) ;
16
18
17
19
extern "C" fn wrapper ( code : i32 ) {
18
20
let callback_status = LexActivatorCode :: from_i32 ( code) ;
19
- unsafe {
20
- if let Some ( callback) = CALLBACK_FUNCTION {
21
- callback ( callback_status) ;
22
- }
21
+ let callback = CALLBACK_FUNCTION . lock ( ) . unwrap ( ) ;
22
+ if let Some ( callback) = callback. as_ref ( ) {
23
+ callback ( callback_status) ;
23
24
}
24
25
}
25
26
@@ -334,26 +335,28 @@ pub fn set_license_user_credential(email: String, password: String) -> Result<()
334
335
}
335
336
}
336
337
337
- /// Sets the license callback function .
338
- ///
339
- /// Whenever the server sync occurs in a separate thread, and server returns the response,
340
- /// license callback function gets invoked with the following status codes:
338
+ /// Sets the license closure callback .
339
+ ///
340
+ /// Whenever the server sync occurs in a separate thread, and server returns the response,
341
+ /// license closure callback gets invoked with the following status codes:
341
342
/// LA_OK, LA_EXPIRED, LA_SUSPENDED, LA_E_REVOKED, LA_E_ACTIVATION_NOT_FOUND, LA_E_MACHINE_FINGERPRINT
342
- /// LA_E_AUTHENTICATION_FAILED, LA_E_COUNTRY, LA_E_INET, LA_E_SERVER,LA_E_RATE_LIMIT, LA_E_IP,
343
+ /// LA_E_AUTHENTICATION_FAILED, LA_E_COUNTRY, LA_E_INET, LA_E_SERVER,LA_E_RATE_LIMIT, LA_E_IP,
343
344
/// LA_E_RELEASE_VERSION_NOT_ALLOWED, LA_E_RELEASE_VERSION_FORMAT
344
345
///
345
346
/// # Arguments
346
347
///
347
- /// * `callback ` - The callback function to be set.
348
+ /// * `closure ` - The closure callback to be set e.g. |code| { println!("{:?}", code) }
348
349
///
349
350
/// # Returns
350
351
///
351
- /// Returns `Ok(())` if the license callback is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
352
+ /// Returns `Ok(())` if the license closure callback is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
352
353
353
- pub fn set_license_callback ( callback : CallbackType ) -> Result < ( ) , LexActivatorError > {
354
- unsafe {
355
- CALLBACK_FUNCTION = Some ( callback) ;
356
- }
354
+ pub fn set_license_callback < F > ( closure : F ) -> Result < ( ) , LexActivatorError >
355
+ where
356
+ F : Fn ( LexActivatorCode ) + Clone + Send + ' static ,
357
+ {
358
+ let mut callback_function = CALLBACK_FUNCTION . lock ( ) . unwrap ( ) ;
359
+ callback_function. replace ( Box :: new ( closure) ) ;
357
360
let status: i32 = unsafe { SetLicenseCallback ( wrapper) } ;
358
361
359
362
if status == 0 {
@@ -363,6 +366,13 @@ pub fn set_license_callback(callback: CallbackType) -> Result<(), LexActivatorEr
363
366
}
364
367
}
365
368
369
+ /// Unset the current license closure callback.
370
+
371
+ pub fn unset_license_callback ( ) {
372
+ let mut callback_function = CALLBACK_FUNCTION . lock ( ) . unwrap ( ) ;
373
+ * callback_function = None ;
374
+ }
375
+
366
376
/// Sets the activation lease duration.
367
377
///
368
378
/// # Arguments
0 commit comments