|
|
|
|
@@ -3,27 +3,42 @@
|
|
|
|
|
//!
|
|
|
|
|
//! # Exports
|
|
|
|
|
//!
|
|
|
|
|
//! See [`lb_libnet`] for items exported by this library.
|
|
|
|
|
//! See [`lb_netlib`] for items exported by this library.
|
|
|
|
|
use derive_more::{From, FromStr};
|
|
|
|
|
use luaffi::{cdef, metatype};
|
|
|
|
|
use std::{
|
|
|
|
|
io,
|
|
|
|
|
net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
|
|
|
|
|
};
|
|
|
|
|
use std::net::{AddrParseError, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
use tokio::net::{TcpListener, TcpSocket, TcpStream};
|
|
|
|
|
|
|
|
|
|
/// Errors that can be thrown by this library.
|
|
|
|
|
///
|
|
|
|
|
/// Functions which return this error will **throw** in Lua. The error message can be caught by
|
|
|
|
|
/// using [`pcall(f, ...)`](https://www.lua.org/manual/5.1/manual.html#pdf-pcall).
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum Error {
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
#[error("{0}")]
|
|
|
|
|
InvalidAddr(#[from] AddrParseError),
|
|
|
|
|
#[error("socket was already converted")]
|
|
|
|
|
SocketConsumed,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
|
|
/// Items exported by the `lb:net` library.
|
|
|
|
|
///
|
|
|
|
|
/// This library can be obtained by calling `require` in Lua.
|
|
|
|
|
/// This library can be obtained by calling
|
|
|
|
|
/// [`require("lb:net")`](https://www.lua.org/manual/5.1/manual.html#pdf-require) in Lua.
|
|
|
|
|
///
|
|
|
|
|
/// ```lua
|
|
|
|
|
/// local net = require("lb:net");
|
|
|
|
|
/// ```
|
|
|
|
|
#[cdef]
|
|
|
|
|
pub struct lb_libnet;
|
|
|
|
|
pub struct lb_netlib;
|
|
|
|
|
|
|
|
|
|
#[metatype]
|
|
|
|
|
impl lb_libnet {
|
|
|
|
|
impl lb_netlib {
|
|
|
|
|
#[new]
|
|
|
|
|
extern "Lua-C" fn new() -> Self {
|
|
|
|
|
Self
|
|
|
|
|
@@ -59,11 +74,7 @@ impl lb_libnet {
|
|
|
|
|
/// If `s` is an [`lb_ipaddr`], a copy of that value is returned. If `s` is an
|
|
|
|
|
/// [`lb_socketaddr`], the IP address part of the socket address is returned. Otherwise, parses
|
|
|
|
|
/// `s` as an IP address string. Both IPv4 or IPv6 addresses are supported.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// Throws if `s` cannot be parsed as an IP address.
|
|
|
|
|
pub extern "Lua" fn ipaddr(&self, s: any) -> lb_ipaddr {
|
|
|
|
|
pub extern "Lua" fn ipaddr(&self, s: any) -> Result<lb_ipaddr> {
|
|
|
|
|
if __istype(__ct.lb_ipaddr, s) {
|
|
|
|
|
__new(__ct.lb_ipaddr, s) // copy constructor
|
|
|
|
|
} else if __istype(__ct.lb_socketaddr, s) {
|
|
|
|
|
@@ -73,8 +84,8 @@ impl lb_libnet {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "Lua-C" fn __parse_ipaddr(&self, s: &str) -> Result<lb_ipaddr, AddrParseError> {
|
|
|
|
|
s.parse()
|
|
|
|
|
extern "Lua-C" fn __parse_ipaddr(&self, s: &str) -> Result<lb_ipaddr> {
|
|
|
|
|
Ok(s.parse()?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates an [`lb_socketaddr`] from the given input.
|
|
|
|
|
@@ -86,11 +97,7 @@ impl lb_libnet {
|
|
|
|
|
/// socket address string. Both IPv4 and IPv6 addresses are supported.
|
|
|
|
|
///
|
|
|
|
|
/// If `port` is not specified, `0` is used as the default.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// Throws if `s` cannot be parsed as an IP or socket address.
|
|
|
|
|
pub extern "Lua" fn socketaddr(&self, s: any, port: any) -> lb_socketaddr {
|
|
|
|
|
pub extern "Lua" fn socketaddr(&self, s: any, port: any) -> Result<lb_socketaddr> {
|
|
|
|
|
if port != () {
|
|
|
|
|
self.__new_socketaddr(self.ipaddr(s), port)
|
|
|
|
|
} else {
|
|
|
|
|
@@ -108,30 +115,22 @@ impl lb_libnet {
|
|
|
|
|
SocketAddr::new(ip.0, port).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr, AddrParseError> {
|
|
|
|
|
s.parse()
|
|
|
|
|
extern "Lua-C" fn __parse_socketaddr(&self, s: &str) -> Result<lb_socketaddr> {
|
|
|
|
|
Ok(s.parse()?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new TCP socket configured for IPv4.
|
|
|
|
|
///
|
|
|
|
|
/// See [`TcpSocket::new_v4`].
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// Throws if an error was encountered during the socket creation.
|
|
|
|
|
pub extern "Lua-C" fn tcp_v4(&self) -> io::Result<lb_tcpsocket> {
|
|
|
|
|
TcpSocket::new_v4().map(lb_tcpsocket)
|
|
|
|
|
pub extern "Lua-C" fn tcp_v4(&self) -> Result<lb_tcpsocket> {
|
|
|
|
|
Ok(Some(TcpSocket::new_v4()?).into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new TCP socket configured for IPv6.
|
|
|
|
|
///
|
|
|
|
|
/// See [`TcpSocket::new_v6`].
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// Throws if an error was encountered during the socket creation.
|
|
|
|
|
pub extern "Lua-C" fn tcp_v6(&self) -> io::Result<lb_tcpsocket> {
|
|
|
|
|
TcpSocket::new_v6().map(lb_tcpsocket)
|
|
|
|
|
pub extern "Lua-C" fn tcp_v6(&self) -> Result<lb_tcpsocket> {
|
|
|
|
|
Ok(Some(TcpSocket::new_v6()?).into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -274,7 +273,7 @@ impl lb_socketaddr {
|
|
|
|
|
|
|
|
|
|
/// Sets the IP part of this address.
|
|
|
|
|
///
|
|
|
|
|
/// This function accepts the same arguments as [`ipaddr`](lb_libnet::ipaddr).
|
|
|
|
|
/// This function accepts the same arguments as [`ipaddr`](lb_netlib::ipaddr).
|
|
|
|
|
pub extern "Lua" fn set_ip(&mut self, s: any) -> &mut Self {
|
|
|
|
|
if __istype(__ct.lb_ipaddr, s) {
|
|
|
|
|
self.__set_ip(s);
|
|
|
|
|
@@ -290,8 +289,8 @@ impl lb_socketaddr {
|
|
|
|
|
self.0.set_ip(ip.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<(), AddrParseError> {
|
|
|
|
|
s.parse().map(|ip| self.0.set_ip(ip))
|
|
|
|
|
extern "Lua-C" fn __set_ip_parse(&mut self, s: &str) -> Result<()> {
|
|
|
|
|
Ok(self.0.set_ip(s.parse()?))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the port part of this address.
|
|
|
|
|
@@ -300,7 +299,7 @@ impl lb_socketaddr {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the port part of this address.
|
|
|
|
|
pub extern "Lua" fn set_port(&mut self, port: number) -> &mut Self {
|
|
|
|
|
pub extern "Lua" fn set_port(&mut self, port: integer) -> &mut Self {
|
|
|
|
|
self.__set_port(port);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
@@ -316,13 +315,17 @@ impl lb_socketaddr {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A TCP socket which has not yet been converted to a [`lb_tcpstream`] or [`lb_tcplistener`].
|
|
|
|
|
/// A TCP socket which has not yet been converted to an [`lb_tcpstream`] or [`lb_tcplistener`].
|
|
|
|
|
#[derive(Debug, From)]
|
|
|
|
|
#[cdef]
|
|
|
|
|
pub struct lb_tcpsocket(#[opaque] TcpSocket);
|
|
|
|
|
pub struct lb_tcpsocket(#[opaque] Option<TcpSocket>);
|
|
|
|
|
|
|
|
|
|
#[metatype]
|
|
|
|
|
impl lb_tcpsocket {}
|
|
|
|
|
impl lb_tcpsocket {
|
|
|
|
|
pub extern "Lua-C" fn keepalive(&self) -> Result<bool> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, From)]
|
|
|
|
|
#[cdef]
|
|
|
|
|
|