-
Notifications
You must be signed in to change notification settings - Fork 132
tcp client: Use non-atomic type for internal transaction id #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
38ac12b
tcp: Use non-atomic type for transaction id
uklotzde 9c99e01
Remove unused trait impls
uklotzde 50bf05e
Extract TransactionIdGenerator
uklotzde d82f373
Reorder fields to match ordering in header
uklotzde c41514f
Merge branch 'main' into tcp-transaction-id
uklotzde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH <[email protected]> | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use std::{ | ||
fmt, io, | ||
sync::atomic::{AtomicU16, Ordering}, | ||
}; | ||
use std::{fmt, io}; | ||
|
||
use futures_util::{SinkExt as _, StreamExt as _}; | ||
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _}; | ||
|
@@ -20,12 +17,18 @@ use crate::{ | |
|
||
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 | ||
} | ||
|
||
/// Modbus TCP client | ||
#[derive(Debug)] | ||
pub(crate) struct Client<T> { | ||
framed: Option<Framed<T, codec::tcp::ClientCodec>>, | ||
unit_id: UnitId, | ||
transaction_id: AtomicU16, | ||
transaction_id: TransactionId, | ||
} | ||
|
||
impl<T> Client<T> | ||
|
@@ -35,30 +38,23 @@ where | |
pub(crate) fn new(transport: T, slave: Slave) -> Self { | ||
let framed = Framed::new(transport, codec::tcp::ClientCodec::default()); | ||
let unit_id: UnitId = slave.into(); | ||
let transaction_id = AtomicU16::new(INITIAL_TRANSACTION_ID); | ||
let transaction_id = INITIAL_TRANSACTION_ID; | ||
Self { | ||
framed: Some(framed), | ||
unit_id, | ||
transaction_id, | ||
} | ||
} | ||
|
||
fn next_transaction_id(&self) -> TransactionId { | ||
let transaction_id = self.transaction_id.load(Ordering::Relaxed); | ||
self.transaction_id | ||
.store(transaction_id.wrapping_add(1), Ordering::Relaxed); | ||
transaction_id | ||
} | ||
|
||
fn next_request_hdr(&self, unit_id: UnitId) -> Header { | ||
let transaction_id = self.next_transaction_id(); | ||
fn next_request_hdr(&mut self, unit_id: UnitId) -> Header { | ||
let transaction_id = next_transaction_id(&mut self.transaction_id); | ||
Header { | ||
transaction_id, | ||
unit_id, | ||
} | ||
} | ||
|
||
fn next_request_adu<'a, R>(&self, req: R) -> RequestAdu<'a> | ||
fn next_request_adu<'a, R>(&mut self, req: R) -> RequestAdu<'a> | ||
where | ||
R: Into<RequestPdu<'a>>, | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.