Skip to content

Commit

Permalink
Extract TransactionIdGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Sep 19, 2024
1 parent 9c99e01 commit 50bf05e
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/service/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,42 @@ use tokio_util::codec::Framed;

use crate::{
codec,
frame::{tcp::*, *},
frame::{
tcp::{Header, RequestAdu, ResponseAdu, TransactionId, UnitId},
RequestPdu, ResponsePdu,
},
service::verify_response_header,
slave::*,
ProtocolError, Result,
ExceptionResponse, ProtocolError, Request, Response, Result,
};

const INITIAL_TRANSACTION_ID: TransactionId = 0;

fn next_transaction_id(transaction_id: &mut TransactionId) -> TransactionId {
let next_transaction_id = *transaction_id;
*transaction_id = next_transaction_id.wrapping_add(1);
next_transaction_id
#[derive(Debug)]
struct TransactionIdGenerator {
next_transaction_id: TransactionId,
}

impl TransactionIdGenerator {
const fn new() -> Self {
Self {
next_transaction_id: INITIAL_TRANSACTION_ID,
}
}

fn next(&mut self) -> TransactionId {
let next_transaction_id = self.next_transaction_id;
self.next_transaction_id = next_transaction_id.wrapping_add(1);
next_transaction_id
}
}

/// Modbus TCP client
#[derive(Debug)]
pub(crate) struct Client<T> {
framed: Option<Framed<T, codec::tcp::ClientCodec>>,
unit_id: UnitId,
transaction_id: TransactionId,
transaction_id_generator: TransactionIdGenerator,
}

impl<T> Client<T>
Expand All @@ -38,16 +54,16 @@ where
pub(crate) fn new(transport: T, slave: Slave) -> Self {
let framed = Framed::new(transport, codec::tcp::ClientCodec::new());
let unit_id: UnitId = slave.into();
let transaction_id = INITIAL_TRANSACTION_ID;
let transaction_id_generator = TransactionIdGenerator::new();
Self {
framed: Some(framed),
unit_id,
transaction_id,
transaction_id_generator,
}
}

fn next_request_hdr(&mut self, unit_id: UnitId) -> Header {
let transaction_id = next_transaction_id(&mut self.transaction_id);
let transaction_id = self.transaction_id_generator.next();
Header {
transaction_id,
unit_id,
Expand Down

0 comments on commit 50bf05e

Please sign in to comment.