gio/auto/
socket_connection.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib;
9use glib::object::IsA;
10use glib::translate::*;
11use glib_sys;
12use gobject_sys;
13#[cfg(feature = "futures")]
14use std::boxed::Box as Box_;
15use std::fmt;
16use std::ptr;
17use Cancellable;
18use Error;
19use IOStream;
20use Socket;
21use SocketAddress;
22use SocketFamily;
23use SocketType;
24
25glib_wrapper! {
26    pub struct SocketConnection(Object<gio_sys::GSocketConnection, gio_sys::GSocketConnectionClass, SocketConnectionClass>) @extends IOStream;
27
28    match fn {
29        get_type => || gio_sys::g_socket_connection_get_type(),
30    }
31}
32
33impl SocketConnection {
34    pub fn factory_lookup_type(
35        family: SocketFamily,
36        type_: SocketType,
37        protocol_id: i32,
38    ) -> glib::types::Type {
39        unsafe {
40            from_glib(gio_sys::g_socket_connection_factory_lookup_type(
41                family.to_glib(),
42                type_.to_glib(),
43                protocol_id,
44            ))
45        }
46    }
47
48    pub fn factory_register_type(
49        g_type: glib::types::Type,
50        family: SocketFamily,
51        type_: SocketType,
52        protocol: i32,
53    ) {
54        unsafe {
55            gio_sys::g_socket_connection_factory_register_type(
56                g_type.to_glib(),
57                family.to_glib(),
58                type_.to_glib(),
59                protocol,
60            );
61        }
62    }
63}
64
65pub const NONE_SOCKET_CONNECTION: Option<&SocketConnection> = None;
66
67pub trait SocketConnectionExt: 'static {
68    fn connect<P: IsA<SocketAddress>, Q: IsA<Cancellable>>(
69        &self,
70        address: &P,
71        cancellable: Option<&Q>,
72    ) -> Result<(), Error>;
73
74    fn connect_async<
75        P: IsA<SocketAddress>,
76        Q: IsA<Cancellable>,
77        R: FnOnce(Result<(), Error>) + Send + 'static,
78    >(
79        &self,
80        address: &P,
81        cancellable: Option<&Q>,
82        callback: R,
83    );
84
85    #[cfg(feature = "futures")]
86    fn connect_async_future<P: IsA<SocketAddress> + Clone + 'static>(
87        &self,
88        address: &P,
89    ) -> Box_<dyn future::Future<Output = Result<(), Error>> + std::marker::Unpin>;
90
91    fn get_local_address(&self) -> Result<SocketAddress, Error>;
92
93    fn get_remote_address(&self) -> Result<SocketAddress, Error>;
94
95    fn get_socket(&self) -> Option<Socket>;
96
97    fn is_connected(&self) -> bool;
98}
99
100impl<O: IsA<SocketConnection>> SocketConnectionExt for O {
101    fn connect<P: IsA<SocketAddress>, Q: IsA<Cancellable>>(
102        &self,
103        address: &P,
104        cancellable: Option<&Q>,
105    ) -> Result<(), Error> {
106        unsafe {
107            let mut error = ptr::null_mut();
108            let _ = gio_sys::g_socket_connection_connect(
109                self.as_ref().to_glib_none().0,
110                address.as_ref().to_glib_none().0,
111                cancellable.map(|p| p.as_ref()).to_glib_none().0,
112                &mut error,
113            );
114            if error.is_null() {
115                Ok(())
116            } else {
117                Err(from_glib_full(error))
118            }
119        }
120    }
121
122    fn connect_async<
123        P: IsA<SocketAddress>,
124        Q: IsA<Cancellable>,
125        R: FnOnce(Result<(), Error>) + Send + 'static,
126    >(
127        &self,
128        address: &P,
129        cancellable: Option<&Q>,
130        callback: R,
131    ) {
132        let user_data: Box<R> = Box::new(callback);
133        unsafe extern "C" fn connect_async_trampoline<
134            R: FnOnce(Result<(), Error>) + Send + 'static,
135        >(
136            _source_object: *mut gobject_sys::GObject,
137            res: *mut gio_sys::GAsyncResult,
138            user_data: glib_sys::gpointer,
139        ) {
140            let mut error = ptr::null_mut();
141            let _ = gio_sys::g_socket_connection_connect_finish(
142                _source_object as *mut _,
143                res,
144                &mut error,
145            );
146            let result = if error.is_null() {
147                Ok(())
148            } else {
149                Err(from_glib_full(error))
150            };
151            let callback: Box<R> = Box::from_raw(user_data as *mut _);
152            callback(result);
153        }
154        let callback = connect_async_trampoline::<R>;
155        unsafe {
156            gio_sys::g_socket_connection_connect_async(
157                self.as_ref().to_glib_none().0,
158                address.as_ref().to_glib_none().0,
159                cancellable.map(|p| p.as_ref()).to_glib_none().0,
160                Some(callback),
161                Box::into_raw(user_data) as *mut _,
162            );
163        }
164    }
165
166    #[cfg(feature = "futures")]
167    fn connect_async_future<P: IsA<SocketAddress> + Clone + 'static>(
168        &self,
169        address: &P,
170    ) -> Box_<dyn future::Future<Output = Result<(), Error>> + std::marker::Unpin> {
171        use fragile::Fragile;
172        use GioFuture;
173
174        let address = address.clone();
175        GioFuture::new(self, move |obj, send| {
176            let cancellable = Cancellable::new();
177            let send = Fragile::new(send);
178            obj.connect_async(&address, Some(&cancellable), move |res| {
179                let _ = send.into_inner().send(res);
180            });
181
182            cancellable
183        })
184    }
185
186    fn get_local_address(&self) -> Result<SocketAddress, Error> {
187        unsafe {
188            let mut error = ptr::null_mut();
189            let ret = gio_sys::g_socket_connection_get_local_address(
190                self.as_ref().to_glib_none().0,
191                &mut error,
192            );
193            if error.is_null() {
194                Ok(from_glib_full(ret))
195            } else {
196                Err(from_glib_full(error))
197            }
198        }
199    }
200
201    fn get_remote_address(&self) -> Result<SocketAddress, Error> {
202        unsafe {
203            let mut error = ptr::null_mut();
204            let ret = gio_sys::g_socket_connection_get_remote_address(
205                self.as_ref().to_glib_none().0,
206                &mut error,
207            );
208            if error.is_null() {
209                Ok(from_glib_full(ret))
210            } else {
211                Err(from_glib_full(error))
212            }
213        }
214    }
215
216    fn get_socket(&self) -> Option<Socket> {
217        unsafe {
218            from_glib_none(gio_sys::g_socket_connection_get_socket(
219                self.as_ref().to_glib_none().0,
220            ))
221        }
222    }
223
224    fn is_connected(&self) -> bool {
225        unsafe {
226            from_glib(gio_sys::g_socket_connection_is_connected(
227                self.as_ref().to_glib_none().0,
228            ))
229        }
230    }
231}
232
233impl fmt::Display for SocketConnection {
234    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235        write!(f, "SocketConnection")
236    }
237}