Socket

Struct Socket 

Source
pub struct Socket { /* private fields */ }

Implementations§

Source§

impl Socket

Source

pub fn new(id: MessageId, hook: impl Hook) -> Result<Self>

Examples found in repository?
examples/main.rs (line 10)
9fn main() {
10    let socket = Socket::new(MessageId::new(), NonHook).unwrap();
11
12    // start wire 1
13    let wire1 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
14
15    wire1.send(msg!{
16        CHAN: PING
17    }).unwrap();
18
19    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
20    println!("wire 1 ping ret: {:?}", ret);
21
22    // start node
23    let _node = Node::<NsonCodec>::new(
24        socket.clone(),
25        4,
26        vec!["127.0.0.1:8888".parse().unwrap()],
27        KeepAlive::default(),
28        ()
29    ).unwrap();
30
31    // start port
32    let port = Port::<NsonCodec>::new(KeepAlive::default()).unwrap();
33
34    // start wire 2
35    let wire2 = port.connect("127.0.0.1:8888", MessageId::new(), false, msg!{}, None, None).unwrap();
36
37    wire2.send(msg!{
38        CHAN: PING
39    }).unwrap();
40
41    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
42    if let Some(err) = Code::get(&ret) {
43        if err != Code::Ok {
44            println!("wire 2 ping error: {:?}", err);
45            return
46        }
47    }
48
49    println!("wire 2 ping ret: {:?}", ret);
50
51    // wire 1 attach
52    wire1.send(msg!{
53        CHAN: ATTACH,
54        VALUE: "hello"
55    }).unwrap();
56
57    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
58    println!("wire 1 attach ret: {:?}", ret);
59
60    // wire 2 send
61    wire2.send(msg!{
62        ID: MessageId::new(),
63        CHAN: "hello",
64        "hello": "world"
65    }).unwrap();
66
67    // wire 1 recv
68    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
69    if let Some(err) = Code::get(&ret) {
70        if err != Code::Ok {
71            println!("wire 1 recv error: {:?}", err);
72            return
73        }
74    }
75
76    println!("wire 1 recv ret: {:?}", ret);
77}
More examples
Hide additional examples
examples/call.rs (line 12)
11fn main() {
12    let socket = Socket::new(MessageId::new(), NonHook).unwrap();
13
14    // start wire 1
15    let wire1 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
16
17    wire1.send(msg!{
18        CHAN: PING
19    }).unwrap();
20
21    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
22    println!("wire 1 ping ret: {:?}", ret);
23
24    // start wire 2
25    let wire2 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
26
27    wire2.send(msg!{
28        CHAN: PING
29    }).unwrap();
30
31    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
32    println!("wire 2 ping ret: {:?}", ret);
33
34
35    // wire 1 attach
36    wire1.send(msg!{
37        CHAN: ATTACH,
38        VALUE: "hello"
39    }).unwrap();
40
41    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
42    if let Some(err) = Code::get(&ret) {
43        if err != Code::Ok {
44            println!("wire 1 attach error: {:?}", err);
45            return
46        }
47    }
48
49    println!("wire 1 attach ret: {:?}", ret);
50
51    thread::spawn(move || {
52        loop {
53            if let Ok(ret) = wire1.wait(Some(Duration::from_secs(1))) {
54                if let Some(err) = Code::get(&ret) {
55                    if err != Code::Ok {
56                        println!("wire 1 recv error: {:?}", err);
57                        return
58                    }
59                }
60
61                println!("wire 1 recv ret: {:?}", ret);
62
63                if let Some(_) = ret.get("aaa") {
64                    wire1.send(msg!{
65                        CHAN: "hello",
66                        CODE: 0i32,
67                        TO: ret.get_message_id(FROM).unwrap(),
68                        "lalala": "wawawa"
69                    }).unwrap();
70                }
71            }
72        }
73    });
74
75
76    // wire 2 send
77    wire2.send(msg!{
78        ID: MessageId::new(),
79        CHAN: "hello",
80        "aaa": true,
81        "hello": "world"
82    }).unwrap();
83
84    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
85    if let Some(err) = Code::get(&ret) {
86        if err != Code::Ok {
87            println!("wire 2 send error: {:?}", err);
88            return
89        }
90    }
91
92    println!("wire 2 send ret: {:?}", ret);
93}
Source

pub fn id(&self) -> &MessageId

Source

pub fn stop(&self)

Source

pub fn running(&self) -> bool

Source

pub fn connect( &self, slot_id: MessageId, root: bool, attr: Message, capacity: Option<usize>, timeout: Option<Duration>, ) -> Result<Wire<Message>>

