alnyan/yggrdasil: basic TcpListener/TcpSocket operation
This commit is contained in:
@@ -168,6 +168,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
|
||||
|
||||
// Network
|
||||
Ok(OsError::HostUnreachable) => ErrorKind::HostUnreachable,
|
||||
Ok(OsError::NetworkUnreachable) => ErrorKind::NetworkUnreachable,
|
||||
|
||||
// Uncategorized
|
||||
Ok(OsError::InvalidFile) => ErrorKind::Uncategorized,
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
use crate::io;
|
||||
use crate::mem::MaybeUninit;
|
||||
use crate::net::SocketAddr;
|
||||
use crate::os::fd::{AsRawFd, FromRawFd};
|
||||
use crate::sys::cvt_io;
|
||||
use crate::sys::fd::FileDesc;
|
||||
|
||||
use yggdrasil_rt::{net::SocketType, sys as syscall};
|
||||
|
||||
use super::TcpStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TcpListener(!);
|
||||
pub struct TcpListener {
|
||||
local: SocketAddr,
|
||||
fd: FileDesc,
|
||||
}
|
||||
|
||||
impl TcpListener {
|
||||
pub fn bind(_addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
|
||||
todo!()
|
||||
pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
|
||||
let addr = addr?;
|
||||
let raw = cvt_io(unsafe { syscall::bind_socket(addr, SocketType::TcpStream) })?;
|
||||
let fd = unsafe { FileDesc::from_raw_fd(raw) };
|
||||
Ok(Self { local: *addr, fd })
|
||||
}
|
||||
|
||||
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
|
||||
todo!()
|
||||
Ok(self.local)
|
||||
}
|
||||
|
||||
pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
|
||||
todo!()
|
||||
let mut remote = MaybeUninit::uninit();
|
||||
let raw = cvt_io(unsafe { syscall::accept(self.fd.as_raw_fd(), &mut remote) })?;
|
||||
let remote = unsafe { remote.assume_init() };
|
||||
let stream = unsafe { TcpStream::from_raw_parts(raw, self.local, remote) };
|
||||
Ok((stream, remote))
|
||||
}
|
||||
|
||||
pub fn duplicate(&self) -> io::Result<TcpListener> {
|
||||
|
||||
@@ -1,11 +1,30 @@
|
||||
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
|
||||
use crate::mem::MaybeUninit;
|
||||
use crate::net::{Shutdown, SocketAddr};
|
||||
use crate::os::fd::{AsRawFd, FromRawFd, RawFd};
|
||||
use crate::sys::cvt_io;
|
||||
use crate::sys::fd::FileDesc;
|
||||
use crate::time::Duration;
|
||||
|
||||
use yggdrasil_rt::sys as syscall;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TcpStream(!);
|
||||
pub struct TcpStream {
|
||||
local: SocketAddr,
|
||||
remote: SocketAddr,
|
||||
fd: FileDesc,
|
||||
}
|
||||
|
||||
impl TcpStream {
|
||||
pub(crate) unsafe fn from_raw_parts(
|
||||
raw: RawFd,
|
||||
local: SocketAddr,
|
||||
remote: SocketAddr,
|
||||
) -> TcpStream {
|
||||
let fd = unsafe { FileDesc::from_raw_fd(raw) };
|
||||
Self { local, remote, fd }
|
||||
}
|
||||
|
||||
pub fn connect(_addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
|
||||
todo!()
|
||||
}
|
||||
@@ -34,8 +53,9 @@ impl TcpStream {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn read(&self, _buf: &mut [u8]) -> io::Result<usize> {
|
||||
todo!()
|
||||
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let mut ignore = MaybeUninit::uninit();
|
||||
cvt_io(unsafe { syscall::receive_from(self.fd.as_raw_fd(), buf, &mut ignore) })
|
||||
}
|
||||
|
||||
pub fn read_buf(&self, _buf: BorrowedCursor<'_>) -> io::Result<()> {
|
||||
@@ -51,8 +71,8 @@ impl TcpStream {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn write(&self, _buf: &[u8]) -> io::Result<usize> {
|
||||
todo!()
|
||||
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
||||
cvt_io(unsafe { syscall::send_to(self.fd.as_raw_fd(), buf, &None) })
|
||||
}
|
||||
|
||||
pub fn write_vectored(&self, _bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
|
||||
Reference in New Issue
Block a user