|
@@ -1,15 +1,76 @@
|
|
|
mod modulecommrust;
|
|
|
|
|
|
+mod proto;
|
|
|
+use proto::mapdata::Tracemap;
|
|
|
+use protobuf::Message;
|
|
|
+extern crate protobuf;
|
|
|
|
|
|
use std::os::raw::{c_char, c_uint};
|
|
|
use std::thread;
|
|
|
use std::time::Duration;
|
|
|
+
|
|
|
+use lazy_static::lazy_static;
|
|
|
+
|
|
|
+
|
|
|
+struct adcdes{
|
|
|
+ xtr:Tracemap
|
|
|
+}
|
|
|
+
|
|
|
+lazy_static! {
|
|
|
+
|
|
|
+ static ref DES:adcdes = adcdes{xtr:Tracemap::new()};
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
// 定义Rust中的回调函数,与C/C++中的SMCallBack兼容
|
|
|
|
|
|
|
|
|
-//extern "C" fn rust_sm_callback(strdata: *const c_char, nsize: c_uint, index: c_uint) {
|
|
|
-// println!("Callback received: (size: {}, index: {})", nsize, index);
|
|
|
-//}
|
|
|
+// 假设nSize是已知的,并且c_str指向一个长度为nSize的数组
|
|
|
+fn c_str_to_bytes(c_str: *const c_char, nsize: usize) -> Vec<u8> {
|
|
|
+ // 由于c_char可能实际上是i8的别名,但在这里我们处理字节,所以我们将其视为*const u8
|
|
|
+ // 如果c_char在你的环境中真的是i8的别名,并且你确信数据实际上是u8的,你可以安全地进行这个转换
|
|
|
+ // 否则,你可能需要确保你的环境或库的使用是一致的
|
|
|
+ let c_str_as_u8: *const u8 = c_str as *const u8;
|
|
|
+
|
|
|
+ // 使用slice::from_raw_parts来创建一个指向原始数据的字节切片
|
|
|
+ // 注意:这个切片是借用的,并且它的生命周期与原始指针相同
|
|
|
+ // 因此,确保在原始数据被释放或修改之前不要使用它
|
|
|
+ let slice = unsafe { std::slice::from_raw_parts(c_str_as_u8, nsize) };
|
|
|
+
|
|
|
+ // 将切片转换为Vec<u8>,这会复制数据
|
|
|
+ slice.to_vec()
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+extern "C" fn tracemap_callback(strdata: *const c_char, nsize: c_uint, index: c_uint) {
|
|
|
+ println!("Callback received: (size: {}, index: {})", nsize, index);
|
|
|
+ let n_size: usize = nsize as usize;
|
|
|
+ let bytes = c_str_to_bytes(strdata, n_size);
|
|
|
+ // let xtracemap = proto::mapdata::Tracemap::parse_from_bytes(&bytes).unwrap();
|
|
|
+ match proto::mapdata::Tracemap::parse_from_bytes(&bytes) {
|
|
|
+
|
|
|
+ Ok(xtracemap) => {
|
|
|
+ let xdes = adcdes{xtr:xtracemap};
|
|
|
+ println!("receive new map Size: {}", xdes.xtr.point.len());
|
|
|
+
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ Err(e) => {
|
|
|
+
|
|
|
+ eprintln!("Error parsing tracemap protobuf: {:?}", e);
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
fn main() {
|
|
|
println!("Hello, world!");
|
|
@@ -22,6 +83,11 @@ fn main() {
|
|
|
|
|
|
let _pa = unsafe { modulecommrust::RegisterSendRUST(c_commname.as_ptr(), nsize, npaccount) };
|
|
|
|
|
|
+ let strmapmsgname = "newtracemap";
|
|
|
+ let c_strmapmsgname = std::ffi::CString::new(strmapmsgname).unwrap();
|
|
|
+
|
|
|
+ let _pamap = unsafe { modulecommrust::RegisterRecvRUST(c_strmapmsgname.as_ptr(), tracemap_callback) };
|
|
|
+
|
|
|
loop {
|
|
|
|
|
|
thread::sleep(Duration::from_millis(10));
|