voidio 0.1.7

VOID I/O - High-performance Cross-platform I/O for Rust.
use std::net::{SocketAddr};

use crate::net::{AfInet, AfInet6, IpProtoUdp, SockDgram, Socket};
use crate::net::connection::QuicConnection;
use super::{QuicStream};

pub struct QuicClient {
    address: Option<SocketAddr>,
    socket: Option<Socket>,
    connection: Option<QuicConnection>,
    onopen_handler: Option<Box<dyn FnMut(& mut QuicConnection) + Send>>,
}

impl QuicClient {
    pub fn new() -> Self {
        Self {
            address: None,
            socket: None,
            connection: None,
            onopen_handler: None,
        }
    }
    pub fn set_address(&mut self, address: SocketAddr) -> &mut Self {
        self.address = Some(address);
        self
    }
    pub fn connect(&mut self) -> Result<(), String> {
        if let Some(addr) = self.address {
            let socket = if addr.is_ipv4() {
                Socket::new(AfInet, SockDgram, IpProtoUdp).unwrap()
            } else {
                Socket::new(AfInet6, SockDgram, IpProtoUdp).unwrap()
            };
            socket.bind(&addr).map_err(|e| e.to_string())?;
            self.socket = Some(socket);

            Ok(())
        } else {
            Err("Address not set".to_string())
        }
    }

    
    pub fn on_open<F>(&mut self, h: F)
    where
        F: FnMut(& mut QuicConnection) + Send + 'static,
    {
        self.onopen_handler = Some(Box::new(h));
    }

    pub fn open_bistream(&mut self) -> Result<QuicStream, String> {
        if let Some(connection) = &mut self.connection {
            connection.open_bistream()
        } else {
            Err("Connection not established".to_string())
        }
    }
}