use crate::commands::Command; use crate::{errors::ServicePointError, packet::Packet}; use servicepoint::UdpSocketExt; use std::{net::UdpSocket, sync::Arc}; #[derive(uniffi::Object)] pub struct Connection { actual: UdpSocket, } #[uniffi::export] impl Connection { #[uniffi::constructor] pub fn new(host: String) -> Result, ServicePointError> { UdpSocket::bind_connect(host) .map(|actual| Arc::new(Connection { actual })) .map_err(|err| ServicePointError::IoError { error: err.to_string(), }) } pub fn send_packet( &self, command: Arc, ) -> Result<(), ServicePointError> { self.actual .send_command(command.read().clone()) .ok_or_else(|| ServicePointError::IoError { error: "send failed".to_string(), }) } pub fn send_command( &self, command: Arc, ) -> Result<(), ServicePointError> { self.send_packet(command.as_packet()?) } }