Skip to main content

tunneler_core/client/
connections.rs

1use crate::{
2    message::Message,
3    streams::{error::RecvError, mpsc},
4};
5
6use super::{Receiver, Sender};
7
8use async_trait::async_trait;
9
10pub mod rx;
11pub mod tx;
12
13/// This represents a single User-Connection and is used to send and receive
14/// Data for that one specific User
15pub struct UserCon {
16    receiver: user_con::OwnedReceiver,
17    sender: user_con::OwnedSender,
18}
19
20impl UserCon {
21    /// Creates a new User-Connection with the given Sender and Receiver Parts
22    pub(crate) fn new(recv: user_con::OwnedReceiver, send: user_con::OwnedSender) -> Self {
23        Self {
24            receiver: recv,
25            sender: send,
26        }
27    }
28
29    /// Splits the Connection into its owned (Reader, Writer)-Halfes, which
30    /// allows you to use them more independantly of one another and enables
31    /// you to send them to different Threads without having to use any extra
32    /// sync between the Halfes
33    pub fn into_split(self) -> (user_con::OwnedReceiver, user_con::OwnedSender) {
34        (self.receiver, self.sender)
35    }
36}
37
38#[async_trait]
39impl Receiver for UserCon {
40    type ReceivingError = RecvError;
41
42    async fn recv_msg(&mut self) -> Result<Message, Self::ReceivingError> {
43        self.receiver.recv_msg().await
44    }
45}
46#[async_trait]
47impl Sender for UserCon {
48    type SendingError = tokio::sync::mpsc::error::SendError<Message>;
49
50    async fn send_msg(&self, data: Vec<u8>, length: u64) -> Result<(), Self::SendingError> {
51        self.sender.send_msg(data, length).await
52    }
53}
54
55pub mod user_con;