aboutsummaryrefslogtreecommitdiff
path: root/core/bindings/lang
diff options
context:
space:
mode:
author1992414357@qq.com <1992414357@qq.com>2025-06-16 06:33:12 +0800
committer1992414357@qq.com <1992414357@qq.com>2025-06-16 06:33:12 +0800
commitbb292eda14b13582b6d6f9a628495e9a0cb283f2 (patch)
tree64c37b5dd24e56eed03d734f02a76caa49197e6a /core/bindings/lang
parent29f22c54213dfda89cb825962045295cb82af792 (diff)
Update CBindings of TcpNetworkService
Diffstat (limited to 'core/bindings/lang')
-rw-r--r--core/bindings/lang/clang/headers/nogamepads_data.h32
-rw-r--r--core/bindings/lang/clang/src/service/ngpd_tcp_service.rs140
2 files changed, 90 insertions, 82 deletions
diff --git a/core/bindings/lang/clang/headers/nogamepads_data.h b/core/bindings/lang/clang/headers/nogamepads_data.h
index dde4f43..91a07d9 100644
--- a/core/bindings/lang/clang/headers/nogamepads_data.h
+++ b/core/bindings/lang/clang/headers/nogamepads_data.h
@@ -545,12 +545,12 @@ void free_ffi_service_type(enum FfiServiceType *ptr);
/**
* Build tcp client
*/
-struct FfiTcpClientService tcp_client_build(struct FfiControllerRuntime *runtime);
+struct FfiTcpClientService *tcp_client_build(struct FfiControllerRuntime *runtime);
/**
* Bind ipv4 address
*/
-void tcp_client_bind_ipv4(struct FfiTcpClientService service,
+void tcp_client_bind_ipv4(struct FfiTcpClientService *service,
uint8_t a0,
uint8_t a1,
uint8_t a2,
@@ -559,17 +559,17 @@ void tcp_client_bind_ipv4(struct FfiTcpClientService service,
/**
* Bind ipv6 address
*/
-bool tcp_client_bind_ipv6(struct FfiTcpClientService service, const char *ip_str);
+bool tcp_client_bind_ipv6(struct FfiTcpClientService *service, const char *ip_str);
/**
* Bind port
*/
-void tcp_client_bind_port(struct FfiTcpClientService service, uint16_t port);
+void tcp_client_bind_port(struct FfiTcpClientService *service, uint16_t port);
/**
* Bind address with ipv4
*/
-void tcp_client_bind_address_v4(struct FfiTcpClientService service,
+void tcp_client_bind_address_v4(struct FfiTcpClientService *service,
uint8_t a0,
uint8_t a1,
uint8_t a2,
@@ -579,29 +579,29 @@ void tcp_client_bind_address_v4(struct FfiTcpClientService service,
/**
* Bind address with ipv6
*/
-bool tcp_client_bind_address_v6(struct FfiTcpClientService service,
+bool tcp_client_bind_address_v6(struct FfiTcpClientService *service,
const char *ip_str,
uint16_t port);
/**
* Connect
*/
-void tcp_client_connect(struct FfiTcpClientService service);
+void tcp_client_connect(struct FfiTcpClientService *service);
/**
* Free tcp client
*/
-void free_tcp_client(struct FfiTcpClientService service);
+void free_tcp_client(struct FfiTcpClientService *service);
/**
* Build tcp server
*/
-struct FfiTcpServerService tcp_server_build(struct FfiGameRuntime *runtime);
+struct FfiTcpServerService *tcp_server_build(struct FfiGameRuntime *runtime);
/**
* Bind ipv4 address
*/
-void tcp_server_bind_ipv4(struct FfiTcpServerService service,
+void tcp_server_bind_ipv4(struct FfiTcpServerService *service,
uint8_t a0,
uint8_t a1,
uint8_t a2,
@@ -610,17 +610,17 @@ void tcp_server_bind_ipv4(struct FfiTcpServerService service,
/**
* Bind ipv6 address
*/
-bool tcp_server_bind_ipv6(struct FfiTcpServerService service, const char *ip_str);
+bool tcp_server_bind_ipv6(struct FfiTcpServerService *service, const char *ip_str);
/**
* Bind port
*/
-void tcp_server_bind_port(struct FfiTcpServerService service, uint16_t port);
+void tcp_server_bind_port(struct FfiTcpServerService *service, uint16_t port);
/**
* Bind address with ipv4
*/
-void tcp_server_bind_address_v4(struct FfiTcpServerService service,
+void tcp_server_bind_address_v4(struct FfiTcpServerService *service,
uint8_t a0,
uint8_t a1,
uint8_t a2,
@@ -630,19 +630,19 @@ void tcp_server_bind_address_v4(struct FfiTcpServerService service,
/**
* Bind address with ipv6
*/
-bool tcp_server_bind_address_v6(struct FfiTcpServerService service,
+bool tcp_server_bind_address_v6(struct FfiTcpServerService *service,
const char *ip_str,
uint16_t port);
/**
* Start listening
*/
-void tcp_server_listening_block_on(struct FfiTcpServerService service);
+void tcp_server_listening_block_on(struct FfiTcpServerService *service);
/**
* Free tcp server
*/
-void free_tcp_server(struct FfiTcpServerService service);
+void free_tcp_server(struct FfiTcpServerService *service);
#ifdef __cplusplus
} // extern "C"
diff --git a/core/bindings/lang/clang/src/service/ngpd_tcp_service.rs b/core/bindings/lang/clang/src/service/ngpd_tcp_service.rs
index f80d5ea..d0bf1f4 100644
--- a/core/bindings/lang/clang/src/service/ngpd_tcp_service.rs
+++ b/core/bindings/lang/clang/src/service/ngpd_tcp_service.rs
@@ -1,12 +1,12 @@
-use std::ffi::{c_char, c_void, CStr};
-use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
-use std::sync::{Arc, Mutex};
+use crate::data::ngpd_controller::FfiControllerRuntime;
+use crate::data::ngpd_game::FfiGameRuntime;
use nogamepads_core::data::controller::controller_runtime::ControllerRuntime;
use nogamepads_core::data::game::game_runtime::GameRuntime;
use nogamepads_core::service::tcp_network::pad_client::pad_client_service::PadClientNetwork;
use nogamepads_core::service::tcp_network::pad_server::pad_server_service::PadServerNetwork;
-use crate::data::ngpd_controller::FfiControllerRuntime;
-use crate::data::ngpd_game::FfiGameRuntime;
+use std::ffi::{c_char, c_void, CStr};
+use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
+use std::sync::{Arc, Mutex};
#[repr(C)]
pub struct FfiTcpClientService(*mut c_void);
@@ -16,35 +16,37 @@ pub struct FfiTcpServerService(*mut c_void);
impl FfiTcpClientService {
- fn as_inner(&self) -> &mut PadClientNetwork {
- unsafe { &mut *(self.0 as *mut PadClientNetwork) }
- }
-
/// Build tcp client
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_build(
runtime: *mut FfiControllerRuntime,
- ) -> FfiTcpClientService {
+ ) -> *mut FfiTcpClientService {
+
+ if runtime.is_null() {
+ return std::ptr::null_mut();
+ }
let arc_ptr = unsafe { (*runtime).inner as *const Arc<Mutex<ControllerRuntime>> };
let arc_clone: Arc<Mutex<ControllerRuntime>> = unsafe { (&*arc_ptr).clone() };
- let service = Box::new(PadClientNetwork::build(arc_clone));
- let ptr = Box::into_raw(service);
+ let service = PadClientNetwork::build(arc_clone);
+ let raw = Box::into_raw(Box::new(FfiTcpClientService(Box::into_raw(Box::new(service)) as *mut _)));
- FfiTcpClientService(ptr as *mut c_void)
+ raw
}
/// Bind ipv4 address
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_bind_ipv4(
- service: FfiTcpClientService,
+ service: *mut FfiTcpClientService,
a0: u8,
a1: u8,
a2: u8,
a3: u8
) {
- let inner = service.as_inner();
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
let ip = IpAddr::V4(Ipv4Addr::new(a0, a1, a2, a3));
inner.bind_ip(ip);
}
@@ -52,14 +54,17 @@ impl FfiTcpClientService {
/// Bind ipv6 address
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_bind_ipv6(
- service: FfiTcpClientService,
+ service: *mut FfiTcpClientService,
ip_str: *const c_char
) -> bool {
+
+ if service.is_null() { return false; }
+
if ip_str.is_null() {
return false;
}
- let inner = service.as_inner();
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
let c_str = unsafe { CStr::from_ptr(ip_str) };
if let Ok(ip_str) = c_str.to_str() {
@@ -75,23 +80,28 @@ impl FfiTcpClientService {
/// Bind port
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_bind_port(
- service: FfiTcpClientService,
+ service: *mut FfiTcpClientService,
port: u16
) {
- service.as_inner().bind_port(port);
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
+ inner.bind_port(port);
}
/// Bind address with ipv4
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_bind_address_v4(
- service: FfiTcpClientService,
+ service: *mut FfiTcpClientService,
a0: u8,
a1: u8,
a2: u8,
a3: u8,
port: u16
) {
- let inner = service.as_inner();
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
let addr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(a0, a1, a2, a3)),
port
@@ -103,15 +113,16 @@ impl FfiTcpClientService {
/// Bind address with ipv6
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_bind_address_v6(
- service: FfiTcpClientService,
+ service: *mut FfiTcpClientService,
ip_str: *const c_char,
port: u16
) -> bool {
- if ip_str.is_null() {
+
+ if ip_str.is_null() || service.is_null() {
return false;
}
- let inner = service.as_inner();
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
let c_str = unsafe { CStr::from_ptr(ip_str) };
if let Ok(ip_str) = c_str.to_str() {
@@ -132,12 +143,12 @@ impl FfiTcpClientService {
/// Connect
#[unsafe(no_mangle)]
pub extern "C" fn tcp_client_connect(
- service: FfiTcpClientService
+ service: *mut FfiTcpClientService
) {
- let service_ptr = service.0 as *mut PadClientNetwork;
- assert!(!service_ptr.is_null(), "Service pointer is null");
+ if service.is_null() { return; }
- let service = unsafe { Box::from_raw(service_ptr) };
+ let inner = unsafe { &mut *((*service).0 as *mut PadClientNetwork) };
+ let service = unsafe { Box::from_raw(inner) };
service.connect();
}
@@ -145,13 +156,11 @@ impl FfiTcpClientService {
/// Free tcp client
#[unsafe(no_mangle)]
pub extern "C" fn free_tcp_client(
- service: FfiTcpClientService
+ service: *mut FfiTcpClientService
) {
- if service.0.is_null() {
- return;
- }
+ if service.is_null() { return; }
- let service_ptr = service.0 as *mut PadClientNetwork;
+ let service_ptr = service as *mut PadClientNetwork;
unsafe {
let _ = Box::from_raw(service_ptr);
@@ -161,35 +170,35 @@ impl FfiTcpClientService {
impl FfiTcpServerService {
- fn as_inner(&self) -> &mut PadServerNetwork {
- unsafe { &mut *(self.0 as *mut PadServerNetwork) }
- }
-
/// Build tcp server
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_build(
runtime: *mut FfiGameRuntime,
- ) -> FfiTcpServerService {
+ ) -> *mut FfiTcpServerService {
+
+ if runtime.is_null() { return std::ptr::null_mut(); }
let arc_ptr = unsafe { (*runtime).inner as *const Arc<Mutex<GameRuntime>> };
let arc_clone: Arc<Mutex<GameRuntime>> = unsafe { (&*arc_ptr).clone() };
- let service = Box::new(PadServerNetwork::build(arc_clone));
- let ptr = Box::into_raw(service);
+ let service = PadServerNetwork::build(arc_clone);
+ let raw = Box::into_raw(Box::new(FfiTcpServerService(Box::into_raw(Box::new(service)) as *mut _)));
- FfiTcpServerService(ptr as *mut c_void)
+ raw
}
/// Bind ipv4 address
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_bind_ipv4(
- service: FfiTcpServerService,
+ service: *mut FfiTcpServerService,
a0: u8,
a1: u8,
a2: u8,
a3: u8
) {
- let inner = service.as_inner();
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
let ip = IpAddr::V4(Ipv4Addr::new(a0, a1, a2, a3));
inner.bind_ip(ip);
}
@@ -197,14 +206,12 @@ impl FfiTcpServerService {
/// Bind ipv6 address
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_bind_ipv6(
- service: FfiTcpServerService,
+ service: *mut FfiTcpServerService,
ip_str: *const c_char
) -> bool {
- if ip_str.is_null() {
- return false;
- }
+ if ip_str.is_null() || service.is_null() { return false; }
- let inner = service.as_inner();
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
let c_str = unsafe { CStr::from_ptr(ip_str) };
if let Ok(ip_str) = c_str.to_str() {
@@ -220,23 +227,28 @@ impl FfiTcpServerService {
/// Bind port
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_bind_port(
- service: FfiTcpServerService,
+ service: *mut FfiTcpServerService,
port: u16
) {
- service.as_inner().bind_port(port);
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
+ inner.bind_port(port);
}
/// Bind address with ipv4
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_bind_address_v4(
- service: FfiTcpServerService,
+ service: *mut FfiTcpServerService,
a0: u8,
a1: u8,
a2: u8,
a3: u8,
port: u16
) {
- let inner = service.as_inner();
+ if service.is_null() { return; }
+
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
let addr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(a0, a1, a2, a3)),
port
@@ -248,15 +260,13 @@ impl FfiTcpServerService {
/// Bind address with ipv6
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_bind_address_v6(
- service: FfiTcpServerService,
+ service: *mut FfiTcpServerService,
ip_str: *const c_char,
port: u16
) -> bool {
- if ip_str.is_null() {
- return false;
- }
+ if ip_str.is_null() || service.is_null() { return false; }
- let inner = service.as_inner();
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
let c_str = unsafe { CStr::from_ptr(ip_str) };
if let Ok(ip_str) = c_str.to_str() {
@@ -277,12 +287,12 @@ impl FfiTcpServerService {
/// Start listening
#[unsafe(no_mangle)]
pub extern "C" fn tcp_server_listening_block_on(
- service: FfiTcpServerService
+ service: *mut FfiTcpServerService
) {
- let service_ptr = service.0 as *mut PadServerNetwork;
- assert!(!service_ptr.is_null(), "Service pointer is null");
+ if service.is_null() { return; }
- let service = unsafe { Box::from_raw(service_ptr) };
+ let inner = unsafe { &mut *((*service).0 as *mut PadServerNetwork) };
+ let service = unsafe { Box::from_raw(inner) };
service.listening_block_on();
}
@@ -290,13 +300,11 @@ impl FfiTcpServerService {
/// Free tcp server
#[unsafe(no_mangle)]
pub extern "C" fn free_tcp_server(
- service: FfiTcpServerService
+ service: *mut FfiTcpServerService
) {
- if service.0.is_null() {
- return;
- }
+ if service.is_null() { return; }
- let service_ptr = service.0 as *mut PadServerNetwork;
+ let service_ptr = service as *mut PadServerNetwork;
unsafe {
let _ = Box::from_raw(service_ptr);