//! Transport-layer implementation of the KDE Connect protocol.
//!
//! As a library user, you should ignore this module as it's only useful to develop other IO
//! backends.
//!
//! Both UDP and MDNS are used as discovery mechanisms. They both broadcast discovery messages
//! at specified intervals and listen to these messages. When a device is found, a TCP connection
//! is established to it (or it establishes a TCP connection to this device) which is upgraded into
//! a TLS connection used to send and receive the application packets defined in the specification.
//! The connection remains active until the other device closes the connection or this device decides
//! to close the connection itself.
#[cfg(feature = "std")]
use std::sync::Arc;

#[cfg(not(feature = "std"))]
use alloc::sync::Arc;

use crate::{device::Device, io::{IoImpl, KnownFunctionName, TcpListenerImpl, TcpStreamImpl, TlsStreamImpl, UdpSocketImpl}};

pub mod mdns;
pub mod tcp;
pub mod udp;
pub(crate) mod tls;

pub(crate) fn start_discovering<
    Io: IoImpl<UdpSocket, TcpStream, TcpListener, TlsStream> + Unpin + 'static,
    UdpSocket: UdpSocketImpl + Unpin + 'static,
    TcpStream: TcpStreamImpl + Unpin + 'static,
    TcpListener: TcpListenerImpl<TcpStream> + Unpin + 'static,
    TlsStream: TlsStreamImpl + Unpin + 'static,
>(
    device: Arc<Device<Io, UdpSocket, TcpStream, TcpListener, TlsStream>>,
) {
    Arc::clone(&device).io_impl.spawn(KnownFunctionName::SetupUdp, Arc::clone(&device));
    Arc::clone(&device).io_impl.spawn(KnownFunctionName::SetupMdns, device);
}
