From ad0f0c74da02b99e7c43dbc4e85fe4d46397172d Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Sun, 13 Oct 2024 22:49:33 +0200 Subject: [PATCH] Retire the `service` modules --- src/client/rtu.rs | 2 +- src/client/tcp.rs | 2 +- src/codec/mod.rs | 20 ++++++++++++++++++++ src/frame/rtu.rs | 15 +-------------- src/lib.rs | 8 ++++++-- src/prelude.rs | 3 +-- src/{service => }/rtu.rs | 15 +++++++++++++-- src/service/mod.rs | 28 ---------------------------- src/{service => }/tcp.rs | 4 +--- 9 files changed, 44 insertions(+), 53 deletions(-) rename src/{service => }/rtu.rs (94%) delete mode 100644 src/service/mod.rs rename src/{service => }/tcp.rs (99%) diff --git a/src/client/rtu.rs b/src/client/rtu.rs index 5d04396f..606ffad3 100644 --- a/src/client/rtu.rs +++ b/src/client/rtu.rs @@ -5,7 +5,7 @@ use tokio::io::{AsyncRead, AsyncWrite}; -use crate::service::rtu::{Client, ClientContext}; +use crate::rtu::{Client, ClientContext}; use super::*; diff --git a/src/client/tcp.rs b/src/client/tcp.rs index acd0df56..da72ce4c 100644 --- a/src/client/tcp.rs +++ b/src/client/tcp.rs @@ -43,7 +43,7 @@ pub fn attach_slave(transport: T, slave: Slave) -> Context where T: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug + 'static, { - let client = crate::service::tcp::Client::new(transport, slave); + let client = crate::tcp::Client::new(transport, slave); Context { client: Box::new(client), } diff --git a/src/codec/mod.rs b/src/codec/mod.rs index 41f3a721..0745fe2d 100644 --- a/src/codec/mod.rs +++ b/src/codec/mod.rs @@ -19,6 +19,26 @@ pub(crate) mod rtu; #[cfg(feature = "tcp")] pub(crate) mod tcp; +#[cfg(any(feature = "rtu", feature = "tcp"))] +pub(crate) async fn disconnect(framed: tokio_util::codec::Framed) -> std::io::Result<()> +where + T: tokio::io::AsyncWrite + Unpin, +{ + use tokio::io::AsyncWriteExt as _; + + framed + .into_inner() + .shutdown() + .await + .or_else(|err| match err.kind() { + std::io::ErrorKind::NotConnected | std::io::ErrorKind::BrokenPipe => { + // Already disconnected. + Ok(()) + } + _ => Err(err), + }) +} + #[allow(clippy::cast_possible_truncation)] fn u16_len(len: usize) -> u16 { // This type conversion should always be safe, because either diff --git a/src/frame/rtu.rs b/src/frame/rtu.rs index 18233716..d7da0189 100644 --- a/src/frame/rtu.rs +++ b/src/frame/rtu.rs @@ -3,20 +3,7 @@ use super::*; -use crate::{ProtocolError, Result, Slave}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct RequestContext { - function_code: FunctionCode, - header: Header, -} - -impl RequestContext { - #[must_use] - pub const fn function_code(&self) -> FunctionCode { - self.function_code - } -} +use crate::{rtu::RequestContext, ProtocolError, Result, Slave}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub(crate) struct Header { diff --git a/src/lib.rs b/src/lib.rs index d2417f2f..6451263c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,12 @@ pub mod client; pub mod slave; pub use self::slave::{Slave, SlaveId}; +#[cfg(feature = "rtu")] +pub mod rtu; + +#[cfg(feature = "tcp")] +pub mod tcp; + mod codec; mod error; @@ -61,7 +67,5 @@ pub use self::frame::{ /// 2. [`ExceptionCode`]: An error occurred on the _Modbus_ server. pub type Result = std::result::Result, Error>; -mod service; - #[cfg(feature = "server")] pub mod server; diff --git a/src/prelude.rs b/src/prelude.rs index 268196d2..35dcd3f3 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -12,8 +12,7 @@ pub use crate::client; #[cfg(feature = "rtu")] pub mod rtu { pub use crate::client::rtu::*; - pub use crate::frame::rtu::RequestContext; - pub use crate::service::rtu::{Client, ClientContext}; + pub use crate::rtu::{Client, ClientContext, RequestContext}; } #[allow(missing_docs)] diff --git a/src/service/rtu.rs b/src/rtu.rs similarity index 94% rename from src/service/rtu.rs rename to src/rtu.rs index 292c8a21..666019e1 100644 --- a/src/service/rtu.rs +++ b/src/rtu.rs @@ -8,13 +8,24 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tokio_util::codec::Framed; use crate::{ - codec, + codec::{self, disconnect}, frame::{rtu::*, *}, slave::*, Result, }; -use super::disconnect; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct RequestContext { + pub(crate) function_code: FunctionCode, + pub(crate) header: Header, +} + +impl RequestContext { + #[must_use] + pub const fn function_code(&self) -> FunctionCode { + self.function_code + } +} /// _Modbus_ RTU client. #[derive(Debug)] diff --git a/src/service/mod.rs b/src/service/mod.rs deleted file mode 100644 index 212f224b..00000000 --- a/src/service/mod.rs +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH -// SPDX-License-Identifier: MIT OR Apache-2.0 - -#[cfg(feature = "rtu")] -pub(crate) mod rtu; - -#[cfg(feature = "tcp")] -pub(crate) mod tcp; - -#[cfg(any(feature = "rtu", feature = "tcp"))] -async fn disconnect(framed: tokio_util::codec::Framed) -> std::io::Result<()> -where - T: tokio::io::AsyncWrite + Unpin, -{ - use tokio::io::AsyncWriteExt as _; - - framed - .into_inner() - .shutdown() - .await - .or_else(|err| match err.kind() { - std::io::ErrorKind::NotConnected | std::io::ErrorKind::BrokenPipe => { - // Already disconnected. - Ok(()) - } - _ => Err(err), - }) -} diff --git a/src/service/tcp.rs b/src/tcp.rs similarity index 99% rename from src/service/tcp.rs rename to src/tcp.rs index 38bb6a60..f6914da1 100644 --- a/src/service/tcp.rs +++ b/src/tcp.rs @@ -8,7 +8,7 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tokio_util::codec::Framed; use crate::{ - codec, + codec::{self, disconnect}, frame::{ tcp::{Header, RequestAdu, ResponseAdu, TransactionId, UnitId}, verify_response_header, RequestPdu, ResponsePdu, @@ -17,8 +17,6 @@ use crate::{ ExceptionResponse, ProtocolError, Request, Response, Result, }; -use super::disconnect; - const INITIAL_TRANSACTION_ID: TransactionId = 0; #[derive(Debug)]