mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
refactor: resolve multiple mutable borrow conflicts in PTP event loop
Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
parent
85cd0cc7db
commit
d6ca055170
1 changed files with 91 additions and 8 deletions
99
src/ptp.rs
99
src/ptp.rs
|
|
@ -160,27 +160,110 @@ async fn run_ptp_session(
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle events and collect new actions
|
// Handle events and process actions immediately to avoid borrowing conflicts
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = tokio::time::sleep(Duration::from_millis(100)) => {
|
_ = tokio::time::sleep(Duration::from_millis(100)) => {
|
||||||
// Handle timer events sequentially to avoid multiple mutable borrows
|
// Handle timer events one by one and process actions immediately
|
||||||
actions.extend(running_port.handle_sync_timer());
|
for action in running_port.handle_sync_timer() {
|
||||||
actions.extend(running_port.handle_announce_timer(&mut NoForwardedTLVs));
|
match action {
|
||||||
actions.extend(running_port.handle_delay_request_timer());
|
PortAction::SendEvent { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:319";
|
||||||
|
if let Err(e) = event_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP event packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PortAction::SendGeneral { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:320";
|
||||||
|
if let Err(e) = general_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP general packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for action in running_port.handle_announce_timer(&mut NoForwardedTLVs) {
|
||||||
|
match action {
|
||||||
|
PortAction::SendEvent { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:319";
|
||||||
|
if let Err(e) = event_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP event packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PortAction::SendGeneral { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:320";
|
||||||
|
if let Err(e) = general_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP general packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for action in running_port.handle_delay_request_timer() {
|
||||||
|
match action {
|
||||||
|
PortAction::SendEvent { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:319";
|
||||||
|
if let Err(e) = event_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP event packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PortAction::SendGeneral { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:320";
|
||||||
|
if let Err(e) = general_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP general packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok((len, _source_address)) = event_socket.recv_from(&mut event_buf) => {
|
Ok((len, _source_address)) = event_socket.recv_from(&mut event_buf) => {
|
||||||
let receive_time = Time::from_nanos(std::time::SystemTime::now()
|
let receive_time = Time::from_nanos(std::time::SystemTime::now()
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_nanos() as u64);
|
.as_nanos() as u64);
|
||||||
actions.extend(running_port.handle_event_receive(&event_buf[..len], receive_time));
|
|
||||||
|
for action in running_port.handle_event_receive(&event_buf[..len], receive_time) {
|
||||||
|
match action {
|
||||||
|
PortAction::SendEvent { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:319";
|
||||||
|
if let Err(e) = event_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP event packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PortAction::SendGeneral { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:320";
|
||||||
|
if let Err(e) = general_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP general packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok((len, _source_address)) = general_socket.recv_from(&mut general_buf) => {
|
Ok((len, _source_address)) = general_socket.recv_from(&mut general_buf) => {
|
||||||
actions.extend(running_port.handle_general_receive(&general_buf[..len]));
|
for action in running_port.handle_general_receive(&general_buf[..len]) {
|
||||||
|
match action {
|
||||||
|
PortAction::SendEvent { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:319";
|
||||||
|
if let Err(e) = event_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP event packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PortAction::SendGeneral { data, .. } => {
|
||||||
|
let dest = "224.0.1.129:320";
|
||||||
|
if let Err(e) = general_socket.send_to(data, dest).await {
|
||||||
|
log::error!("Error sending PTP general packet: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update shared state periodically (after all mutable operations are done)
|
// Update shared state periodically (no borrowing conflicts now)
|
||||||
if last_state_update.elapsed() > Duration::from_millis(500) {
|
if last_state_update.elapsed() > Duration::from_millis(500) {
|
||||||
let port_ds = running_port.port_ds();
|
let port_ds = running_port.port_ds();
|
||||||
let mut st = state.lock().unwrap();
|
let mut st = state.lock().unwrap();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue