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
use std::time::{Duration};
use async_std::{sync::Arc, future, task};
use async_trait::{async_trait};
use cyfs_base::*;
use crate::protocol;
use super::super::*;
use tunnel::{Tunnel, TunnelState};
use log::*;


#[async_trait]
pub trait BuildTunnelAction: Send + Sync + std::fmt::Display {
    fn local(&self) -> &Endpoint;
    fn remote(&self) -> &Endpoint;
}

pub type DynBuildTunnelAction = Box<dyn BuildTunnelAction>;

struct SynUdpTunnelImpl {
    tunnel: udp::Tunnel, 
}

impl std::fmt::Display for SynUdpTunnel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SynUdpTunnel{{tunnel:{}}}", self.0.tunnel)
    }
}

#[derive(Clone)]
pub struct SynUdpTunnel(Arc<SynUdpTunnelImpl>);
impl SynUdpTunnel {
    pub fn new(
        tunnel: udp::Tunnel, 
        first_box: Arc<protocol::PackageBox>, 
        interval: Duration) -> Self {
        let action = Self(Arc::new(SynUdpTunnelImpl {
            tunnel: tunnel.clone(), 
        }));
        // 有可能收到called之后触发 syn tunnel, 
        if tunnel.try_update_key(&first_box).is_err() {
            let action = action.clone();
            task::spawn(async move {
                loop {
                    match tunnel.state() {
                        TunnelState::Connecting => {
                            let result = tunnel.send_box(&first_box);
                            debug!("{} send first box result: {:?}", action, result);
                        },
                        _ => break,
                    } 
                    future::timeout(interval, future::pending::<()>()).await.err();
                }
            });
        }
        action
    }
}

#[async_trait]
impl BuildTunnelAction for SynUdpTunnel {
    fn local(&self) -> &Endpoint {
        self.0.tunnel.local()
    }

    fn remote(&self) -> &Endpoint {
        self.0.tunnel.remote()
    }
}



#[derive(Clone)]
pub struct ConnectTcpTunnel {
    tunnel: tcp::Tunnel
}

impl std::fmt::Display for ConnectTcpTunnel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ConnectTunnel{{tunnel:{}}}", self.tunnel)
    }
}


impl ConnectTcpTunnel {
    pub fn new(tunnel: tcp::Tunnel) -> Self {
        let _ = tunnel.connect();
        Self {
            tunnel
        }
    }
}

impl BuildTunnelAction for ConnectTcpTunnel {
    fn local(&self) -> &Endpoint {
        self.tunnel.local()
    }

    fn remote(&self) -> &Endpoint {
        self.tunnel.remote()
    }
}