Create a new instance of the websocket client.
(&self)
| 41 | |
| 42 | /// Create a new instance of the websocket client. |
| 43 | fn create_client(&self) -> Result<Ref<CoreWebsocketClient>, ()> { |
| 44 | let client_uninit = MaybeUninit::uninit(); |
| 45 | // SAFETY: Websocket client is freed by cb_destroy_client |
| 46 | let leaked_client = Box::leak(Box::new(client_uninit)); |
| 47 | let mut callbacks = BNWebsocketClientCallbacks { |
| 48 | context: leaked_client as *mut _ as *mut c_void, |
| 49 | connect: Some(client::cb_connect::<Self::Client>), |
| 50 | destroyClient: Some(client::cb_destroy_client::<Self::Client>), |
| 51 | disconnect: Some(client::cb_disconnect::<Self::Client>), |
| 52 | write: Some(client::cb_write::<Self::Client>), |
| 53 | }; |
| 54 | let client_ptr = |
| 55 | unsafe { BNInitWebsocketClient(self.handle().handle.as_ptr(), &mut callbacks) }; |
| 56 | // TODO: If possible pass a sensible error back... |
| 57 | let client_ptr = NonNull::new(client_ptr).ok_or(())?; |
| 58 | let client_ref = unsafe { CoreWebsocketClient::ref_from_raw(client_ptr) }; |
| 59 | // We now have the core client so we can actually construct the object. |
| 60 | leaked_client.write(Self::Client::from_core(client_ref.clone())); |
| 61 | Ok(client_ref) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[derive(Clone, Copy, Hash, PartialEq, Eq)] |