ext.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c)2021 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. use std::ffi::CStr;
  13. use std::os::raw::c_char;
  14. use crate::NetworkJoinedParams;
  15. use crate::SmeeClient;
  16. #[no_mangle]
  17. pub extern "C" fn smee_client_new(
  18. temporal_url: *const c_char,
  19. namespace: *const c_char,
  20. task_queue: *const c_char,
  21. ) -> *mut SmeeClient {
  22. let url = unsafe {
  23. assert!(!temporal_url.is_null());
  24. CStr::from_ptr(temporal_url).to_str().unwrap()
  25. };
  26. let ns = unsafe {
  27. assert!(!namespace.is_null());
  28. CStr::from_ptr(namespace).to_str().unwrap()
  29. };
  30. let tq = unsafe {
  31. assert!(!task_queue.is_null());
  32. CStr::from_ptr(task_queue).to_str().unwrap()
  33. };
  34. match SmeeClient::new(url, ns, tq) {
  35. Ok(c) => Box::into_raw(Box::new(c)),
  36. Err(e) => {
  37. println!("error creating smee client instance: {}", e);
  38. std::ptr::null_mut()
  39. }
  40. }
  41. }
  42. #[no_mangle]
  43. pub extern "C" fn smee_client_delete(ptr: *mut SmeeClient) {
  44. if ptr.is_null() {
  45. return;
  46. }
  47. let smee = unsafe {
  48. assert!(!ptr.is_null());
  49. Box::from_raw(&mut *ptr)
  50. };
  51. smee.shutdown();
  52. }
  53. #[no_mangle]
  54. pub extern "C" fn smee_client_notify_network_joined(
  55. smee_instance: *mut SmeeClient,
  56. network_id: *const c_char,
  57. member_id: *const c_char,
  58. ) -> bool {
  59. let nwid = unsafe {
  60. assert!(!network_id.is_null());
  61. CStr::from_ptr(network_id).to_str().unwrap()
  62. };
  63. let mem_id = unsafe {
  64. assert!(!member_id.is_null());
  65. CStr::from_ptr(member_id).to_str().unwrap()
  66. };
  67. let smee = unsafe {
  68. assert!(!smee_instance.is_null());
  69. &mut *smee_instance
  70. };
  71. let params = NetworkJoinedParams::new(nwid, mem_id);
  72. match smee.notify_network_joined(params) {
  73. Ok(()) => true,
  74. Err(e) => {
  75. println!("error notifying network joined: {0}", e.to_string());
  76. false
  77. }
  78. }
  79. }