Skip to content

Commit

Permalink
Add more logs to server process() loop
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Oct 16, 2024
1 parent 4823606 commit 97fb192
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 17 additions & 7 deletions src/server/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
codec::rtu::ServerCodec,
frame::{
rtu::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
ExceptionResponse, OptionalResponsePdu, RequestPdu,
},
};

Expand Down Expand Up @@ -78,15 +78,22 @@ where
S::Request: From<RequestAdu<'static>> + 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 {
Expand All @@ -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;
};

Expand All @@ -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(())
}
24 changes: 17 additions & 7 deletions src/server/rtu_over_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
codec::rtu::ServerCodec,
frame::{
rtu::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
ExceptionResponse, OptionalResponsePdu, RequestPdu,
},
};

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
};

Expand All @@ -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(())
Expand Down
24 changes: 17 additions & 7 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
codec::tcp::ServerCodec,
frame::{
tcp::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
ExceptionResponse, OptionalResponsePdu, RequestPdu,
},
};

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
};

Expand All @@ -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(())
Expand Down

0 comments on commit 97fb192

Please sign in to comment.