diff --git a/src/client/rtu.rs b/src/client/rtu.rs index 5d04396..606ffad 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 acd0df5..da72ce4 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 41f3a72..0745fe2 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 1823371..d7da018 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 d2417f2..6451263 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 268196d..35dcd3f 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 292c8a2..666019e 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 212f224..0000000 --- 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 38bb6a6..f6914da 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)]