Skip to content

Commit

Permalink
Merge pull request #70 from lhw2002426/nginx
Browse files Browse the repository at this point in the history
Support MSG_PEEK, MSG_DONTWAIT for tcp recv.
  • Loading branch information
coolyjg authored Mar 25, 2024
2 parents 603bbba + ebf7954 commit 1529b73
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/arceos_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn ax_tcp_send(socket: &AxTcpSocketHandle, buf: &[u8]) -> AxResult<usize> {
}

pub fn ax_tcp_recv(socket: &AxTcpSocketHandle, buf: &mut [u8]) -> AxResult<usize> {
socket.0.recv(buf)
socket.0.recv(buf, 0)
}

pub fn ax_tcp_poll(socket: &AxTcpSocketHandle) -> AxResult<AxPollState> {
Expand Down
10 changes: 5 additions & 5 deletions api/ruxos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl Socket {
}
}

fn recv(&self, buf: &mut [u8]) -> LinuxResult<usize> {
fn recv(&self, buf: &mut [u8], flags: i32) -> LinuxResult<usize> {
match self {
Socket::Udp(udpsocket) => Ok(udpsocket.lock().recv_from(buf).map(|e| e.0)?),
Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf)?),
Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf, flags)?),
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ impl Socket {
.lock()
.recv_from(buf)
.map(|res| (res.0, Some(res.1)))?),
Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf).map(|res| (res, None))?),
Socket::Tcp(tcpsocket) => Ok(tcpsocket.lock().recv(buf, 0).map(|res| (res, None))?),
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ impl Socket {

impl FileLike for Socket {
fn read(&self, buf: &mut [u8]) -> LinuxResult<usize> {
self.recv(buf)
self.recv(buf, 0)
}

fn write(&self, buf: &[u8]) -> LinuxResult<usize> {
Expand Down Expand Up @@ -422,7 +422,7 @@ pub fn sys_recv(
return Err(LinuxError::EFAULT);
}
let buf = unsafe { core::slice::from_raw_parts_mut(buf_ptr as *mut u8, len) };
Socket::from_fd(socket_fd)?.recv(buf)
Socket::from_fd(socket_fd)?.recv(buf, flag)
})
}

Expand Down
28 changes: 22 additions & 6 deletions modules/axnet/src/smoltcp_impl/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const STATE_CONNECTING: u8 = 2;
const STATE_CONNECTED: u8 = 3;
const STATE_LISTENING: u8 = 4;

const MSG_OOB: i32 = 1;
const MSG_PEEK: i32 = 2;
const MSG_DONTWAIT: i32 = 4;
const MSG_CTRUNC: i32 = 8;

/// A TCP socket that provides POSIX-like APIs.
///
/// - [`connect`] is for TCP clients.
Expand Down Expand Up @@ -282,7 +287,7 @@ impl TcpSocket {
}

/// Receives data from the socket, stores it in the given buffer.
pub fn recv(&self, buf: &mut [u8]) -> AxResult<usize> {
pub fn recv(&self, buf: &mut [u8], flags: i32) -> AxResult<usize> {
if self.is_connecting() {
return Err(AxError::WouldBlock);
} else if !self.is_connected() {
Expand All @@ -302,10 +307,20 @@ impl TcpSocket {
} else if socket.recv_queue() > 0 {
// data available
// TODO: use socket.recv(|buf| {...})
let len = socket
.recv_slice(buf)
.map_err(|_| ax_err_type!(BadState, "socket recv() failed"))?;
Ok(len)
if flags & MSG_DONTWAIT != 0 {
self.set_nonblocking(true);
}
if flags & MSG_PEEK != 0 {
let len = socket
.peek_slice(buf)
.map_err(|_| ax_err_type!(BadState, "socket recv() failed"))?;
Ok(len)
} else {
let len = socket
.recv_slice(buf)
.map_err(|_| ax_err_type!(BadState, "socket recv() failed"))?;
Ok(len)
}
} else {
// no more data
Err(AxError::WouldBlock)
Expand All @@ -315,6 +330,7 @@ impl TcpSocket {
}

/// Transmits data in the given buffer.
/// TODO: impl send flags
pub fn send(&self, buf: &[u8]) -> AxResult<usize> {
if self.is_connecting() {
return Err(AxError::WouldBlock);
Expand Down Expand Up @@ -517,7 +533,7 @@ impl Drop for TcpSocket {

impl axio::Read for TcpSocket {
fn read(&mut self, buf: &mut [u8]) -> AxResult<usize> {
self.recv(buf)
self.recv(buf, 0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/rux9p/src/netdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl _9pDriverOps for Net9pDev {
return Err(0);
}
}
match self.socket.recv(outputs) {
match self.socket.recv(outputs, 0) {
Ok(length) => {
debug!("net9p recv successfully,length = {}", length);
Ok(length as u32)
Expand Down

0 comments on commit 1529b73

Please sign in to comment.