diff --git a/Cargo.lock b/Cargo.lock index 3bac2b0..260aedb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,8 +45,7 @@ dependencies = [ "encoding_rs", "flate2", "futures-core", - "h2", - "http", + "http 0.2.12", "httparse", "httpdate", "itoa", @@ -61,7 +60,6 @@ dependencies = [ "tokio", "tokio-util", "tracing", - "zstd", ] [[package]] @@ -120,7 +118,7 @@ checksum = "13d324164c51f63867b57e73ba5936ea151b8a41a1d23d1031eeb9f70d0236f8" dependencies = [ "bytestring", "cfg-if", - "http", + "http 0.2.12", "regex", "regex-lite", "serde", @@ -205,7 +203,6 @@ dependencies = [ "mime", "once_cell", "pin-project-lite", - "regex", "regex-lite", "serde", "serde_json", @@ -534,11 +531,6 @@ name = "cc" version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" -dependencies = [ - "jobserver", - "libc", - "once_cell", -] [[package]] name = "cfg-if" @@ -740,10 +732,16 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + [[package]] name = "ddddocr" version = "0.3.0" -source = "git+https://github.com/86maid/ddddocr.git?branch=master#e48666cbf619ea314f47c7ee001f1290b9c86a0e" +source = "git+https://github.com/86maid/ddddocr.git?branch=master#56b94a417374a836cfd6d0becc39dfe6d479f52f" dependencies = [ "actix-multipart", "actix-web", @@ -763,11 +761,14 @@ dependencies = [ [[package]] name = "ddddocr_rust_dll" -version = "0.1.0" +version = "0.1.1" dependencies = [ "bson", "ddddocr", + "lazy_static", "serde_json", + "tokio", + "tungstenite", ] [[package]] @@ -1050,7 +1051,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.12", "indexmap", "slab", "tokio", @@ -1103,6 +1104,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -1110,7 +1122,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.12", "pin-project-lite", ] @@ -1137,7 +1149,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.12", "http-body", "httparse", "httpdate", @@ -1252,15 +1264,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" -[[package]] -name = "jobserver" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e" -dependencies = [ - "libc", -] - [[package]] name = "jpeg-decoder" version = "0.3.1" @@ -1899,7 +1902,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.12", "http-body", "hyper", "hyper-tls", @@ -2467,6 +2470,24 @@ version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd" +[[package]] +name = "tungstenite" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e2ce1e47ed2994fd43b04c8f618008d4cabdd5ee34027cf14f9d918edd9c8" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http 1.1.0", + "httparse", + "log", + "rand 0.8.5", + "sha1", + "thiserror", + "utf-8", +] + [[package]] name = "typenum" version = "1.17.0" @@ -2505,6 +2526,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8parse" version = "0.2.2" @@ -2877,34 +2904,6 @@ dependencies = [ "time 0.1.45", ] -[[package]] -name = "zstd" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a" -dependencies = [ - "zstd-safe", -] - -[[package]] -name = "zstd-safe" -version = "7.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a" -dependencies = [ - "zstd-sys", -] - -[[package]] -name = "zstd-sys" -version = "2.0.10+zstd.1.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c253a4914af5bafc8fa8c86ee400827e83cf6ec01195ec1f1ed8441bf00d65aa" -dependencies = [ - "cc", - "pkg-config", -] - [[package]] name = "zune-inflate" version = "0.2.54" diff --git a/Cargo.toml b/Cargo.toml index ef2b0bd..2780b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ddddocr_rust_dll" -version = "0.1.0" +version = "0.1.1" edition = "2021" [lib] crate-type = ["cdylib"] @@ -8,4 +8,7 @@ name = "ddddocr" [dependencies] ddddocr = { git = "https://github.com/86maid/ddddocr.git", branch = "master" } bson = "2.11.0" -serde_json = "1.0.117" \ No newline at end of file +serde_json = "1.0.117" +lazy_static = "1.4.0" +tungstenite = "0.23.0" +tokio = "1.38.0" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index edddcf4..4544d00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,13 @@ -use std::ffi::CString; -use std::os::raw::{c_char, c_void}; +use std::ffi::{CStr, CString}; +use std::fs::File; +use std::io::Read; +use std::os::raw::{c_char, c_int, c_uchar, c_void}; use std::result::Result; use std::vec::Vec; use bson; use ddddocr::Ddddocr; use serde_json; +use std::sync::{Arc, Mutex}; //初始化ocr识别 #[no_mangle] pub extern "stdcall" fn initialize_OCR() -> *mut c_void { @@ -136,7 +139,7 @@ pub extern "stdcall" fn slideral_gorithm_two_slide_comparison(target: *const u8, } } #[no_mangle] -pub extern "stdcall" fn freee(ptr: *mut c_void) { +pub extern "stdcall" fn rust_free(ptr: *mut c_void) { if ptr.is_null() { return; } @@ -180,3 +183,81 @@ fn bson_to_json_string(data: &[u8]) -> Result } +//回调函数示例! +type CallbackFn = extern "stdcall" fn(int_param: c_int, str_param: *const c_char); +//callback传递易语言函数地址指针 到整数 (&回调) 回调为子程序 这个子程序2个参数一个是整数型一个是文本型 +#[no_mangle] +pub extern "stdcall" fn set_callback_and_call(callback: CallbackFn) { + let int_param: c_int = 42; // 示例整数参数 + let str_param = CString::new("Hello from Rust").expect("CString::new failed"); + // 调用传递过来的回调函数 + callback(int_param, str_param.as_ptr()); +} + +//callback2传递易语言函数地址指针 到整数 (&回调) 回调为子程序 这个子程序2个参数都是整数型 指针到字节集 (bin, len)取回字节集 +//这里是循环回调数据用loop循环往易语言传回字节集和字节集长度 +type CallbackFn2 = extern "stdcall" fn(byte_ptr: *const std::ffi::c_uchar, byte_len: std::ffi::c_int); + +#[no_mangle] +pub extern "stdcall" fn set_callback_and_call2(callback: CallbackFn2) { + loop{ + // 示例文本字符串 + let text = "Hello from Rust"; + // 将字符串转换为字节数组 + let byte_array = text.as_bytes(); + let byte_len: std::ffi::c_int = byte_array.len() as std::ffi::c_int; + // 调用传递过来的回调函数 + callback(byte_array.as_ptr(), byte_len); + + } + +} +//callback3传递易语言函数地址指针 到整数 (&回调) 回调为子程序 这个子程序2个参数都是整数型 指针到字节集 (bin, len)取回字节集 +type CallbackFn3 = extern "stdcall" fn(byte_ptr: *const c_uchar, byte_len: c_int); + +lazy_static::lazy_static! { + static ref BUFFER: Arc>> = Arc::new(Mutex::new(Vec::new())); +} +// +#[no_mangle] +pub extern "stdcall" fn set_callback_and_call3(callback: CallbackFn3, file_path: *const c_char) { + loop{ + + // 将传入的 C 字符串转换为 Rust 字符串 + let c_str = unsafe { CStr::from_ptr(file_path) }; + let file_path = c_str.to_str().expect("Invalid UTF-8 string"); + // 打开并读取图片文件 + let mut file = File::open(file_path).expect("Failed to open file"); + let buffer = BUFFER.clone(); + let mut buffer_guard = buffer.lock().unwrap(); + buffer_guard.clear(); + file.read_to_end(&mut buffer_guard).expect("Failed to read file"); + let byte_len: c_int = buffer_guard.len() as c_int; + // 使用 Box 包装回调函数,确保其生命周期长于调用 + let boxed_callback = Box::new(callback); + // 调用传递过来的回调函数 + boxed_callback(buffer_guard.as_ptr(), byte_len); + // 回调函数退出作用域后会自动释放内存 + + } + +} + +//这个示例是易语言传递一个空白文本例如:str = 取空白文本 (300) 调用e_redirect (str) 调试输出 (str) 可以取回结果11111 +#[no_mangle] +pub extern "stdcall" fn e_redirect(buffer: *mut c_char) { + // 创建一个Rust字符串 + let rust_string = "11111"; + // 将Rust字符串转换为C风格的字符串(包括空终止符) + let c_string = CString::new(rust_string).unwrap(); + // 获取C风格字符串的字节切片(包括空终止符) + let bytes = c_string.as_bytes_with_nul(); + // 安全地将字节复制到易语言提供的缓冲区中 + unsafe { + std::ptr::copy_nonoverlapping(bytes.as_ptr() as *const _, buffer as *mut _, bytes.len()); + } + // Rust字符串和C风格字符串的内存管理由Rust和CString自动处理 + // 我们不需要在这里手动释放任何内存,因为C字符串的生命周期与Rust字符串绑定 + // 易语言负责提供的缓冲区的内存管理 +} +