Examples found in repository?
examples/main.rs (line 13)
9fn main() {
10    let socket = Socket::new(MessageId::new(), NonHook).unwrap();
11
12    // start wire 1
13    let wire1 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
14
15    wire1.send(msg!{
16        CHAN: PING
17    }).unwrap();
18
19    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
20    println!("wire 1 ping ret: {:?}", ret);
21
22    // start node
23    let _node = Node::<NsonCodec>::new(
24        socket.clone(),
25        4,
26        vec!["127.0.0.1:8888".parse().unwrap()],
27        KeepAlive::default(),
28        ()
29    ).unwrap();
30
31    // start port
32    let port = Port::<NsonCodec>::new(KeepAlive::default()).unwrap();
33
34    // start wire 2
35    let wire2 = port.connect("127.0.0.1:8888", MessageId::new(), false, msg!{}, None, None).unwrap();
36
37    wire2.send(msg!{
38        CHAN: PING
39    }).unwrap();
40
41    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
42    if let Some(err) = Code::get(&ret) {
43        if err != Code::Ok {
44            println!("wire 2 ping error: {:?}", err);
45            return
46        }
47    }
48
49    println!("wire 2 ping ret: {:?}", ret);
50
51    // wire 1 attach
52    wire1.send(msg!{
53        CHAN: ATTACH,
54        VALUE: "hello"
55    }).unwrap();
56
57    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
58    println!("wire 1 attach ret: {:?}", ret);
59
60    // wire 2 send
61    wire2.send(msg!{
62        ID: MessageId::new(),
63        CHAN: "hello",
64        "hello": "world"
65    }).unwrap();
66
67    // wire 1 recv
68    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
69    if let Some(err) = Code::get(&ret) {
70        if err != Code::Ok {
71            println!("wire 1 recv error: {:?}", err);
72            return
73        }
74    }
75
76    println!("wire 1 recv ret: {:?}", ret);
77}
More examples
Hide additional examples
examples/call.rs (line 15)
11fn main() {
12    let socket = Socket::new(MessageId::new(), NonHook).unwrap();
13
14    // start wire 1
15    let wire1 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
16
17    wire1.send(msg!{
18        CHAN: PING
19    }).unwrap();
20
21    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
22    println!("wire 1 ping ret: {:?}", ret);
23
24    // start wire 2
25    let wire2 = socket.connect(MessageId::new(), false, msg!{}, None, None).unwrap();
26
27    wire2.send(msg!{
28        CHAN: PING
29    }).unwrap();
30
31    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
32    println!("wire 2 ping ret: {:?}", ret);
33
34
35    // wire 1 attach
36    wire1.send(msg!{
37        CHAN: ATTACH,
38        VALUE: "hello"
39    }).unwrap();
40
41    let ret = wire1.wait(Some(Duration::from_secs(1))).unwrap();
42    if let Some(err) = Code::get(&ret) {
43        if err != Code::Ok {
44            println!("wire 1 attach error: {:?}", err);
45            return
46        }
47    }
48
49    println!("wire 1 attach ret: {:?}", ret);
50
51    thread::spawn(move || {
52        loop {
53            if let Ok(ret) = wire1.wait(Some(Duration::from_secs(1))) {
54                if let Some(err) = Code::get(&ret) {
55                    if err != Code::Ok {
56                        println!("wire 1 recv error: {:?}", err);
57                        return
58                    }
59                }
60
61                println!("wire 1 recv ret: {:?}", ret);
62
63                if let Some(_) = ret.get("aaa") {
64                    wire1.send(msg!{
65                        CHAN: "hello",
66                        CODE: 0i32,
67                        TO: ret.get_message_id(FROM).unwrap(),
68                        "lalala": "wawawa"
69                    }).unwrap();
70                }
71            }
72        }
73    });
74
75
76    // wire 2 send
77    wire2.send(msg!{
78        ID: MessageId::new(),
79        CHAN: "hello",
80        "aaa": true,
81        "hello": "world"
82    }).unwrap();
83
84    let ret = wire2.wait(Some(Duration::from_secs(1))).unwrap();
85    if let Some(err) = Code::get(&ret) {
86        if err != Code::Ok {
87            println!("wire 2 send error: {:?}", err);
88            return
89        }
90    }
91
92    println!("wire 2 send ret: {:?}", ret);
93}

Trait Implementations§

Source§

impl Clone for Socket

Source§

fn clone(&self) -> Socket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Connector for Socket

Source§

fn connect( &self, slot_id: MessageId, root: bool, attr: Message, capacity: Option<usize>, timeout: Option<Duration>, ) -> Result<Wire<Message>>

Source§

fn running(&self) -> bool

Source§

impl Drop for Socket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Socket

§

impl !RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin for Socket

§

impl !UnwindSafe for Socket

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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