1use crate::{ffi, WebsocketConnectionType, WebsocketExtension, WebsocketState};
7use glib::{
8 object::ObjectType as _,
9 prelude::*,
10 signal::{connect_raw, SignalHandlerId},
11 translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16 #[doc(alias = "SoupWebsocketConnection")]
17 pub struct WebsocketConnection(Object<ffi::SoupWebsocketConnection, ffi::SoupWebsocketConnectionClass>);
18
19 match fn {
20 type_ => || ffi::soup_websocket_connection_get_type(),
21 }
22}
23
24impl WebsocketConnection {
25 pub fn builder() -> WebsocketConnectionBuilder {
30 WebsocketConnectionBuilder::new()
31 }
32
33 #[doc(alias = "soup_websocket_connection_close")]
34 pub fn close(&self, code: libc::c_ushort, data: Option<&str>) {
35 unsafe {
36 ffi::soup_websocket_connection_close(
37 self.to_glib_none().0,
38 code,
39 data.to_glib_none().0,
40 );
41 }
42 }
43
44 #[doc(alias = "soup_websocket_connection_get_close_code")]
45 #[doc(alias = "get_close_code")]
46 pub fn close_code(&self) -> libc::c_ushort {
47 unsafe { ffi::soup_websocket_connection_get_close_code(self.to_glib_none().0) }
48 }
49
50 #[doc(alias = "soup_websocket_connection_get_close_data")]
51 #[doc(alias = "get_close_data")]
52 pub fn close_data(&self) -> Option<glib::GString> {
53 unsafe {
54 from_glib_none(ffi::soup_websocket_connection_get_close_data(
55 self.to_glib_none().0,
56 ))
57 }
58 }
59
60 #[doc(alias = "soup_websocket_connection_get_connection_type")]
61 #[doc(alias = "get_connection_type")]
62 #[doc(alias = "connection-type")]
63 pub fn connection_type(&self) -> WebsocketConnectionType {
64 unsafe {
65 from_glib(ffi::soup_websocket_connection_get_connection_type(
66 self.to_glib_none().0,
67 ))
68 }
69 }
70
71 #[doc(alias = "soup_websocket_connection_get_extensions")]
72 #[doc(alias = "get_extensions")]
73 pub fn extensions(&self) -> Vec<WebsocketExtension> {
74 unsafe {
75 FromGlibPtrContainer::from_glib_none(ffi::soup_websocket_connection_get_extensions(
76 self.to_glib_none().0,
77 ))
78 }
79 }
80
81 #[doc(alias = "soup_websocket_connection_get_io_stream")]
82 #[doc(alias = "get_io_stream")]
83 #[doc(alias = "io-stream")]
84 pub fn io_stream(&self) -> Option<gio::IOStream> {
85 unsafe {
86 from_glib_none(ffi::soup_websocket_connection_get_io_stream(
87 self.to_glib_none().0,
88 ))
89 }
90 }
91
92 #[doc(alias = "soup_websocket_connection_get_keepalive_interval")]
93 #[doc(alias = "get_keepalive_interval")]
94 #[doc(alias = "keepalive-interval")]
95 pub fn keepalive_interval(&self) -> u32 {
96 unsafe { ffi::soup_websocket_connection_get_keepalive_interval(self.to_glib_none().0) }
97 }
98
99 #[cfg(feature = "v3_6")]
100 #[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
101 #[doc(alias = "soup_websocket_connection_get_keepalive_pong_timeout")]
102 #[doc(alias = "get_keepalive_pong_timeout")]
103 #[doc(alias = "keepalive-pong-timeout")]
104 pub fn keepalive_pong_timeout(&self) -> u32 {
105 unsafe { ffi::soup_websocket_connection_get_keepalive_pong_timeout(self.to_glib_none().0) }
106 }
107
108 #[doc(alias = "soup_websocket_connection_get_max_incoming_payload_size")]
109 #[doc(alias = "get_max_incoming_payload_size")]
110 #[doc(alias = "max-incoming-payload-size")]
111 pub fn max_incoming_payload_size(&self) -> u64 {
112 unsafe {
113 ffi::soup_websocket_connection_get_max_incoming_payload_size(self.to_glib_none().0)
114 }
115 }
116
117 #[doc(alias = "soup_websocket_connection_get_origin")]
118 #[doc(alias = "get_origin")]
119 pub fn origin(&self) -> Option<glib::GString> {
120 unsafe {
121 from_glib_none(ffi::soup_websocket_connection_get_origin(
122 self.to_glib_none().0,
123 ))
124 }
125 }
126
127 #[doc(alias = "soup_websocket_connection_get_protocol")]
128 #[doc(alias = "get_protocol")]
129 pub fn protocol(&self) -> Option<glib::GString> {
130 unsafe {
131 from_glib_none(ffi::soup_websocket_connection_get_protocol(
132 self.to_glib_none().0,
133 ))
134 }
135 }
136
137 #[doc(alias = "soup_websocket_connection_get_state")]
138 #[doc(alias = "get_state")]
139 pub fn state(&self) -> WebsocketState {
140 unsafe {
141 from_glib(ffi::soup_websocket_connection_get_state(
142 self.to_glib_none().0,
143 ))
144 }
145 }
146
147 #[doc(alias = "soup_websocket_connection_get_uri")]
148 #[doc(alias = "get_uri")]
149 pub fn uri(&self) -> Option<glib::Uri> {
150 unsafe {
151 from_glib_none(ffi::soup_websocket_connection_get_uri(
152 self.to_glib_none().0,
153 ))
154 }
155 }
156
157 #[doc(alias = "soup_websocket_connection_send_text")]
158 pub fn send_text(&self, text: &str) {
159 unsafe {
160 ffi::soup_websocket_connection_send_text(self.to_glib_none().0, text.to_glib_none().0);
161 }
162 }
163
164 #[doc(alias = "soup_websocket_connection_set_keepalive_interval")]
165 #[doc(alias = "keepalive-interval")]
166 pub fn set_keepalive_interval(&self, interval: u32) {
167 unsafe {
168 ffi::soup_websocket_connection_set_keepalive_interval(self.to_glib_none().0, interval);
169 }
170 }
171
172 #[cfg(feature = "v3_6")]
173 #[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
174 #[doc(alias = "soup_websocket_connection_set_keepalive_pong_timeout")]
175 #[doc(alias = "keepalive-pong-timeout")]
176 pub fn set_keepalive_pong_timeout(&self, pong_timeout: u32) {
177 unsafe {
178 ffi::soup_websocket_connection_set_keepalive_pong_timeout(
179 self.to_glib_none().0,
180 pong_timeout,
181 );
182 }
183 }
184
185 #[doc(alias = "soup_websocket_connection_set_max_incoming_payload_size")]
186 #[doc(alias = "max-incoming-payload-size")]
187 pub fn set_max_incoming_payload_size(&self, max_incoming_payload_size: u64) {
188 unsafe {
189 ffi::soup_websocket_connection_set_max_incoming_payload_size(
190 self.to_glib_none().0,
191 max_incoming_payload_size,
192 );
193 }
194 }
195
196 #[doc(alias = "closed")]
197 pub fn connect_closed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
198 unsafe extern "C" fn closed_trampoline<F: Fn(&WebsocketConnection) + 'static>(
199 this: *mut ffi::SoupWebsocketConnection,
200 f: glib::ffi::gpointer,
201 ) {
202 let f: &F = &*(f as *const F);
203 f(&from_glib_borrow(this))
204 }
205 unsafe {
206 let f: Box_<F> = Box_::new(f);
207 connect_raw(
208 self.as_ptr() as *mut _,
209 c"closed".as_ptr() as *const _,
210 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
211 closed_trampoline::<F> as *const (),
212 )),
213 Box_::into_raw(f),
214 )
215 }
216 }
217
218 #[doc(alias = "closing")]
219 pub fn connect_closing<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
220 unsafe extern "C" fn closing_trampoline<F: Fn(&WebsocketConnection) + 'static>(
221 this: *mut ffi::SoupWebsocketConnection,
222 f: glib::ffi::gpointer,
223 ) {
224 let f: &F = &*(f as *const F);
225 f(&from_glib_borrow(this))
226 }
227 unsafe {
228 let f: Box_<F> = Box_::new(f);
229 connect_raw(
230 self.as_ptr() as *mut _,
231 c"closing".as_ptr() as *const _,
232 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
233 closing_trampoline::<F> as *const (),
234 )),
235 Box_::into_raw(f),
236 )
237 }
238 }
239
240 #[doc(alias = "error")]
241 pub fn connect_error<F: Fn(&Self, &glib::Error) + 'static>(&self, f: F) -> SignalHandlerId {
242 unsafe extern "C" fn error_trampoline<
243 F: Fn(&WebsocketConnection, &glib::Error) + 'static,
244 >(
245 this: *mut ffi::SoupWebsocketConnection,
246 error: *mut glib::ffi::GError,
247 f: glib::ffi::gpointer,
248 ) {
249 let f: &F = &*(f as *const F);
250 f(&from_glib_borrow(this), &from_glib_borrow(error))
251 }
252 unsafe {
253 let f: Box_<F> = Box_::new(f);
254 connect_raw(
255 self.as_ptr() as *mut _,
256 c"error".as_ptr() as *const _,
257 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
258 error_trampoline::<F> as *const (),
259 )),
260 Box_::into_raw(f),
261 )
262 }
263 }
264
265 #[doc(alias = "message")]
266 pub fn connect_message<F: Fn(&Self, i32, &glib::Bytes) + 'static>(
267 &self,
268 f: F,
269 ) -> SignalHandlerId {
270 unsafe extern "C" fn message_trampoline<
271 F: Fn(&WebsocketConnection, i32, &glib::Bytes) + 'static,
272 >(
273 this: *mut ffi::SoupWebsocketConnection,
274 type_: std::ffi::c_int,
275 message: *mut glib::ffi::GBytes,
276 f: glib::ffi::gpointer,
277 ) {
278 let f: &F = &*(f as *const F);
279 f(&from_glib_borrow(this), type_, &from_glib_borrow(message))
280 }
281 unsafe {
282 let f: Box_<F> = Box_::new(f);
283 connect_raw(
284 self.as_ptr() as *mut _,
285 c"message".as_ptr() as *const _,
286 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
287 message_trampoline::<F> as *const (),
288 )),
289 Box_::into_raw(f),
290 )
291 }
292 }
293
294 #[doc(alias = "pong")]
295 pub fn connect_pong<F: Fn(&Self, &glib::Bytes) + 'static>(&self, f: F) -> SignalHandlerId {
296 unsafe extern "C" fn pong_trampoline<
297 F: Fn(&WebsocketConnection, &glib::Bytes) + 'static,
298 >(
299 this: *mut ffi::SoupWebsocketConnection,
300 message: *mut glib::ffi::GBytes,
301 f: glib::ffi::gpointer,
302 ) {
303 let f: &F = &*(f as *const F);
304 f(&from_glib_borrow(this), &from_glib_borrow(message))
305 }
306 unsafe {
307 let f: Box_<F> = Box_::new(f);
308 connect_raw(
309 self.as_ptr() as *mut _,
310 c"pong".as_ptr() as *const _,
311 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
312 pong_trampoline::<F> as *const (),
313 )),
314 Box_::into_raw(f),
315 )
316 }
317 }
318
319 #[doc(alias = "keepalive-interval")]
320 pub fn connect_keepalive_interval_notify<F: Fn(&Self) + 'static>(
321 &self,
322 f: F,
323 ) -> SignalHandlerId {
324 unsafe extern "C" fn notify_keepalive_interval_trampoline<
325 F: Fn(&WebsocketConnection) + 'static,
326 >(
327 this: *mut ffi::SoupWebsocketConnection,
328 _param_spec: glib::ffi::gpointer,
329 f: glib::ffi::gpointer,
330 ) {
331 let f: &F = &*(f as *const F);
332 f(&from_glib_borrow(this))
333 }
334 unsafe {
335 let f: Box_<F> = Box_::new(f);
336 connect_raw(
337 self.as_ptr() as *mut _,
338 c"notify::keepalive-interval".as_ptr() as *const _,
339 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
340 notify_keepalive_interval_trampoline::<F> as *const (),
341 )),
342 Box_::into_raw(f),
343 )
344 }
345 }
346
347 #[cfg(feature = "v3_6")]
348 #[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
349 #[doc(alias = "keepalive-pong-timeout")]
350 pub fn connect_keepalive_pong_timeout_notify<F: Fn(&Self) + 'static>(
351 &self,
352 f: F,
353 ) -> SignalHandlerId {
354 unsafe extern "C" fn notify_keepalive_pong_timeout_trampoline<
355 F: Fn(&WebsocketConnection) + 'static,
356 >(
357 this: *mut ffi::SoupWebsocketConnection,
358 _param_spec: glib::ffi::gpointer,
359 f: glib::ffi::gpointer,
360 ) {
361 let f: &F = &*(f as *const F);
362 f(&from_glib_borrow(this))
363 }
364 unsafe {
365 let f: Box_<F> = Box_::new(f);
366 connect_raw(
367 self.as_ptr() as *mut _,
368 c"notify::keepalive-pong-timeout".as_ptr() as *const _,
369 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
370 notify_keepalive_pong_timeout_trampoline::<F> as *const (),
371 )),
372 Box_::into_raw(f),
373 )
374 }
375 }
376
377 #[doc(alias = "max-incoming-payload-size")]
378 pub fn connect_max_incoming_payload_size_notify<F: Fn(&Self) + 'static>(
379 &self,
380 f: F,
381 ) -> SignalHandlerId {
382 unsafe extern "C" fn notify_max_incoming_payload_size_trampoline<
383 F: Fn(&WebsocketConnection) + 'static,
384 >(
385 this: *mut ffi::SoupWebsocketConnection,
386 _param_spec: glib::ffi::gpointer,
387 f: glib::ffi::gpointer,
388 ) {
389 let f: &F = &*(f as *const F);
390 f(&from_glib_borrow(this))
391 }
392 unsafe {
393 let f: Box_<F> = Box_::new(f);
394 connect_raw(
395 self.as_ptr() as *mut _,
396 c"notify::max-incoming-payload-size".as_ptr() as *const _,
397 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
398 notify_max_incoming_payload_size_trampoline::<F> as *const (),
399 )),
400 Box_::into_raw(f),
401 )
402 }
403 }
404
405 #[doc(alias = "state")]
406 pub fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
407 unsafe extern "C" fn notify_state_trampoline<F: Fn(&WebsocketConnection) + 'static>(
408 this: *mut ffi::SoupWebsocketConnection,
409 _param_spec: glib::ffi::gpointer,
410 f: glib::ffi::gpointer,
411 ) {
412 let f: &F = &*(f as *const F);
413 f(&from_glib_borrow(this))
414 }
415 unsafe {
416 let f: Box_<F> = Box_::new(f);
417 connect_raw(
418 self.as_ptr() as *mut _,
419 c"notify::state".as_ptr() as *const _,
420 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
421 notify_state_trampoline::<F> as *const (),
422 )),
423 Box_::into_raw(f),
424 )
425 }
426 }
427}
428
429#[must_use = "The builder must be built to be used"]
434pub struct WebsocketConnectionBuilder {
435 builder: glib::object::ObjectBuilder<'static, WebsocketConnection>,
436}
437
438impl WebsocketConnectionBuilder {
439 fn new() -> Self {
440 Self {
441 builder: glib::object::Object::builder(),
442 }
443 }
444
445 pub fn connection_type(self, connection_type: WebsocketConnectionType) -> Self {
446 Self {
447 builder: self.builder.property("connection-type", connection_type),
448 }
449 }
450
451 pub fn io_stream(self, io_stream: &impl IsA<gio::IOStream>) -> Self {
456 Self {
457 builder: self
458 .builder
459 .property("io-stream", io_stream.clone().upcast()),
460 }
461 }
462
463 pub fn keepalive_interval(self, keepalive_interval: u32) -> Self {
464 Self {
465 builder: self
466 .builder
467 .property("keepalive-interval", keepalive_interval),
468 }
469 }
470
471 #[cfg(feature = "v3_6")]
472 #[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
473 pub fn keepalive_pong_timeout(self, keepalive_pong_timeout: u32) -> Self {
474 Self {
475 builder: self
476 .builder
477 .property("keepalive-pong-timeout", keepalive_pong_timeout),
478 }
479 }
480
481 pub fn max_incoming_payload_size(self, max_incoming_payload_size: u64) -> Self {
482 Self {
483 builder: self
484 .builder
485 .property("max-incoming-payload-size", max_incoming_payload_size),
486 }
487 }
488
489 pub fn origin(self, origin: impl Into<glib::GString>) -> Self {
490 Self {
491 builder: self.builder.property("origin", origin.into()),
492 }
493 }
494
495 pub fn protocol(self, protocol: impl Into<glib::GString>) -> Self {
496 Self {
497 builder: self.builder.property("protocol", protocol.into()),
498 }
499 }
500
501 pub fn uri(self, uri: &glib::Uri) -> Self {
502 Self {
503 builder: self.builder.property("uri", uri.clone()),
504 }
505 }
506
507 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
510 pub fn build(self) -> WebsocketConnection {
511 assert_initialized_main_thread!();
512 self.builder.build()
513 }
514}