1use std::cell::Cell;
5use std::collections::hash_map::DefaultHasher;
6use std::ffi::{c_char, c_void};
7use std::hash::{Hash, Hasher};
8use std::time::Duration;
9
10use rustorb_core::{
11 CompletionStatus, Exception as CoreException, ObjectRef, Orb, OutStream, SystemException,
12 SystemExceptionKind, minor,
13};
14
15use crate::stream::{In, Out, RorbIn, RorbOut};
16use crate::{
17 CALLBACK_FAILED, Exception, OK, SYSTEM_EXCEPTION, Status, USER_EXCEPTION, guard, into_handle,
18 release_handle, set_system, str_arg, sys,
19};
20
21#[unsafe(no_mangle)]
22pub unsafe extern "C" fn rorb_object_clone(obj: *mut ObjectRef) -> *mut ObjectRef {
23 into_handle(unsafe { &*obj }.clone())
24}
25
26#[unsafe(no_mangle)]
27pub unsafe extern "C" fn rorb_object_release(obj: *mut ObjectRef) {
28 unsafe { release_handle(obj) }
29}
30
31#[unsafe(no_mangle)]
32pub unsafe extern "C" fn rorb_object_orb(obj: *mut ObjectRef) -> *mut Orb {
33 into_handle(unsafe { &*obj }.orb().clone())
34}
35
36#[unsafe(no_mangle)]
37pub unsafe extern "C" fn rorb_object_type_id(
38 obj: *mut ObjectRef,
39 len: *mut usize,
40) -> *const c_char {
41 let id = unsafe { &*obj }.type_id();
42 unsafe { *len = id.len() };
43 id.as_ptr() as *const c_char
44}
45
46fn unlisted() -> SystemException {
48 sys(
49 SystemExceptionKind::UNKNOWN,
50 minor::UNLISTED_USER_EXCEPTION,
51 CompletionStatus::Yes,
52 )
53}
54
55fn system(e: CoreException) -> SystemException {
56 match e {
57 CoreException::System(e) => e,
58 CoreException::User(_) => unlisted(),
59 }
60}
61
62#[unsafe(no_mangle)]
63pub unsafe extern "C" fn rorb_object_is_a(
64 obj: *mut ObjectRef,
65 id: *const c_char,
66 len: usize,
67 result: *mut i32,
68 exc: *mut Exception,
69) -> Status {
70 guard(exc, || {
71 let obj = unsafe { &*obj };
72 let r = unsafe { str_arg(id, len) }.and_then(|id| obj.is_a(id).map_err(system));
73 match r {
74 Ok(v) => {
75 unsafe { *result = i32::from(v) };
76 OK
77 }
78 Err(e) => unsafe { set_system(exc, &e) },
79 }
80 })
81}
82
83#[unsafe(no_mangle)]
84pub unsafe extern "C" fn rorb_object_non_existent(
85 obj: *mut ObjectRef,
86 result: *mut i32,
87 exc: *mut Exception,
88) -> Status {
89 guard(exc, || match unsafe { &*obj }.non_existent() {
90 Ok(v) => {
91 unsafe { *result = i32::from(v) };
92 OK
93 }
94 Err(e) => unsafe { set_system(exc, &system(e)) },
95 })
96}
97
98#[unsafe(no_mangle)]
99pub unsafe extern "C" fn rorb_object_is_equivalent(a: *mut ObjectRef, b: *mut ObjectRef) -> i32 {
100 match (unsafe { a.as_ref() }, unsafe { b.as_ref() }) {
101 (Some(a), Some(b)) => i32::from(a.is_equivalent(b)),
102 (None, None) => 1,
103 _ => 0,
104 }
105}
106
107#[unsafe(no_mangle)]
108pub unsafe extern "C" fn rorb_object_hash(obj: *mut ObjectRef, maximum: u32) -> u32 {
109 let mut h = DefaultHasher::new();
110 for p in &unsafe { &*obj }.ior().profiles {
111 p.tag.hash(&mut h);
112 p.profile_data.hash(&mut h);
113 }
114 let maximum = u64::from(maximum).max(1);
115 u32::try_from(h.finish() % maximum).unwrap_or(0)
116}
117
118#[unsafe(no_mangle)]
119pub unsafe extern "C" fn rorb_object_set_call_timeout(obj: *mut ObjectRef, us: i64) {
120 unsafe { &*obj }.set_call_timeout(crate::orb::setting_us(us));
121}
122
123#[unsafe(no_mangle)]
124pub unsafe extern "C" fn rorb_object_call_timeout(obj: *mut ObjectRef) -> i64 {
125 crate::orb::us_of_setting(unsafe { &*obj }.call_timeout())
126}
127
128type WriteFn = extern "C" fn(*mut c_void, *mut RorbOut) -> Status;
129type ReadFn = extern "C" fn(*mut c_void, *mut RorbIn) -> Status;
130type UserExceptionFn = extern "C" fn(*mut c_void, *const c_char, usize, *mut RorbIn) -> Status;
131
132#[repr(C)]
134#[derive(Debug)]
135pub struct Invocation {
136 pub operation: *const c_char,
137 pub operation_len: usize,
138 pub oneway: i32,
139 pub timeout_us: i64,
140 pub context: *mut c_void,
141 pub write_args: Option<WriteFn>,
142 pub read_reply: Option<ReadFn>,
143 pub read_user_exception: Option<UserExceptionFn>,
144}
145
146pub(crate) enum Sent {
148 Oneway,
149 Reply(rustorb_core::Reply),
150 User(rustorb_core::UserException),
151}
152
153pub(crate) unsafe fn send(
157 obj: &ObjectRef,
158 inv: &Invocation,
159 exc: *mut Exception,
160) -> Result<Sent, Status> {
161 let operation = match unsafe { str_arg(inv.operation, inv.operation_len) } {
162 Ok(op) => op,
163 Err(e) => return Err(unsafe { set_system(exc, &e) }),
164 };
165 let failed = Cell::new(false);
168 let write = |out: &mut OutStream<'_>| -> Result<(), SystemException> {
169 let Some(write_args) = inv.write_args else {
170 return Ok(());
171 };
172 let mut o = Out::new(out);
173 if write_args(inv.context, o.as_ptr()) != OK {
174 failed.set(true);
175 return Err(sys(SystemExceptionKind::UNKNOWN, 0, CompletionStatus::No));
176 }
177 match o.error {
178 Some(e) => Err(e),
179 None => Ok(()),
180 }
181 };
182 let mut request = obj.request(operation).args(write);
183 if inv.timeout_us >= 0 {
184 let timeout = u64::try_from(inv.timeout_us).unwrap_or(0);
185 request = request.timeout((timeout > 0).then(|| Duration::from_micros(timeout)));
186 }
187 if inv.oneway != 0 {
188 return match request.send_oneway() {
189 Ok(()) => Ok(Sent::Oneway),
190 Err(_) if failed.get() => Err(CALLBACK_FAILED),
191 Err(e) => Err(unsafe { set_system(exc, &e) }),
192 };
193 }
194 match request.invoke() {
195 Ok(reply) => Ok(Sent::Reply(reply)),
196 Err(CoreException::System(_)) if failed.get() => Err(CALLBACK_FAILED),
197 Err(CoreException::System(e)) => Err(unsafe { set_system(exc, &e) }),
198 Err(CoreException::User(u)) => Ok(Sent::User(u)),
199 }
200}
201
202#[unsafe(no_mangle)]
203pub unsafe extern "C" fn rorb_object_invoke(
204 obj: *mut ObjectRef,
205 inv: *const Invocation,
206 exc: *mut Exception,
207) -> Status {
208 guard(exc, || {
209 let obj = unsafe { &*obj };
210 let inv = unsafe { &*inv };
211 let sent = match unsafe { send(obj, inv, exc) } {
212 Ok(sent) => sent,
213 Err(status) => return status,
214 };
215 match sent {
216 Sent::Oneway => OK,
217 Sent::Reply(reply) => {
218 let Some(read_reply) = inv.read_reply else {
219 return OK;
220 };
221 let mut stream = reply.input();
222 let mut input = In::new(&mut stream, CompletionStatus::Yes);
223 if read_reply(inv.context, input.as_ptr()) != OK {
224 return CALLBACK_FAILED;
225 }
226 match input.error {
227 Some(e) => unsafe { set_system(exc, &e) },
228 None => OK,
229 }
230 }
231 Sent::User(u) => {
232 let Some(read_user_exception) = inv.read_user_exception else {
233 return unsafe { set_system(exc, &unlisted()) };
234 };
235 let mut stream = match u.input() {
236 Ok(s) => s,
237 Err(e) => return unsafe { set_system(exc, &e) },
238 };
239 let id = u.repository_id();
240 let mut input = In::new(&mut stream, CompletionStatus::Yes);
241 match read_user_exception(
242 inv.context,
243 id.as_ptr() as *const c_char,
244 id.len(),
245 input.as_ptr(),
246 ) {
247 USER_EXCEPTION => match input.error {
248 Some(e) => unsafe { set_system(exc, &e) },
249 None => USER_EXCEPTION,
250 },
251 SYSTEM_EXCEPTION => unsafe { set_system(exc, &unlisted()) },
252 _ => CALLBACK_FAILED,
253 }
254 }
255 }
256 })
257}