-
Notifications
You must be signed in to change notification settings - Fork 45
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
added a 100ms sleep to the keep alive system to prevent it from having excessive cpu usage #117
Changes from all commits
8fa61ee
b856ba6
04ef7ae
f16adf0
6399270
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
use crate::packets::outgoing::keep_alive::KeepAlive; | ||
use crate::packets::outgoing::keep_alive::OutgoingKeepAlivePacket; | ||
use crate::packets::IncomingPacket; | ||
use crate::{NetResult, ServerState}; | ||
use ferrumc_ecs::components::storage::ComponentRefMut; | ||
use ferrumc_ecs::errors::ECSError; | ||
use ferrumc_macros::{packet, NetDecode}; | ||
use std::sync::Arc; | ||
use tracing::debug; | ||
use tracing::{debug, warn}; | ||
|
||
#[derive(NetDecode)] | ||
#[packet(packet_id = 0x18, state = "play")] | ||
|
@@ -13,15 +15,42 @@ pub struct IncomingKeepAlivePacket { | |
|
||
impl IncomingPacket for IncomingKeepAlivePacket { | ||
async fn handle(self, conn_id: usize, state: Arc<ServerState>) -> NetResult<()> { | ||
let mut last_keep_alive = state.universe.get_mut::<KeepAlive>(conn_id)?; | ||
// TODO handle errors. | ||
let last_keep_alive = state.universe.get_mut::<OutgoingKeepAlivePacket>(conn_id)?; | ||
|
||
if self.id != last_keep_alive.id { | ||
debug!( | ||
"Invalid keep alive packet received from {:?} with id {:?} (expected {:?})", | ||
"Invalid keep alive packet received from entity {:?} with id {:?} (expected {:?})", | ||
conn_id, self.id, last_keep_alive.id | ||
); | ||
return NetResult::Err(crate::errors::NetError::Packet( | ||
crate::errors::PacketError::InvalidState(0x18), | ||
)); | ||
// TODO Kick player | ||
} | ||
|
||
let result = state.universe.get_mut::<IncomingKeepAlivePacket>(conn_id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the problem happens I'm guessing. |
||
|
||
if result.is_err() { | ||
let err = result.as_ref().err().unwrap(); | ||
if matches!(err, ECSError::ComponentTypeNotFound) { | ||
state | ||
.universe | ||
.add_component(conn_id, IncomingKeepAlivePacket { id: self.id })?; | ||
let mut last_received_keep_alive = state.universe.get_mut(conn_id)?; | ||
*last_received_keep_alive = self; | ||
} else { | ||
warn!( | ||
"Failed to get or create <IncomingKeepAlive> component: {:?}", | ||
err | ||
); | ||
return Err(crate::errors::NetError::ECSError(result.err().unwrap())); | ||
} | ||
} else { | ||
*last_keep_alive = KeepAlive::from(self.id); | ||
let mut last_received_keep_alive: ComponentRefMut<'_, IncomingKeepAlivePacket> = | ||
result.unwrap(); | ||
|
||
*last_received_keep_alive = self; | ||
Comment on lines
+33
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just do a match statement? match result {
Ok(component) => {
// ...
},
Err(e) => {
// ...
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesnt fix the problem of the error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. state.universe.get_mut::<IncomingKeepAlivePacket>(conn_id) is the culprit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be a deadlock from dashmap? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If something stupid is being done, probably. I haven't read the code in too much depth, but this shouldn't be the case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it wasnt, I think the error system is a bit inconsistent, I expected only ComponentTypeNotFound, but also got ComponentRetrievalError, when they're both reporting what should be the same error since the IncomingKeepAlive hasn't been created yet, I've fixed this in #122 |
||
} | ||
|
||
Ok(()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,18 @@ | ||
use ferrumc_macros::{packet, NetEncode}; | ||
use std::io::Write; | ||
|
||
#[derive(Debug, NetEncode)] | ||
pub struct KeepAlive { | ||
pub id: i64, | ||
} | ||
|
||
mod adapters { | ||
impl From<i64> for super::KeepAlive { | ||
fn from(id: i64) -> Self { | ||
Self { id } | ||
} | ||
} | ||
} | ||
|
||
#[derive(NetEncode)] | ||
#[packet(packet_id = 0x26)] | ||
pub struct KeepAlivePacket { | ||
pub id: KeepAlive, | ||
pub struct OutgoingKeepAlivePacket { | ||
pub id: i64, | ||
} | ||
|
||
impl Default for KeepAlivePacket { | ||
impl Default for OutgoingKeepAlivePacket { | ||
fn default() -> Self { | ||
let current_ms = std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.expect("Time went backwards?? LMAO") | ||
.as_millis() as i64; | ||
Self::new(current_ms) | ||
} | ||
} | ||
|
||
impl KeepAlivePacket { | ||
pub fn new(id: i64) -> Self { | ||
Self { | ||
id: KeepAlive::from(id), | ||
} | ||
Self { id: current_ms } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
15 seconds delay means the client gets 2 chances. The default timeout for keepalive is 30 seconds.