Browse Source

change rustdecisondemo, for import proto ,not complete.

yuchuli 6 months ago
parent
commit
ff4509d871

+ 48 - 0
src/RUST/rustdecisiondemo/Cargo.lock

@@ -100,6 +100,12 @@ dependencies = [
  "either",
 ]
 
+[[package]]
+name = "lazy_static"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+
 [[package]]
 name = "libc"
 version = "0.2.155"
@@ -218,6 +224,26 @@ dependencies = [
  "prost",
 ]
 
+[[package]]
+name = "protobuf"
+version = "3.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df67496db1a89596beaced1579212e9b7c53c22dca1d9745de00ead76573d514"
+dependencies = [
+ "once_cell",
+ "protobuf-support",
+ "thiserror",
+]
+
+[[package]]
+name = "protobuf-support"
+version = "3.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70e2d30ab1878b2e72d1e2fc23ff5517799c9929e2cf81a8516f9f4dcf2b9cf3"
+dependencies = [
+ "thiserror",
+]
+
 [[package]]
 name = "quote"
 version = "1.0.36"
@@ -260,8 +286,10 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
 name = "rustdecisiondemo"
 version = "0.1.0"
 dependencies = [
+ "lazy_static",
  "prost",
  "prost-build",
+ "protobuf",
 ]
 
 [[package]]
@@ -300,6 +328,26 @@ dependencies = [
  "windows-sys",
 ]
 
+[[package]]
+name = "thiserror"
+version = "1.0.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.61"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "unicode-ident"
 version = "1.0.12"

+ 2 - 0
src/RUST/rustdecisiondemo/Cargo.toml

@@ -13,3 +13,5 @@ prost-build = "0.12"
 [dependencies]  
 
 prost = "0.12"  
+lazy_static = "1.4"
+protobuf= "3.5"

+ 6 - 0
src/RUST/rustdecisiondemo/Readme.md

@@ -2,3 +2,9 @@
 
 rust编译
 cargo rustc -- -L .   -lmodulecommrust
+
+
+protobuf:
+cargo install protobuf-codegen
+在.bashrc里增加
+export PATH=$PATH:/home/(user))/.cargo/bin

+ 10 - 0
src/RUST/rustdecisiondemo/build.rs

@@ -0,0 +1,10 @@
+
+
+fn main() {  
+
+    println!("cargo:rustc-link-search=native=./");  
+
+    println!("cargo:rustc-link-lib=modulecommrust"); // 假设你的.so文件名为libmylib.so  
+    println!("hello build");
+
+}

+ 0 - 6
src/RUST/rustdecisiondemo/src/build.rs

@@ -1,6 +0,0 @@
-fn main() {  
-
-    println!("hello build");
-
-
-}

+ 69 - 3
src/RUST/rustdecisiondemo/src/main.rs

@@ -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));

+ 1 - 1
src/RUST/rustdecisiondemo/src/modulecommrust.rs

@@ -6,7 +6,7 @@ extern "C" {
 
     pub fn RegisterSendRUST(strcommname: *const c_char, nsize: c_uint,npaccount: c_uint) -> *mut c_void; 
 
- //   pub fn RegisterRecvRUST(strcommname: *const c_char, pCall: extern "C" fn(*const c_char, c_uint, c_uint)) -> *mut c_void;  
+    pub fn RegisterRecvRUST(strcommname: *const c_char, pCall: extern "C" fn(*const c_char, c_uint, c_uint)) -> *mut c_void;  
 
     pub fn UnregisterRUST(instance: *mut c_void);