Skip to content

Commit

Permalink
Merge pull request #1128 from zeenix/split-method
Browse files Browse the repository at this point in the history
♻️  zb: Split a big internal method a bit
  • Loading branch information
zeenix authored Nov 5, 2024
2 parents 40ea8c1 + 8d0a06c commit 538c974
Showing 1 changed file with 61 additions and 57 deletions.
118 changes: 61 additions & 57 deletions zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,63 +351,7 @@ impl<'a> Builder<'a> {
#[cfg(not(feature = "p2p"))]
let is_bus_conn = true;

#[cfg(not(feature = "bus-impl"))]
let unique_name = None;
#[cfg(feature = "bus-impl")]
let unique_name = self.unique_name.take().map(Into::into);

#[allow(unused_mut)]
let (mut stream, server_guid, authenticated) = self.target_connect().await?;
let mut auth = if authenticated {
let (socket_read, socket_write) = stream.take();
Authenticated {
#[cfg(unix)]
cap_unix_fd: socket_read.can_pass_unix_fd(),
socket_read: Some(socket_read),
socket_write,
// SAFETY: `server_guid` is provided as arg of `Builder::authenticated_socket`.
server_guid: server_guid.unwrap(),
already_received_bytes: vec![],
unique_name,
#[cfg(unix)]
already_received_fds: vec![],
}
} else {
#[cfg(feature = "p2p")]
match self.guid {
None => {
// SASL Handshake
Authenticated::client(stream, server_guid, self.auth_mechanism, is_bus_conn)
.await?
}
Some(guid) => {
if !self.p2p {
return Err(Error::Unsupported);
}

let creds = stream.read_mut().peer_credentials().await?;
#[cfg(unix)]
let client_uid = creds.unix_user_id();
#[cfg(windows)]
let client_sid = creds.into_windows_sid();

Authenticated::server(
stream,
guid.to_owned().into(),
#[cfg(unix)]
client_uid,
#[cfg(windows)]
client_sid,
self.auth_mechanism,
unique_name,
)
.await?
}
}

#[cfg(not(feature = "p2p"))]
Authenticated::client(stream, server_guid, self.auth_mechanism, is_bus_conn).await?
};
let mut auth = self.connect(is_bus_conn).await?;

// SAFETY: `Authenticated` is always built with these fields set to `Some`.
let socket_read = auth.socket_read.take().unwrap();
Expand Down Expand Up @@ -469,6 +413,66 @@ impl<'a> Builder<'a> {
}
}

async fn connect(&mut self, is_bus_conn: bool) -> Result<Authenticated> {
#[cfg(not(feature = "bus-impl"))]
let unique_name = None;
#[cfg(feature = "bus-impl")]
let unique_name = self.unique_name.take().map(Into::into);

#[allow(unused_mut)]
let (mut stream, server_guid, authenticated) = self.target_connect().await?;
if authenticated {
let (socket_read, socket_write) = stream.take();
Ok(Authenticated {
#[cfg(unix)]
cap_unix_fd: socket_read.can_pass_unix_fd(),
socket_read: Some(socket_read),
socket_write,
// SAFETY: `server_guid` is provided as arg of `Builder::authenticated_socket`.
server_guid: server_guid.unwrap(),
already_received_bytes: vec![],
unique_name,
#[cfg(unix)]
already_received_fds: vec![],
})
} else {
#[cfg(feature = "p2p")]
match self.guid.take() {
None => {
// SASL Handshake
Authenticated::client(stream, server_guid, self.auth_mechanism, is_bus_conn)
.await
}
Some(guid) => {
if !self.p2p {
return Err(Error::Unsupported);
}

let creds = stream.read_mut().peer_credentials().await?;
#[cfg(unix)]
let client_uid = creds.unix_user_id();
#[cfg(windows)]
let client_sid = creds.into_windows_sid();

Authenticated::server(
stream,
guid.to_owned().into(),
#[cfg(unix)]
client_uid,
#[cfg(windows)]
client_sid,
self.auth_mechanism,
unique_name,
)
.await
}
}

#[cfg(not(feature = "p2p"))]
Authenticated::client(stream, server_guid, self.auth_mechanism, is_bus_conn).await
}
}

async fn target_connect(&mut self) -> Result<(BoxedSplit, Option<OwnedGuid>, bool)> {
let mut authenticated = false;
let mut guid = None;
Expand Down

0 comments on commit 538c974

Please sign in to comment.