Skip to content

Commit

Permalink
Renet, Renetcode: add client.is_connecting()
Browse files Browse the repository at this point in the history
Rename in renetcode client.connected() to client.is_connected() for consistency.
  • Loading branch information
lucaspoffo committed Mar 12, 2023
1 parent 7b0292c commit 88834d4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bevy_renet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl RenetClientPlugin {

pub fn client_connecting(client: Option<Res<RenetClient>>) -> bool {
match client {
Some(client) => !client.is_connected(),
Some(client) => client.is_connecting(),
None => false,
}
}
Expand Down
8 changes: 6 additions & 2 deletions renet/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ impl RenetClient {
self.netcode_client.client_id()
}

pub fn is_connecting(&self) -> bool {
self.netcode_client.is_connecting()
}

pub fn is_connected(&self) -> bool {
self.netcode_client.connected()
self.netcode_client.is_connected()
}

/// If the client is disconnected, returns the reason.
Expand Down Expand Up @@ -161,7 +165,7 @@ impl RenetClient {

/// Send packets to the server.
pub fn send_packets(&mut self) -> Result<(), RenetError> {
if self.netcode_client.connected() {
if self.netcode_client.is_connected() {
let packets = self.reliable_connection.get_packets_to_send()?;
for packet in packets.into_iter() {
let (addr, payload) = self.netcode_client.generate_payload_packet(&packet)?;
Expand Down
2 changes: 1 addition & 1 deletion renetcode/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn client(connect_token: ConnectToken) {

match stdin_channel.try_recv() {
Ok(text) => {
if client.connected() {
if client.is_connected() {
let (addr, payload) = client.generate_payload_packet(text.as_bytes()).unwrap();
udp_socket.send_to(payload, addr).unwrap();
} else {
Expand Down
9 changes: 8 additions & 1 deletion renetcode/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ impl NetcodeClient {
}
}

pub fn connected(&self) -> bool {
pub fn is_connecting(&self) -> bool {
matches!(
self.state,
ClientState::SendingConnectionRequest | ClientState::SendingConnectionResponse
)
}

pub fn is_connected(&self) -> bool {
self.state == ClientState::Connected
}

Expand Down
8 changes: 4 additions & 4 deletions renetcode/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ mod tests {
_ => unreachable!(),
};

assert!(!client.connected());
assert!(!client.is_connected());
let (client_packet, _) = client.update(Duration::ZERO).unwrap();
let result = server.process_packet(client_addr, client_packet);

Expand All @@ -724,7 +724,7 @@ mod tests {
_ => unreachable!(),
};

assert!(client.connected());
assert!(client.is_connected());

for _ in 0..3 {
let payload = [7u8; 300];
Expand Down Expand Up @@ -762,9 +762,9 @@ mod tests {
ServerResult::ClientDisconnected {
payload: Some(payload), ..
} => {
assert!(client.connected());
assert!(client.is_connected());
assert!(client.process_packet(payload).is_none());
assert!(!client.connected());
assert!(!client.is_connected());
}
_ => unreachable!(),
}
Expand Down

0 comments on commit 88834d4

Please sign in to comment.