Skip to content

Commit

Permalink
Add methods taking file descriptor as OwnedFd. (#641)
Browse files Browse the repository at this point in the history
The OwnedFd type confers ownership of the file descriptor, so these can
be safe methods rather than relying on the caller not to use the file
descriptor anywhere else after making the call.

Signed-off-by: Andrew Walbran <[email protected]>
  • Loading branch information
qwandor authored May 7, 2024
1 parent 2057a21 commit 3c2dbca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl ChannelBuilder {
self.build_args()
}

/// Build an [`Channel`] that connects to a specific address.
/// Build a [`Channel`] that connects to a specific address.
pub fn connect(mut self, addr: &str) -> Channel {
let args = self.prepare_connect_args();
let addr = CString::new(addr).unwrap();
Expand All @@ -509,7 +509,7 @@ impl ChannelBuilder {
unsafe { Channel::new(self.env.pick_cq(), self.env, channel) }
}

/// Build an [`Channel`] taking over an established connection from
/// Build a [`Channel`] taking over an established connection from
/// a file descriptor. The target string given is purely informative to
/// describe the endpoint of the connection. Takes ownership of the given
/// file descriptor and will close it when the connection is closed.
Expand All @@ -535,6 +535,22 @@ impl ChannelBuilder {

Channel::new(self.env.pick_cq(), self.env, channel)
}

/// Build a [`Channel`] taking over an established connection from a file
/// descriptor. The target string given is purely informative to describe
/// the endpoint of the connection. Takes ownership of the given file
/// descriptor and will close it when the connection is closed. The file
/// descriptor must correspond to a connected stream socket.
///
/// This function is available on posix systems only.
#[cfg(unix)]
pub fn connect_from_ownedfd(self, target: &str, fd: ::std::os::fd::OwnedFd) -> Channel {
use std::os::fd::IntoRawFd;

// SAFETY: `OwnedFd` confers ownership of the file descriptor, so it
// won't be accessed by other code.
unsafe { self.connect_from_fd(target, fd.into_raw_fd()) }
}
}

#[cfg(feature = "_secure")]
Expand Down
13 changes: 13 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,19 @@ impl Server {
let mut creds = ServerCredentials::insecure();
grpcio_sys::grpc_server_add_channel_from_fd(self.core.server, fd, creds.as_mut_ptr())
}

/// Add an RPC channel for an established connection represented as a file
/// descriptor. The file descriptor must correspond to a connected stream
/// socket. Takes ownership of the file descriptor, closing it when channel
/// is closed.
#[cfg(unix)]
pub fn add_channel_from_ownedfd(&mut self, fd: ::std::os::fd::OwnedFd) {
use std::os::fd::IntoRawFd;

// SAFETY: `OwnedFd` confers ownership of the file descriptor, so it
// won't be accessed by other code.
unsafe { self.add_channel_from_fd(fd.into_raw_fd()) }
}
}

impl Drop for Server {
Expand Down

0 comments on commit 3c2dbca

Please sign in to comment.