|
| 1 | +// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | + |
| 3 | +use crate::Cancellable; |
| 4 | +use crate::DatagramBased; |
| 5 | +use crate::InputMessage; |
| 6 | +use crate::OutputMessage; |
| 7 | +use futures_core::stream::Stream; |
| 8 | +use glib::object::{Cast, IsA}; |
| 9 | +use glib::translate::*; |
| 10 | +use std::cell::RefCell; |
| 11 | +use std::mem::transmute; |
| 12 | +use std::pin::Pin; |
| 13 | +use std::ptr; |
| 14 | +use std::time::Duration; |
| 15 | + |
| 16 | +pub trait DatagramBasedExtManual: Sized { |
| 17 | + #[doc(alias = "g_datagram_based_create_source")] |
| 18 | + fn create_source<F, C>( |
| 19 | + &self, |
| 20 | + condition: glib::IOCondition, |
| 21 | + cancellable: Option<&C>, |
| 22 | + name: Option<&str>, |
| 23 | + priority: glib::Priority, |
| 24 | + func: F, |
| 25 | + ) -> glib::Source |
| 26 | + where |
| 27 | + F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static, |
| 28 | + C: IsA<Cancellable>; |
| 29 | + |
| 30 | + fn create_source_future<C: IsA<Cancellable>>( |
| 31 | + &self, |
| 32 | + condition: glib::IOCondition, |
| 33 | + cancellable: Option<&C>, |
| 34 | + priority: glib::Priority, |
| 35 | + ) -> Pin<Box<dyn std::future::Future<Output = glib::IOCondition> + 'static>>; |
| 36 | + |
| 37 | + fn create_source_stream<C: IsA<Cancellable>>( |
| 38 | + &self, |
| 39 | + condition: glib::IOCondition, |
| 40 | + cancellable: Option<&C>, |
| 41 | + priority: glib::Priority, |
| 42 | + ) -> Pin<Box<dyn Stream<Item = glib::IOCondition> + 'static>>; |
| 43 | + |
| 44 | + #[doc(alias = "g_datagram_based_condition_wait")] |
| 45 | + fn condition_wait( |
| 46 | + &self, |
| 47 | + condition: glib::IOCondition, |
| 48 | + timeout: Option<Duration>, |
| 49 | + cancellable: Option<&impl IsA<Cancellable>>, |
| 50 | + ) -> Result<(), glib::Error>; |
| 51 | + |
| 52 | + #[doc(alias = "g_datagram_based_receive_messages")] |
| 53 | + fn receive_messages<'v, V: IntoIterator<Item = &'v mut [&'v mut [u8]]>, C: IsA<Cancellable>>( |
| 54 | + &self, |
| 55 | + messages: &mut [InputMessage], |
| 56 | + flags: i32, |
| 57 | + timeout: Option<Duration>, |
| 58 | + cancellable: Option<&C>, |
| 59 | + ) -> Result<usize, glib::Error>; |
| 60 | + |
| 61 | + #[doc(alias = "g_datagram_based_send_messages")] |
| 62 | + fn send_messages<C: IsA<Cancellable>>( |
| 63 | + &self, |
| 64 | + messages: &mut [OutputMessage], |
| 65 | + flags: i32, |
| 66 | + timeout: Option<Duration>, |
| 67 | + cancellable: Option<&C>, |
| 68 | + ) -> Result<usize, glib::Error>; |
| 69 | +} |
| 70 | + |
| 71 | +impl<O: IsA<DatagramBased>> DatagramBasedExtManual for O { |
| 72 | + fn create_source<F, C>( |
| 73 | + &self, |
| 74 | + condition: glib::IOCondition, |
| 75 | + cancellable: Option<&C>, |
| 76 | + name: Option<&str>, |
| 77 | + priority: glib::Priority, |
| 78 | + func: F, |
| 79 | + ) -> glib::Source |
| 80 | + where |
| 81 | + F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static, |
| 82 | + C: IsA<Cancellable>, |
| 83 | + { |
| 84 | + unsafe extern "C" fn trampoline< |
| 85 | + O: IsA<DatagramBased>, |
| 86 | + F: FnMut(&O, glib::IOCondition) -> glib::Continue + 'static, |
| 87 | + >( |
| 88 | + datagram_based: *mut ffi::GDatagramBased, |
| 89 | + condition: glib::ffi::GIOCondition, |
| 90 | + func: glib::ffi::gpointer, |
| 91 | + ) -> glib::ffi::gboolean { |
| 92 | + let func: &RefCell<F> = &*(func as *const RefCell<F>); |
| 93 | + let mut func = func.borrow_mut(); |
| 94 | + (*func)( |
| 95 | + DatagramBased::from_glib_borrow(datagram_based).unsafe_cast_ref(), |
| 96 | + from_glib(condition), |
| 97 | + ) |
| 98 | + .into_glib() |
| 99 | + } |
| 100 | + unsafe extern "C" fn destroy_closure<O, F>(ptr: glib::ffi::gpointer) { |
| 101 | + let _ = Box::<RefCell<F>>::from_raw(ptr as *mut _); |
| 102 | + } |
| 103 | + let cancellable = cancellable.map(|c| c.as_ref()); |
| 104 | + let gcancellable = cancellable.to_glib_none(); |
| 105 | + unsafe { |
| 106 | + let source = ffi::g_datagram_based_create_source( |
| 107 | + self.as_ref().to_glib_none().0, |
| 108 | + condition.into_glib(), |
| 109 | + gcancellable.0, |
| 110 | + ); |
| 111 | + let trampoline = trampoline::<O, F> as glib::ffi::gpointer; |
| 112 | + glib::ffi::g_source_set_callback( |
| 113 | + source, |
| 114 | + Some(transmute::< |
| 115 | + _, |
| 116 | + unsafe extern "C" fn(glib::ffi::gpointer) -> glib::ffi::gboolean, |
| 117 | + >(trampoline)), |
| 118 | + Box::into_raw(Box::new(RefCell::new(func))) as glib::ffi::gpointer, |
| 119 | + Some(destroy_closure::<O, F>), |
| 120 | + ); |
| 121 | + glib::ffi::g_source_set_priority(source, priority.into_glib()); |
| 122 | + |
| 123 | + if let Some(name) = name { |
| 124 | + glib::ffi::g_source_set_name(source, name.to_glib_none().0); |
| 125 | + } |
| 126 | + |
| 127 | + from_glib_full(source) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn create_source_future<C: IsA<Cancellable>>( |
| 132 | + &self, |
| 133 | + condition: glib::IOCondition, |
| 134 | + cancellable: Option<&C>, |
| 135 | + priority: glib::Priority, |
| 136 | + ) -> Pin<Box<dyn std::future::Future<Output = glib::IOCondition> + 'static>> { |
| 137 | + let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned(); |
| 138 | + |
| 139 | + let obj = self.clone(); |
| 140 | + Box::pin(glib::SourceFuture::new(move |send| { |
| 141 | + let mut send = Some(send); |
| 142 | + obj.create_source( |
| 143 | + condition, |
| 144 | + cancellable.as_ref(), |
| 145 | + None, |
| 146 | + priority, |
| 147 | + move |_, condition| { |
| 148 | + let _ = send.take().unwrap().send(condition); |
| 149 | + glib::Continue(false) |
| 150 | + }, |
| 151 | + ) |
| 152 | + })) |
| 153 | + } |
| 154 | + |
| 155 | + fn create_source_stream<C: IsA<Cancellable>>( |
| 156 | + &self, |
| 157 | + condition: glib::IOCondition, |
| 158 | + cancellable: Option<&C>, |
| 159 | + priority: glib::Priority, |
| 160 | + ) -> Pin<Box<dyn Stream<Item = glib::IOCondition> + 'static>> { |
| 161 | + let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned(); |
| 162 | + |
| 163 | + let obj = self.clone(); |
| 164 | + Box::pin(glib::SourceStream::new(move |send| { |
| 165 | + let send = Some(send); |
| 166 | + obj.create_source( |
| 167 | + condition, |
| 168 | + cancellable.as_ref(), |
| 169 | + None, |
| 170 | + priority, |
| 171 | + move |_, condition| { |
| 172 | + if send.as_ref().unwrap().unbounded_send(condition).is_err() { |
| 173 | + glib::Continue(false) |
| 174 | + } else { |
| 175 | + glib::Continue(true) |
| 176 | + } |
| 177 | + }, |
| 178 | + ) |
| 179 | + })) |
| 180 | + } |
| 181 | + |
| 182 | + fn condition_wait( |
| 183 | + &self, |
| 184 | + condition: glib::IOCondition, |
| 185 | + timeout: Option<Duration>, |
| 186 | + cancellable: Option<&impl IsA<Cancellable>>, |
| 187 | + ) -> Result<(), glib::Error> { |
| 188 | + unsafe { |
| 189 | + let mut error = ptr::null_mut(); |
| 190 | + let is_ok = ffi::g_datagram_based_condition_wait( |
| 191 | + self.as_ref().to_glib_none().0, |
| 192 | + condition.into_glib(), |
| 193 | + timeout |
| 194 | + .map(|t| t.as_micros().try_into().unwrap()) |
| 195 | + .unwrap_or(-1), |
| 196 | + cancellable.map(|p| p.as_ref()).to_glib_none().0, |
| 197 | + &mut error, |
| 198 | + ); |
| 199 | + assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null()); |
| 200 | + if error.is_null() { |
| 201 | + Ok(()) |
| 202 | + } else { |
| 203 | + Err(from_glib_full(error)) |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + fn receive_messages<'v, V: IntoIterator<Item = &'v mut [&'v mut [u8]]>, C: IsA<Cancellable>>( |
| 209 | + &self, |
| 210 | + messages: &mut [InputMessage], |
| 211 | + flags: i32, |
| 212 | + timeout: Option<Duration>, |
| 213 | + cancellable: Option<&C>, |
| 214 | + ) -> Result<usize, glib::Error> { |
| 215 | + let cancellable = cancellable.map(|c| c.as_ref()); |
| 216 | + unsafe { |
| 217 | + let mut error = ptr::null_mut(); |
| 218 | + |
| 219 | + let count = ffi::g_datagram_based_receive_messages( |
| 220 | + self.as_ref().to_glib_none().0, |
| 221 | + messages.as_mut_ptr() as *mut _, |
| 222 | + messages.len().try_into().unwrap(), |
| 223 | + flags, |
| 224 | + timeout |
| 225 | + .map(|t| t.as_micros().try_into().unwrap()) |
| 226 | + .unwrap_or(-1), |
| 227 | + cancellable.to_glib_none().0, |
| 228 | + &mut error, |
| 229 | + ); |
| 230 | + if error.is_null() { |
| 231 | + Ok(count as usize) |
| 232 | + } else { |
| 233 | + Err(from_glib_full(error)) |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + fn send_messages<C: IsA<Cancellable>>( |
| 239 | + &self, |
| 240 | + messages: &mut [OutputMessage], |
| 241 | + flags: i32, |
| 242 | + timeout: Option<Duration>, |
| 243 | + cancellable: Option<&C>, |
| 244 | + ) -> Result<usize, glib::Error> { |
| 245 | + let cancellable = cancellable.map(|c| c.as_ref()); |
| 246 | + unsafe { |
| 247 | + let mut error = ptr::null_mut(); |
| 248 | + let count = ffi::g_datagram_based_send_messages( |
| 249 | + self.as_ref().to_glib_none().0, |
| 250 | + messages.as_mut_ptr() as *mut _, |
| 251 | + messages.len().try_into().unwrap(), |
| 252 | + flags, |
| 253 | + timeout |
| 254 | + .map(|t| t.as_micros().try_into().unwrap()) |
| 255 | + .unwrap_or(-1), |
| 256 | + cancellable.to_glib_none().0, |
| 257 | + &mut error, |
| 258 | + ); |
| 259 | + if error.is_null() { |
| 260 | + Ok(count as usize) |
| 261 | + } else { |
| 262 | + Err(from_glib_full(error)) |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments