From 97fb192e4dd7809ef7769b416a9330405fc168e9 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Wed, 16 Oct 2024 18:20:28 +0200 Subject: [PATCH] Add more logs to server process() loop --- CHANGELOG.md | 6 ++++++ Cargo.toml | 2 +- src/server/rtu.rs | 24 +++++++++++++++++------- src/server/rtu_over_tcp.rs | 24 +++++++++++++++++------- src/server/tcp.rs | 24 +++++++++++++++++------- 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3136dd38..806734ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ # Changelog +## v0.16.0 (Unreleased) + +### Breaking Changes + +- Increased MSRV from 1.65 to 1.76 + ## v0.15.0 (2024-10-10) - Implement `Report Server ID` (function code 17). diff --git a/Cargo.toml b/Cargo.toml index aa5fdf83..15b6de7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ keywords = ["fieldbus", "modbus", "hardware", "automation"] homepage = "https://github.com/slowtec/tokio-modbus" repository = "https://github.com/slowtec/tokio-modbus" edition = "2021" -rust-version = "1.65" +rust-version = "1.76" include = ["/src", "/CHANGELOG.md", "/README.md", "/LICENSES"] [package.metadata.docs.rs] diff --git a/src/server/rtu.rs b/src/server/rtu.rs index 37195cac..c10bf1f4 100644 --- a/src/server/rtu.rs +++ b/src/server/rtu.rs @@ -13,7 +13,7 @@ use crate::{ codec::rtu::ServerCodec, frame::{ rtu::{RequestAdu, ResponseAdu}, - ExceptionResponse, OptionalResponsePdu, + ExceptionResponse, OptionalResponsePdu, RequestPdu, }, }; @@ -78,15 +78,22 @@ where S::Request: From> + Send, { loop { - let Some(request) = framed.next().await.transpose()? else { + let Some(request_adu) = framed.next().await.transpose().inspect_err(|err| { + log::debug!("Failed to receive and decode request ADU: {err}"); + })? + else { log::debug!("Stream has finished"); break; }; - let fc = request.pdu.0.function_code(); - let hdr = request.hdr; + let RequestAdu { + hdr, + pdu: RequestPdu(request), + } = &request_adu; + let hdr = *hdr; + let fc = request.function_code(); let OptionalResponsePdu(Some(response_pdu)) = service - .call(request.into()) + .call(request_adu.into()) .await .map(Into::into) .map_err(|e| ExceptionResponse { @@ -95,7 +102,7 @@ where }) .into() else { - log::debug!("Sending no response for request {hdr:?}"); + log::trace!("No response for request {hdr:?} (function = {fc})"); continue; }; @@ -104,7 +111,10 @@ where hdr, pdu: response_pdu, }) - .await?; + .await + .inspect_err(|err| { + log::debug!("Failed to send response for request {hdr:?} (function = {fc}): {err}"); + })?; } Ok(()) } diff --git a/src/server/rtu_over_tcp.rs b/src/server/rtu_over_tcp.rs index d3c2c194..dc5a1193 100644 --- a/src/server/rtu_over_tcp.rs +++ b/src/server/rtu_over_tcp.rs @@ -18,7 +18,7 @@ use crate::{ codec::rtu::ServerCodec, frame::{ rtu::{RequestAdu, ResponseAdu}, - ExceptionResponse, OptionalResponsePdu, + ExceptionResponse, OptionalResponsePdu, RequestPdu, }, }; @@ -140,15 +140,22 @@ where T: AsyncRead + AsyncWrite + Unpin, { loop { - let Some(request) = framed.next().await.transpose()? else { + let Some(request_adu) = framed.next().await.transpose().inspect_err(|err| { + log::debug!("Failed to receive and decode request ADU: {err}"); + })? + else { log::debug!("TCP socket has been closed"); break; }; - let fc = request.pdu.0.function_code(); - let hdr = request.hdr; + let RequestAdu { + hdr, + pdu: RequestPdu(request), + } = &request_adu; + let hdr = *hdr; + let fc = request.function_code(); let OptionalResponsePdu(Some(response_pdu)) = service - .call(request.into()) + .call(request_adu.into()) .await .map(Into::into) .map_err(|e| ExceptionResponse { @@ -157,7 +164,7 @@ where }) .into() else { - log::trace!("Sending no response for request {hdr:?}"); + log::trace!("No response for request {hdr:?} (function = {fc})"); continue; }; @@ -166,7 +173,10 @@ where hdr, pdu: response_pdu, }) - .await?; + .await + .inspect_err(|err| { + log::debug!("Failed to send response for request {hdr:?} (function = {fc}): {err}"); + })?; } Ok(()) diff --git a/src/server/tcp.rs b/src/server/tcp.rs index 82b1a9e1..e4127479 100644 --- a/src/server/tcp.rs +++ b/src/server/tcp.rs @@ -18,7 +18,7 @@ use crate::{ codec::tcp::ServerCodec, frame::{ tcp::{RequestAdu, ResponseAdu}, - ExceptionResponse, OptionalResponsePdu, + ExceptionResponse, OptionalResponsePdu, RequestPdu, }, }; @@ -139,15 +139,22 @@ where T: AsyncRead + AsyncWrite + Unpin, { loop { - let Some(request) = framed.next().await.transpose()? else { + let Some(request_adu) = framed.next().await.transpose().inspect_err(|err| { + log::debug!("Failed to receive and decode request ADU: {err}"); + })? + else { log::debug!("TCP socket has been closed"); break; }; - let fc = request.pdu.0.function_code(); - let hdr = request.hdr; + let RequestAdu { + hdr, + pdu: RequestPdu(request), + } = &request_adu; + let hdr = *hdr; + let fc = request.function_code(); let OptionalResponsePdu(Some(response_pdu)) = service - .call(request.into()) + .call(request_adu.into()) .await .map(Into::into) .map_err(|e| ExceptionResponse { @@ -156,7 +163,7 @@ where }) .into() else { - log::trace!("Sending no response for request {hdr:?}"); + log::trace!("No response for request {hdr:?} (function = {fc})"); continue; }; @@ -165,7 +172,10 @@ where hdr, pdu: response_pdu, }) - .await?; + .await + .inspect_err(|err| { + log::debug!("Failed to send response for request {hdr:?} (function = {fc}): {err}"); + })?; } Ok(())