Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 302
Refactor part 4#124
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Refactor part 4 #124
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
afe4ed7
Update Cargo.toml
Thomasdezeeuw f9e738f
Put Socket::set_nosigpipe behind the all feature
Thomasdezeeuw 8e05ced
Refactor Socket::local_addr
Thomasdezeeuw 053735f
Refactor Socket::peer_addr
Thomasdezeeuw 0c002e8
Put Socket::set_no_inherit behind the all feature
Thomasdezeeuw e887d43
Refactor Socket::try_clone
Thomasdezeeuw 05bae6d
Add Type::no_inherit
Thomasdezeeuw cbbd77d
Refactor Socket::take_error
Thomasdezeeuw e024a11
Refactor Socket::set_nonblocking
Thomasdezeeuw dc7b1ad
Refactor Socket::shutdown
Thomasdezeeuw 27f93eb
Remove web-programming from Cargo's categories
Thomasdezeeuw ea9d730
Rename Type::non_blocking to nonblocking
Thomasdezeeuw 282b6b8
Add note about atomicity of setting options on Socket
Thomasdezeeuw c346cf6
Rename assert_nonblocking test functions
Thomasdezeeuw 7f65390
Change Socket::set_cloexec to accept a bool
Thomasdezeeuw 05a3a82
Add test for Type::cloexec
Thomasdezeeuw 878e6b4
Change Socket::set_no_inherit signature
Thomasdezeeuw 77969dc
Fix Socket example
Thomasdezeeuw 75aa2ec
Fix set_no_inherit test
Thomasdezeeuw adb1424
Change Socket::set_nosigpipe to accept a bool
Thomasdezeeuw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,6 +41,14 @@ use crate::{Domain, Protocol, SockAddr, Type}; | ||
| /// [`TcpStream`]: std::net::TcpStream | ||
| /// [`UdpSocket`]: std::net::UdpSocket | ||
| /// | ||
| /// # Notes | ||
| /// | ||
| /// Some methods that set options on `Socket` require two system calls to set | ||
| /// there options without overwriting previously set options. We do this by | ||
| /// first getting the current settings, applying the desired changes and than | ||
| /// updating the settings. This means that the operation is **not** atomic. This | ||
| /// can lead to a data race when two threads are changing options in parallel. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Creating a new socket setting all advisable flags. | ||
| @@ -58,19 +66,20 @@ use crate::{Domain, Protocol, SockAddr, Type}; | ||
| /// #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd"))] | ||
| /// let ty = ty.cloexec(); | ||
| /// | ||
| /// // On windows set `WSA_FLAG_NO_HANDLE_INHERIT`. | ||
| /// #[cfg(windows)] | ||
| /// let ty = ty.no_inherit(); | ||
| /// | ||
| /// let socket = Socket::new(domain, ty, Some(protocol))?; | ||
| /// | ||
| /// // On platforms that don't support `SOCK_CLOEXEC`, use `FD_CLOEXEC`. | ||
| /// #[cfg(all(not(windows), not(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd"))))] | ||
| /// socket.set_cloexec()?; | ||
| /// socket.set_cloexec(true)?; | ||
| /// | ||
| /// // On macOS and iOS set `NOSIGPIPE`. | ||
| /// #[cfg(target_vendor = "apple")] | ||
| /// socket.set_nosigpipe()?; | ||
| /// socket.set_nosigpipe(true)?; | ||
| /// | ||
| /// // On windows set `HANDLE_FLAG_INHERIT`. | ||
| /// #[cfg(windows)] | ||
| /// socket.set_no_inherit()?; | ||
| /// # drop(socket); | ||
| /// # Ok(()) | ||
| /// # } | ||
| @@ -198,26 +207,44 @@ impl Socket { | ||
| sys::accept(self.inner).map(|(inner, addr)| (Socket { inner }, addr)) | ||
| } | ||
| /// Returns the socket address of the local half of this TCP connection. | ||
| /// Returns the socket address of the local half of this socket. | ||
| /// | ||
| /// # Notes | ||
| /// | ||
| /// Depending on the OS this may return an error if the socket is not | ||
| /// [bound]. | ||
| /// | ||
| /// [bound]: Socket::bind | ||
| pub fn local_addr(&self) -> io::Result<SockAddr> { | ||
| self.inner().local_addr() | ||
| sys::getsockname(self.inner) | ||
| } | ||
| /// Returns the socket address of the remote peer of this TCP connection. | ||
| /// Returns the socket address of the remote peer of this socket. | ||
| /// | ||
| /// # Notes | ||
| /// | ||
| /// This returns an error if the socket is not [`connect`ed]. | ||
| /// | ||
| /// [`connect`ed]: Socket::connect | ||
| pub fn peer_addr(&self) -> io::Result<SockAddr> { | ||
| self.inner().peer_addr() | ||
| sys::getpeername(self.inner) | ||
| } | ||
| /// Creates a new independently owned handle to the underlying socket. | ||
| /// | ||
| /// The returned `TcpStream` is a reference to the same stream that this | ||
| /// object references. Both handles will read and write the same stream of | ||
| /// data, and options set on one stream will be propagated to the other | ||
| /// stream. | ||
| /// # Notes | ||
| /// | ||
| /// On Unix this uses `F_DUPFD_CLOEXEC` and thus sets the `FD_CLOEXEC` on | ||
| /// the returned socket. | ||
| /// | ||
| /// On Windows this uses `WSA_FLAG_NO_HANDLE_INHERIT` setting inheriting to | ||
| /// false. | ||
| /// | ||
| /// On Windows this can **not** be used function cannot be used on a | ||
| /// QOS-enabled socket, see | ||
| /// <https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketw>. | ||
| pub fn try_clone(&self) -> io::Result<Socket> { | ||
| self.inner() | ||
| .try_clone() | ||
| .map(|s| Socket { inner: s.inner() }) | ||
| sys::try_clone(self.inner).map(|inner| Socket { inner }) | ||
| } | ||
| /// Get the value of the `SO_ERROR` option on this socket. | ||
| @@ -226,23 +253,27 @@ impl Socket { | ||
| /// the field in the process. This can be useful for checking errors between | ||
| /// calls. | ||
| pub fn take_error(&self) -> io::Result<Option<io::Error>> { | ||
| self.inner().take_error() | ||
| sys::take_error(self.inner) | ||
| } | ||
| /// Moves this TCP stream into or out of nonblocking mode. | ||
| /// | ||
| /// On Unix this corresponds to calling fcntl, and on Windows this | ||
| /// corresponds to calling ioctlsocket. | ||
| /// # Notes | ||
| /// | ||
| /// On Unix this corresponds to calling `fcntl` (un)setting `O_NONBLOCK`. | ||
| /// | ||
| /// On Windows this corresponds to calling `ioctlsocket` (un)setting | ||
| /// `FIONBIO`. | ||
Thomasdezeeuw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { | ||
| self.inner().set_nonblocking(nonblocking) | ||
| sys::set_nonblocking(self.inner, nonblocking) | ||
| } | ||
| /// Shuts down the read, write, or both halves of this connection. | ||
| /// | ||
| /// This function will cause all pending and future I/O on the specified | ||
| /// portions to return immediately with an appropriate value. | ||
| pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { | ||
| self.inner().shutdown(how) | ||
| sys::shutdown(self.inner, how) | ||
| } | ||
| /// Receives data on the socket from the remote address to which it is | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.