Skip to content

Commit

Permalink
Retire the service modules
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Oct 13, 2024
1 parent 1299c81 commit ad0f0c7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/client/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use tokio::io::{AsyncRead, AsyncWrite};

use crate::service::rtu::{Client, ClientContext};
use crate::rtu::{Client, ClientContext};

use super::*;

Expand Down
2 changes: 1 addition & 1 deletion src/client/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn attach_slave<T>(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),
}
Expand Down
20 changes: 20 additions & 0 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, C>(framed: tokio_util::codec::Framed<T, C>) -> 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
Expand Down
15 changes: 1 addition & 14 deletions src/frame/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,7 +67,5 @@ pub use self::frame::{
/// 2. [`ExceptionCode`]: An error occurred on the _Modbus_ server.
pub type Result<T> = std::result::Result<std::result::Result<T, ExceptionCode>, Error>;

mod service;

#[cfg(feature = "server")]
pub mod server;
3 changes: 1 addition & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 13 additions & 2 deletions src/service/rtu.rs → src/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
28 changes: 0 additions & 28 deletions src/service/mod.rs

This file was deleted.

4 changes: 1 addition & 3 deletions src/service/tcp.rs → src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,8 +17,6 @@ use crate::{
ExceptionResponse, ProtocolError, Request, Response, Result,
};

use super::disconnect;

const INITIAL_TRANSACTION_ID: TransactionId = 0;

#[derive(Debug)]
Expand Down

0 comments on commit ad0f0c7

Please sign in to comment.