Skip to content

Commit

Permalink
Merge pull request #20 from cryptlex/ahmad/fix-license-callback
Browse files Browse the repository at this point in the history
fix: license callback crash
  • Loading branch information
adnan-kamili authored Sep 4, 2024
2 parents 9d4d7be + 718ce67 commit 1da08a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,14 @@ impl From<NulError> for LexActivatorError {
pub enum LexActivatorCode {
Status(LexActivatorStatus),
Error(LexActivatorError),
}

impl LexActivatorCode {
pub fn from_i32(code: i32) -> Self {
match code {
0..=32 => LexActivatorCode::Status(LexActivatorStatus::from(code)),
40..=104 => LexActivatorCode::Error(LexActivatorError::from(code)),
_ => LexActivatorCode::Error(LexActivatorError::LA_E_CLIENT), // Fallback to a general error
}
}
}
2 changes: 1 addition & 1 deletion src/extern_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro_rules! strtype {
}
}

pub type CallbackType = extern "C" fn(LexActivatorCode);
pub type CallbackType = extern "C" fn(i32);

extern "C" {
// --------------- Setter Functions ---------------
Expand Down
18 changes: 16 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ pub use error_codes::*;
mod string_utils;
use string_utils::*;

static mut CALLBACK_FUNCTION: Option<CallbackType> = None;

pub type CallbackType = extern "C" fn(LexActivatorCode);

extern "C" fn wrapper(code: i32) {
let callback_status = LexActivatorCode::from_i32(code);
unsafe {
if let Some(callback) = CALLBACK_FUNCTION {
callback(callback_status);
}
}
}

/// Represents a license meter attribute.
#[derive(Debug)]
Expand Down Expand Up @@ -340,8 +351,11 @@ pub fn set_license_user_credential(email: String, password: String) -> Result<()
/// Returns `Ok(())` if the license callback is set successfully, If an error occurs, an `Err` containing the `LexActivatorError`is returned.
pub fn set_license_callback(callback: CallbackType) -> Result<(), LexActivatorError> {
let status: i32;
status = unsafe { SetLicenseCallback(callback) };
unsafe {
CALLBACK_FUNCTION = Some(callback);
}
let status: i32 = unsafe { SetLicenseCallback(wrapper) };

if status == 0 {
Ok(())
} else {
Expand Down

0 comments on commit 1da08a4

Please sign in to comment.