Struct TCPMsgClient

Source
pub struct TCPMsgClient<T> { /* private fields */ }

Implementations§

Source§

impl<T> TCPMsgClient<T>
where T: DeserializeOwned + Serialize + Send + 'static + Clone,

Source

pub fn new(addr: &str, client_name: &str) -> TCPMsgClient<T>

addr is socket address. like: 127.0.0.1:6666. name is the client’s name, to identity which client it is.

Source

pub fn start_client<F>( &self, process_function: F, ) -> Box<dyn Future<Item = (), Error = ()> + Send + 'static>
where F: FnMut(T) -> Vec<T> + Send + Sync + 'static,

process_function receive a message from server and send a series of messages to server

Examples found in repository?
examples/simple_connection.rs (lines 40-47)
38fn client() {
39    let client = create_tcp_client("127.0.0.1:6666", "client");
40    let client_task = client.start_client(|msg: u32| {
41        println!("{}", msg);
42        if msg < 20 {
43            vec![msg + 1]
44        } else {
45            std::process::exit(0);
46        }
47    });
48    tokio::run(client_task);
49}
More examples
Hide additional examples
examples/transmit_multiple_structs.rs (lines 72-85)
68fn client() {
69    let client = create_tcp_client("127.0.0.1:6666", "client");
70    // x is used to test whether the closure can change the outer mutable variable
71    let mut x: u32 = 0;
72    let client_task = client.start_client(move |msg: Message| {
73        println!("{:?}", msg);
74        match msg {
75            Message::VecOfF32msg(mut vec_of_32) => {
76                x += 1;
77                vec_of_32.vec.push(1);
78                vec![Message::VecOfF32msg(vec_of_32)]
79            }
80            Message::Endmsg(_) => {
81                println!("Outer count is {:?}", x);
82                std::process::exit(0)
83            }
84        }
85    });
86    tokio::run(client_task);
87}
examples/master_and_slave.rs (lines 76-88)
68fn manager() {
69    use futures::sync::mpsc;
70    use std::sync::{Arc, Mutex};
71    let future_task = future::ok(1).and_then(|_| {
72        let srv = create_tcp_server("127.0.0.1:6666", "management_server");
73        let clt = create_tcp_client("127.0.0.1:7777", "management_client");
74        let connections = srv.connections.clone();
75
76        let clt_task = clt.start_client(move |msg: Message| {
77            println!("manager receive {:?} from master", msg);
78            match msg {
79                Message::Stop() => {
80                    for (_, tx) in connections.lock().unwrap().iter_mut() {
81                        (*tx).try_send(Some(0)).unwrap();
82                    }
83                    std::process::exit(0);
84                }
85                _ =>{}
86            }
87            vec![msg]
88        });
89        tokio::spawn(clt_task);
90
91        let srv_task = srv.start_server(1, |client_name, msg: u32| {
92            println!("manager receive {} from {}", msg, client_name);
93            vec![(client_name, msg + 1)]
94        });
95        tokio::spawn(srv_task);
96        Ok(())
97    }).map_err(|e: std::io::Error| println!("{:?}", e));
98    tokio::run(future_task);
99}
100
101fn driver() {
102    let clt = create_tcp_client("127.0.0.1:6666", "driver");
103    let client_task = clt.start_client(|msg: u32| {
104        println!("{}", msg);
105        if msg != 0 {
106            vec![msg + 1]
107        } else {
108            println!("oh god");
109            std::process::exit(0);
110        }
111    });
112    tokio::run(client_task);
113}

Trait Implementations§

Source§

impl<T: Debug> Debug for TCPMsgClient<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TCPMsgClient<T>

§

impl<T> RefUnwindSafe for TCPMsgClient<T>
where T: RefUnwindSafe,

§

impl<T> Send for TCPMsgClient<T>
where T: Send,

§

impl<T> Sync for TCPMsgClient<T>
where T: Sync,

§

impl<T> Unpin for TCPMsgClient<T>
where T: Unpin,

§

impl<T> UnwindSafe for TCPMsgClient<T>
where T: UnwindSafe,

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.