Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement dgram for unix socket #164

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

HeartLinked
Copy link

No description provided.

@HeartLinked HeartLinked requested review from lhw2002426 and ken4647 and removed request for ken4647 January 18, 2025 13:30
}

/// Get inode from addr, if not exist, create a new unix socket file
fn get_or_create_inode(&mut self, addr: &SocketAddrUnix) -> Result<usize, LinuxError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fn get_inode , it will create file if not exist also, you can del one of them

Copy link
Author

@HeartLinked HeartLinked Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon closer inspection, I realized that this function is actually a wrapper around the two functions you previously wrote, get_inode and create_socket_file, rather than a direct replacement. In fact, this portion of the code was originally written directly within the bind function. However, since I noticed that the same code was needed in both the stream and dgram match arms, I extracted it and encapsulated it into a separate function. It's possible that the function name and comments I provided were not clear enough, leading to the misunderstanding. I have now updated the function's name and comment to: "Finds or creates the inode associated with the SocketAddrUnix address and updates the handle related to the socket." This should help avoid any further confusion.

unimplemented!()
}

/// Returns the file descriptor for the socket.
fn fd(&self) -> c_int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this func return unix_table index (inode number) instread of real file discriptor, I think there will be some problems?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is not referenced elsewhere, and I can't recall why I added it in the first place. Alright, so I've gone ahead and deleted it, haha.

pub addr: Mutex<SocketAddrUnix>,
pub buf: SocketBuffer<'a>,
pub peer_socket: Option<usize>,
pub status: UnixSocketStatus,

/// DGRAM socket, use a queue to store (source address, datagram).
pub datagram_queue: VecDeque<(SocketAddrUnix, Vec<u8>)>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already a buf, is there necessary to use datagram_queue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the DGRAM type, each message is an independent unit of data, known as a datagram. Typically, these messages are discrete, do not span across buffers, and each message must be tagged with its source address. This is distinctly different from STREAM sockets, which are connection-oriented and transmit data in a continuous stream. Therefore, I have designed a message queue that includes both the source address and the datagram for each message. I believe this approach should be appropriate, don't you think?

target_inner
.datagram_queue
.push_back((source_addr, buf.to_vec()));
target_inner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why there push twice?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mistake was likely caused by an oversight on my part. fixed.

return Err(LinuxError::EAGAIN);
} else {
// block until data is available
drop(inner);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can drop(inner) able to drop lock on uinx_table?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might not have a fully comprehensive understanding of Rust's locking mechanism, but here's my take: socket_inner is of type Arc<Mutex>. The inner variable acquires the Mutex lock on UnixSocketInner via socket_inner.lock(), and drop(inner) will release the lock on socket_inner. Additionally, there's no need to explicitly release the lock on UNIX_TABLE because Rust will automatically release it when its scope ends. So, I believe there shouldn't be an issue here, right?

Copy link
Contributor

@lhw2002426 lhw2002426 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rebase two commits into one

@lhw2002426 lhw2002426 merged commit eb2cb0a into syswonder:dev Jan 22, 2025
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants