39 lines
1 KiB
Rust
39 lines
1 KiB
Rust
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<Arc<Self>, 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<Packet>,
|
|
) -> 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<dyn Command>,
|
|
) -> Result<(), ServicePointError> {
|
|
self.send_packet(command.as_packet()?)
|
|
}
|
|
}
|