-
Notifications
You must be signed in to change notification settings - Fork 265
impl AsyncRead, AsyncWrite for native MixnetClient #4634
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
Closed
Closed
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
34e9822
serde for ReconstructedMessage
durch 575056a
AsyncRead for MixnetClient
durch eed87ff
Switch to PollSender
durch edd9fef
Use Sink always
durch f54cee7
InputMessageCodec, Serde for MixPacket
durch 7820b81
ReconstructedMessageCodec
durch 4adcd32
Use tokio AsyncRead
durch cefb217
AsyncWrite
durch 36e8468
WASM changes
durch bc28cc7
Update IPR sig
durch 46ebb70
Cleanup prints
durch abfd121
Log decoding error
durch 2fe282d
fmt
durch 1b33d8e
Address part of PR comments
durch 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,16 +1,23 @@ | ||
| // Copyright 2020-2023 - Nym Technologies SA <[email protected]> | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use nym_sphinx::addressing::clients::Recipient; | ||
| use nym_sphinx::anonymous_replies::requests::AnonymousSenderTag; | ||
| use nym_sphinx::forwarding::packet::MixPacket; | ||
| use nym_sphinx::params::PacketType; | ||
| use nym_task::connections::TransmissionLane; | ||
| use serde::{Deserialize, Serialize}; | ||
| use tokio_util::{ | ||
| bytes::Buf, | ||
| bytes::BytesMut, | ||
| codec::{Decoder, Encoder}, | ||
| }; | ||
|
|
||
| use crate::error::ClientCoreError; | ||
|
|
||
| pub type InputMessageSender = tokio::sync::mpsc::Sender<InputMessage>; | ||
| pub type InputMessageSender = tokio_util::sync::PollSender<InputMessage>; | ||
| pub type InputMessageReceiver = tokio::sync::mpsc::Receiver<InputMessage>; | ||
|
|
||
| #[derive(Debug)] | ||
| #[derive(Serialize, Deserialize, Debug)] | ||
| pub enum InputMessage { | ||
| /// Fire an already prepared mix packets into the network. | ||
| /// No guarantees are made about it. For example no retransmssion | ||
|
|
@@ -64,6 +71,10 @@ pub enum InputMessage { | |
| } | ||
|
|
||
| impl InputMessage { | ||
| pub fn simple(data: &[u8], recipient: Recipient) -> Self { | ||
| InputMessage::new_regular(recipient, data.to_vec(), TransmissionLane::General, None) | ||
| } | ||
|
|
||
| pub fn new_premade( | ||
| msgs: Vec<MixPacket>, | ||
| lane: TransmissionLane, | ||
|
|
@@ -197,4 +208,50 @@ impl InputMessage { | |
| InputMessage::MessageWrapper { message, .. } => message.lane(), | ||
| } | ||
| } | ||
|
|
||
| pub fn serialized_size(&self) -> u64 { | ||
| bincode::serialized_size(self).expect("failed to get serialized InputMessage size") + 4 | ||
| } | ||
| } | ||
|
|
||
| // TODO: Tests | ||
| pub struct InputMessageCodec; | ||
|
|
||
| impl Encoder<InputMessage> for InputMessageCodec { | ||
| type Error = ClientCoreError; | ||
|
|
||
| fn encode(&mut self, item: InputMessage, buf: &mut BytesMut) -> Result<(), Self::Error> { | ||
| let encoded = bincode::serialize(&item).expect("failed to serialize InputMessage"); | ||
| let encoded_len = encoded.len() as u32; | ||
| let mut encoded_with_len = encoded_len.to_le_bytes().to_vec(); | ||
| encoded_with_len.extend(encoded); | ||
| buf.reserve(encoded_with_len.len()); | ||
| buf.extend_from_slice(&encoded_with_len); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Decoder for InputMessageCodec { | ||
| type Item = InputMessage; | ||
| type Error = ClientCoreError; | ||
|
|
||
| fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||
| if buf.len() < 4 { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let len = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize; | ||
| if buf.len() < len + 4 { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let decoded = match bincode::deserialize(&buf[4..len + 4]) { | ||
| Ok(decoded) => decoded, | ||
| Err(_) => return Ok(None), | ||
| }; | ||
|
|
||
| buf.advance(len + 4); | ||
|
|
||
| Ok(Some(decoded)) | ||
| } | ||
| } | ||
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
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
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
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
Oops, something went wrong.
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.