Skip to content

Commit 197ac83

Browse files
committed
Add a c_api and build .so and .dll
1 parent 071a914 commit 197ac83

File tree

13 files changed

+107
-16
lines changed

13 files changed

+107
-16
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ jobs:
4545
name: comsrv
4646
path: |
4747
out/comsrv
48+
out/libcomsrv.so
4849
out/comsrv.exe
50+
out/comsrv.dll
51+
out/comsrv.dll.lib
4952
out/*.whl

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [2.3.1] - unreleased
8+
9+
### Added
10+
11+
- Compile a C library for embedding in other application
12+
713
## [2.3.0] - 2024-04-23
814

915
### Added

Cargo.lock

+24-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ci/build-ci.sh

+3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ rm -rf target
1010
mv /cache/windows-release-cache target
1111
cargo build --release --target x86_64-pc-windows-msvc
1212
cp target/x86_64-pc-windows-msvc/release/comsrv.exe out
13+
cp target/x86_64-pc-windows-msvc/release/comsrv.dll out
14+
cp target/x86_64-pc-windows-msvc/release/comsrv.dll.lib out
1315

1416
rm -rf target
1517
mv /cache/linux-release-cache target
1618
cargo build --release
1719
cp target/release/comsrv out
20+
cp target/release/libcomsrv.so out

client/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "comsrv_client"
3-
version = "2.3.0"
3+
version = "2.3.1"
44
edition = "2018"
55
license = "MIT OR Apache-2.0"
66

comsrv/Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
[package]
22
name = "comsrv"
3-
version = "2.3.0"
3+
version = "2.3.1"
44
authors = ["Raphael Bernhard <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
77

8+
[lib]
9+
crate-type = ["rlib", "cdylib"]
10+
11+
[[bin]]
12+
name = "comsrv"
13+
path = "src/main.rs"
14+
815
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
916

1017
[dependencies]
@@ -23,7 +30,7 @@ tokio = { version = "^1", features = [
2330
"macros",
2431
"rt-multi-thread",
2532
] }
26-
broadcast_wsrpc = { git = "https://github.com/raffber/wsrpc.git", rev = "9565f8f03502e93c21e5159fba48e2779a429b2d" }
33+
broadcast_wsrpc = { git = "https://github.com/raffber/wsrpc.git", rev = "6c5fc98d0a51f2517e26d82dc8d9bb2343c02255" }
2734
clap = { version = "3", features = ["cargo"] }
2835
env_logger = "0.7.1"
2936
log = "0.4.11"

comsrv/src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl App {
150150
Request::DropAll => self.drop_all().await,
151151
Request::Shutdown => {
152152
let _ = self.drop_all();
153-
self.server.shutdown().await;
153+
self.server.shutdown();
154154
Ok(Response::Done)
155155
}
156156
Request::ListHidDevices => hid::list_devices().await.map(|x| Response::Hid(HidResponse::List(x))),

comsrv/src/c_api.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![allow(unsafe_code)]
2+
3+
use std::{net::SocketAddr, time::Duration};
4+
5+
use tokio::runtime::Runtime;
6+
7+
use crate::app::App;
8+
9+
#[repr(C)]
10+
pub struct AppState {
11+
app: *const App,
12+
}
13+
14+
#[no_mangle]
15+
pub extern "C" fn comsrv_spawn() -> AppState {
16+
let port = 5902;
17+
let (tx, rx) = std::sync::mpsc::channel();
18+
19+
let rt = Runtime::new().unwrap();
20+
rt.block_on(async move {
21+
let (app, rx) = App::new();
22+
let app = Box::new(app);
23+
let ws_addr: SocketAddr = format!("127.0.0.1:{}", port).parse().unwrap();
24+
let http_addr: SocketAddr = format!("127.0.0.1:{}", port + 1).parse().unwrap();
25+
println!("Listening on ws://{}", ws_addr);
26+
println!("Listening on http://{}", http_addr);
27+
app.server.listen_ws(&ws_addr).await.unwrap();
28+
app.server.listen_http(&http_addr).await;
29+
30+
let ret = AppState {
31+
app: app.as_ref() as *const App,
32+
};
33+
34+
tx.send(ret).unwrap();
35+
36+
app.run(rx).await;
37+
std::mem::forget(app);
38+
log::debug!("Application quitting.");
39+
});
40+
41+
return rx.recv_timeout(Duration::from_secs(1)).expect("Failed to start comsrv");
42+
}
43+
44+
#[no_mangle]
45+
pub extern "C" fn comsrv_stop(state: AppState) {
46+
let app: &App = unsafe { &*state.app };
47+
app.server.shutdown();
48+
log::debug!("Application stopping.");
49+
}

comsrv/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate lazy_static;
88
pub use comsrv_protocol::{Request, Response};
99

1010
pub mod app;
11+
pub mod c_api;
1112
mod inventory;
1213
mod iotask;
1314
mod protocol;

comsrv/src/main.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ fn main() {
4949
rt.block_on(async move {
5050
let (app, rx) = App::new();
5151
app.server.enable_broadcast_reqrep(broadcast_reqrep);
52-
let url = format!("0.0.0.0:{}", port);
52+
53+
let ws_addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
5354
let http_addr: SocketAddr = format!("0.0.0.0:{}", port + 1).parse().unwrap();
54-
println!("Listening on ws://{}", url);
55-
app.server.listen_ws(url).await;
55+
println!("Listening on ws://{}", ws_addr);
5656
println!("Listening on http://{}", http_addr);
57-
app.server.listen_http(http_addr).await;
57+
58+
app.server.listen_ws(&ws_addr).await.expect("Failed to listen on WebSocket");
59+
app.server.listen_http(&http_addr).await;
60+
5861
app.run(rx).await;
5962
log::debug!("Application quitting.");
6063
});

dart/comsrv/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: comsrv
22
description: Client implementation for comsrv
3-
version: 2.3.0
3+
version: 2.3.1
44
homepage: https://github.com/raffber/comsrv
55
publish_to: none
66

protocol/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "comsrv_protocol"
3-
version = "2.3.0"
3+
version = "2.3.1"
44
authors = ["Raphael Bernhard <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "comsrv"
3-
version = "2.3.0"
3+
version = "2.3.1"
44
description = ""
55
authors = ["Raphael Bernhard <[email protected]>"]
66
packages = [{ include = "comsrv", from = "python" }]

0 commit comments

Comments
 (0)