RelayClient

Struct RelayClient 

Source
pub struct RelayClient {
    pub connections: Vec<Arc<RwLock<Connection>>>,
    pub connection_errors: Vec<ConnectionError>,
    pub info: ConnectionInfo,
}

Fields§

§connections: Vec<Arc<RwLock<Connection>>>§connection_errors: Vec<ConnectionError>§info: ConnectionInfo

Implementations§

Source§

impl RelayClient

Source

pub fn new( info: ConnectionInfo, relays: Vec<String>, ) -> Result<Self, RelayClientError>

Examples found in repository?
examples/client.rs (lines 22-25)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}
Source

pub fn step(&mut self)

Examples found in repository?
examples/client.rs (line 30)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}
Source

pub fn where_is_adress(&self, adress: &Adress) -> Vec<usize>

Examples found in repository?
examples/client.rs (line 43)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}
Source

pub fn search(&self, search: Search) -> Response<SearchResponse, Vec<Adress>>

Examples found in repository?
examples/client.rs (lines 31-36)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}
Source

pub fn has_new(&self) -> Option<(usize, RequestStage)>

Examples found in repository?
examples/client.rs (line 86)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}
Source

pub fn get(&self, index: usize) -> Option<&dyn TConnection>

Examples found in repository?
examples/client.rs (line 45)
12fn main() {
13    println!("Starting client");
14    let info = ConnectionInfo {
15        client: "Test".into(),
16        name: "konkito".into(),
17        public: vec![random(), random(), random(), random()],
18        other: vec![],
19        privacy: false,
20    };
21    println!("Info: {:?}", info);
22    let mut client = RelayClient::new(
23        info,
24        vec![String::from("0.0.0.0"), String::from("w.konkito.com")],
25    )
26    .unwrap();
27
28    println!("Create connection");
29
30    client.step();
31    let search = client.search(Search {
32        session: 0,
33        client: SearchType::None,
34        name: SearchType::None,
35        other: SearchType::None,
36    });
37
38    let search = search.get();
39
40    println!("Search: {:?}", search);
41
42    for adress in search {
43        let mut where_is = client.where_is_adress(&adress);
44        let Some(where_is) = where_is.pop() else{continue};
45        let info = client.get(where_is).unwrap().info(&adress);
46        let client_info = info.get();
47        if let Some(info) = client_info {
48            println!("Client: {:?}", info);
49        }
50    }
51
52    let mut connections = Vec::new();
53    let mut thread: Option<JoinHandle<(Adress, Conn)>> = None;
54
55    let mut port = rand::thread_rng().gen_range(2120..50000);
56    let mut connecting_to = Vec::new();
57
58    let search = client.search(Search::default()).get();
59    for adress in search {
60        if adress != client.get(0).unwrap().adress() && !connecting_to.contains(&adress) {
61            connecting_to.push(adress.clone());
62            println!("Cannecting to: {:?}", adress);
63            client.get(0).unwrap().request(&adress, String::new());
64        }
65    }
66
67    loop {
68        client.step();
69        if let Some(worker) = thread.take() {
70            if worker.is_finished() {
71                let res = worker.join().unwrap();
72                let mut buffer = [MaybeUninit::new(0); 1024];
73                res.1.set_nonblocking(false).unwrap();
74                res.1.send(b"Hello There").unwrap();
75                let len = res.1.recv(&mut buffer).unwrap();
76                let buffer: &[u8] = unsafe { std::mem::transmute(&buffer[0..len]) };
77                let message = String::from_utf8(buffer.to_vec()).unwrap();
78                println!("Message: {}", message);
79                connections.push(res);
80                return;
81            } else {
82                thread = Some(worker)
83            }
84        }
85
86        if let Some((_, step)) = client.has_new() {
87            match step {
88                relay_man::client::response::RequestStage::NewRequest(new) => {
89                    connecting_to.push(new.from.clone());
90                    println!("New from: {:?}", new.from);
91                    new.accept(true);
92                }
93                relay_man::client::response::RequestStage::NewRequestResponse(new) => {
94                    println!("Res from: {:?}", new.from);
95                    println!("Add port: {}", port);
96                    new.add_port(port);
97                    port += 1;
98                    new.accept(true, Some(Duration::from_secs(2).as_nanos()));
99                }
100                relay_man::client::response::RequestStage::NewRequestFinal(new) => {
101                    println!("Final from: {:?}", new.from);
102                    println!("Add port: {}", port);
103                    new.add_port(port);
104                    port += 1;
105                }
106                relay_man::client::response::RequestStage::ConnectOn(new) => {
107                    println!("ConnectOn: {:?}", new);
108                    thread = Some(std::thread::spawn(|| {
109                        (
110                            new.adress.clone(),
111                            new.connect(Duration::from_secs(5), Duration::from_millis(100), true)
112                                .unwrap(),
113                        )
114                    }));
115                }
116            }
117        }
118    }
119}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,