use smoltcp;

pub mod raw;
// pub mod icmp;
pub mod common;
pub mod datagram;
pub mod stream;
pub mod syscall;

pub use common::BoundInner;
pub use common::Types;
pub use datagram::UdpSocket;
pub use raw::RawSocket;

use smoltcp::wire::IpAddress;
use smoltcp::wire::IpEndpoint;
use smoltcp::wire::Ipv4Address;
use smoltcp::wire::Ipv6Address;

pub use stream::TcpSocket;
// pub use syscall::Inet;

use crate::filesystem::epoll::event_poll::EventPoll;

use super::Socket;

/// A local endpoint, which indicates that the local endpoint is unspecified.
///
/// According to the Linux man pages and the Linux implementation, `getsockname()` will _not_ fail
/// even if the socket is unbound. Instead, it will return an unspecified socket address. This
/// unspecified endpoint helps with that.
pub const UNSPECIFIED_LOCAL_ENDPOINT_V4: IpEndpoint =
    IpEndpoint::new(IpAddress::Ipv4(Ipv4Address::UNSPECIFIED), 0);
pub const UNSPECIFIED_LOCAL_ENDPOINT_V6: IpEndpoint =
    IpEndpoint::new(IpAddress::Ipv6(Ipv6Address::UNSPECIFIED), 0);

pub trait InetSocket: Socket {
    /// `on_iface_events`
    /// 通知socket发生的事件
    fn on_iface_events(&self);

    fn notify(&self) {
        self.on_iface_events();

        // Wake threads blocked on this socket (send/recv/connect/accept).
        // Without this, only epoll waiters are woken and blocking syscalls can hang.
        let _woken = self.wait_queue().wake_all();

        let events = self.check_io_event();
        if let Err(e) = EventPoll::wakeup_epoll(self.epoll_items().as_ref(), events) {
            log::warn!(
                "InetSocket::notify: wakeup_epoll failed: {:?}, events={:?}",
                e,
                events
            );
        }
    }
}
