1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]

pub mod config;
pub mod network;
pub mod mutable_cipher;
pub mod client_base;

pub extern crate tokio;
pub extern crate tcp_handler;

use async_trait::async_trait;
use tcp_handler::common::AesCipher;
use tokio::net::{TcpStream, ToSocketAddrs};
use crate::network::{NetworkError, start_client};

/// The client factory to create clients.
/// # Example
/// ```rust,no_run
/// use tcp_client::client_base::ClientBase;
/// use tcp_client::client_factory;
/// use tcp_client::ClientFactory;
/// use tcp_client::network::NetworkError;
///
/// client_factory!(MyClientFactory, MyClient, "MyTcpApplication");
///
/// impl MyClient {
///     // define your method here.
///     // example:
///     async fn my_method(&mut self) -> Result<(), NetworkError> {
///         self.check_func("my_method").await?;
///         // ...
///         Ok(())
///     }
/// }
///
/// #[tokio::main]
/// async fn main() {
///     let mut client = MyClientFactory.connect("127.0.0.1:1234").await.unwrap();
///     // use client.
///     // example:
///     client.my_method().await.unwrap();
/// }
/// ```
#[async_trait]
pub trait ClientFactory<C: From<(TcpStream, AesCipher)>> {
    /// Get the identifier of your application.
    /// # Note
    /// This should be a const.
    fn get_identifier(&self) -> &'static str;

    /// Get the version of your application.
    /// # Note
    /// This should be a const.
    /// # Example
    /// ```rust,ignore
    /// env!("CARGO_PKG_VERSION")
    /// ```
    fn get_version(&self) -> &'static str;

    /// Build a new client.
    async fn connect<A: ToSocketAddrs + Send>(&self, addr: A) -> Result<C, NetworkError> {
        start_client(self, addr).await.map(|c| c.into())
    }
}

/// Conveniently define a client factory.
/// # Example
/// ```rust,ignore
/// use tcp_client::client_factory;
///
/// client_factory!(MyClientFactory, MyClient, "MyTcpApplication");
/// ```
#[macro_export]
macro_rules! client_factory {
    ($factory_vis: vis $factory: ident, $client_vis: vis $client: ident, $identifier: literal) => {
        $factory_vis struct $factory;
        #[derive(Debug)]
        $client_vis struct $client {
            receiver: $crate::tokio::io::BufReader<$crate::tokio::net::tcp::OwnedReadHalf>,
            sender: $crate::tokio::io::BufWriter<$crate::tokio::net::tcp::OwnedWriteHalf>,
            cipher: $crate::mutable_cipher::MutableCipher,
        }

        impl $crate::ClientFactory<$client> for $factory {
            fn get_identifier(&self) -> &'static str {
                $identifier
            }

            fn get_version(&self) -> &'static str {
                env!("CARGO_PKG_VERSION")
            }
        }
        impl From<($crate::tokio::net::TcpStream, $crate::tcp_handler::common::AesCipher)> for $client {
            fn from(value: ($crate::tokio::net::TcpStream, $crate::tcp_handler::common::AesCipher)) -> Self {
                let (receiver, sender)= value.0.into_split();
                Self {
                    receiver: $crate::tokio::io::BufReader::new(receiver),
                    sender: $crate::tokio::io::BufWriter::new(sender),
                    cipher: $crate::mutable_cipher::MutableCipher::new(value.1),
                }
            }
        }
        impl $crate::client_base::ClientBase<$crate::tokio::io::BufReader<$crate::tokio::net::tcp::OwnedReadHalf>, $crate::tokio::io::BufWriter<$crate::tokio::net::tcp::OwnedWriteHalf>> for $client {
            fn get_receiver<'a>(&'a mut self) -> (&'a mut $crate::tokio::io::BufReader<$crate::tokio::net::tcp::OwnedReadHalf>, &$crate::mutable_cipher::MutableCipher) {
                (&mut self.receiver, &self.cipher)
            }

            fn get_sender<'a>(&'a mut self) -> (&'a mut $crate::tokio::io::BufWriter<$crate::tokio::net::tcp::OwnedWriteHalf>, &$crate::mutable_cipher::MutableCipher) {
                (&mut self.sender, &self.cipher)
            }
        }
    };
}

/// A shortcut for [`ClientFactory`].
pub async fn quickly_connect<A: ToSocketAddrs + Send, C: From<(TcpStream, AesCipher)>>(identifier: &'static str, version:&'static str, addr: A) -> Result<C, NetworkError> {
    struct TempClient(&'static str, &'static str);
    impl ClientFactory<(TcpStream, AesCipher)> for TempClient {
        fn get_identifier(&self) -> &'static str {
            self.0
        }
        fn get_version(&self) -> &'static str {
            self.1
        }
    }
    TempClient(identifier, version).connect(addr).await.map(|c| c.into())
}