pub struct UdpManager<T>where
T: SerDesType,{ /* private fields */ }
Expand description
Sends and receives datagrams conveniently. Runs a background thread to continuously check for datagrams without interrupting other functionality.
Implementations§
Source§impl<T> UdpManager<T>where
T: SerDesType,
impl<T> UdpManager<T>where
T: SerDesType,
Sourcepub fn get<J>(&self) -> Result<(SocketAddr, J), Error>where
J: DeserializeOwned + 'static,
pub fn get<J>(&self) -> Result<(SocketAddr, J), Error>where
J: DeserializeOwned + 'static,
Provides the oldest datagram of the specified type, if one exists.
Attempts to retrieve the serialized object from the underlying storage depending on the requested data type. The serialized object is removed (if one exists) from the underyling storage regardless of deserialization success. The deserialized object is returned to the user, if deserialization is successful
§Errors
Returns error when the underlying storage is empty or the data could not be deserialized.
§Panics
This will panic if the lock becomes poisioned.
Examples found in repository?
11fn main()
12{
13 let manager = Builder::init()
14 .socket(String::from("0.0.0.0:40061"))
15 //.use_ids(false)
16 .start::<JSON>().unwrap();
17
18 let manager = Arc::from(manager);
19 let man = manager.clone();
20
21 std::thread::spawn(move || {
22 let man = man.clone();
23
24 loop {
25 match man.get::<Message>() {
26 Ok((_,obj)) => println!("{} says: {}", obj.user, obj.message),
27 Err(_) => {}
28 }
29 }
30 });
31
32 loop {
33 let mut message = String::new();
34 std::io::stdin().read_line(&mut message).expect("Did not enter a correct string");
35
36 let m = Message{user: String::from("User1"), message};
37
38 manager.send(m, String::from("127.0.0.1:40062")).unwrap();
39 }
40}
More examples
11fn main()
12{
13 let manager = Builder::init()
14 .socket(String::from("0.0.0.0:40062"))
15 //.use_ids(false)
16 .start::<JSON>().unwrap();
17
18 let manager = Arc::from(manager);
19 let man = manager.clone();
20
21 std::thread::spawn(move || {
22 let man = man.clone();
23
24 loop {
25 match man.get::<Message>() {
26 Ok((_,obj)) => println!("{} says: {}", obj.user, obj.message),
27 Err(_) => {}
28 }
29 }
30 });
31
32 loop {
33 let mut message = String::new();
34 std::io::stdin().read_line(&mut message).expect("Did not enter a correct string");
35
36 let m = Message{user: String::from("User1"), message};
37
38 manager.send(m, String::from("127.0.0.1:40061")).unwrap();
39 }
40}
18fn main() {
19 //source_ip and dest_ip are the same so we don't have to spin up a server and client
20 let source_ip = String::from("0.0.0.0:12000");
21 let net_msg = Builder::init().socket(source_ip).start::<JSON>().unwrap();
22
23 let dest_ip = String::from("127.0.0.1:12000");
24
25 let new_entity = CreateEntity {entity_type: String::from("Boss"), location: (50f32, 15f32, 17f32)};
26
27 match net_msg.send(new_entity, &dest_ip) {
28 Ok(_) => println!("datagram sent!"),
29 Err(e) => println!("datagram failed to send because: {}", e)
30 }
31
32 thread::sleep(time::Duration::from_millis(100));
33
34 let (from_addr, create_entity_message) = net_msg.get::<CreateEntity>().unwrap();
35
36 println!("Message Received!: {:?}", create_entity_message);
37
38 let move_entity = UpdatePos {x: 16f32, y: 17f32, z: 20f32, text: String::from("Hello! I Moved")};
39
40 match net_msg.send(move_entity, from_addr) {
41 Ok(_) => println!("datagram sent!"),
42 Err(e) => println!("datagram failed to send because: {}", e)
43 }
44
45 thread::sleep(time::Duration::from_millis(100));
46
47 let (_, update_pos_message) = net_msg.get::<UpdatePos>().unwrap();
48
49 println!("Message Received!: {:?}", update_pos_message);
50}
Sourcepub fn get_all<J>(&self) -> Result<Vec<(SocketAddr, J)>, Error>where
J: DeserializeOwned + 'static,
pub fn get_all<J>(&self) -> Result<Vec<(SocketAddr, J)>, Error>where
J: DeserializeOwned + 'static,
Provides all datagrams of the specified type, if any exist.
Attempts to retrieve all serialized objects from the underlying storage depending on the requested data type. If storage for the object exists, it will attempt to deserialize any datagrams that exist. If deserialization fails, the datagram is lost. It will return an empty vector as long as the underlying storage existed. If use_ids is set to false, this will return only the datagrams that were able to be converted. All others are removed.
§Errors
Returns an error when the underlying storage for that data type does not exist (different than being empty) or the data could not be deserialized
§Panics
This will panic if the lock becomes poisioned.
Sourcepub fn peek<J>(&self) -> Result<(SocketAddr, J), Error>where
J: DeserializeOwned + 'static,
pub fn peek<J>(&self) -> Result<(SocketAddr, J), Error>where
J: DeserializeOwned + 'static,
Provides the oldest datagram of the specified type, if one exists, without removing it from the underlying storage.
Attempts to retrieve the serialized object from the underlying storage depending on the requested data type. If a serialized object is available, a copy is taken and an attempt to deserialize the data is made. If successful, the deserialized object is returned to the user.
§Errors
Returns error when the underlying storage is empty or the data could not be deserialized.
§Panics
This will panic if the lock becomes poisioned.
Sourcepub fn remove_front<J>(&self) -> Result<(), Error>where
J: DeserializeOwned + 'static,
pub fn remove_front<J>(&self) -> Result<(), Error>where
J: DeserializeOwned + 'static,
Removes the oldest datagram of the specified type, if one exists, without providing it to the user.
if use_ids is set false, it will remove the oldest datagram and the specified type is ignored.
§Errors
Returns error when the underlying storage does not exist.
§Panics
This will panic if the lock becomes poisioned.
Sourcepub fn remove_all<J>(&self) -> Result<(), Error>where
J: DeserializeOwned + 'static,
pub fn remove_all<J>(&self) -> Result<(), Error>where
J: DeserializeOwned + 'static,
Removes all datagram of the specified type, if one exists, without providing it to the user.
if use_ids is set false, it will remove all datagram and the specified type is ignored.
§Errors
Returns error when the underlying storage does not exist.
§Panics
This will panic if the lock becomes poisioned.
Sourcepub fn send<J, A>(&self, datagram: J, dest_addr: A) -> Result<(), Error>where
J: Serialize + 'static,
A: ToSocketAddrs,
pub fn send<J, A>(&self, datagram: J, dest_addr: A) -> Result<(), Error>where
J: Serialize + 'static,
A: ToSocketAddrs,
Deserializes the datagram, appends the ID, and sends to requested location.
Consumes a datagram and a destination address for the datagram to be sent to. An attempt to serialize the data is made; If use_id is true, the datagram ID is prepended to the message. A request to the underlying UDP socket is then made to send the data.
§Errors
Returns an error when the data could not be serialized or when the underyling UDP socket failed to send the message.
§Panics
This will panic if the lock becomes poisioned.
Examples found in repository?
11fn main()
12{
13 let manager = Builder::init()
14 .socket(String::from("0.0.0.0:40061"))
15 //.use_ids(false)
16 .start::<JSON>().unwrap();
17
18 let manager = Arc::from(manager);
19 let man = manager.clone();
20
21 std::thread::spawn(move || {
22 let man = man.clone();
23
24 loop {
25 match man.get::<Message>() {
26 Ok((_,obj)) => println!("{} says: {}", obj.user, obj.message),
27 Err(_) => {}
28 }
29 }
30 });
31
32 loop {
33 let mut message = String::new();
34 std::io::stdin().read_line(&mut message).expect("Did not enter a correct string");
35
36 let m = Message{user: String::from("User1"), message};
37
38 manager.send(m, String::from("127.0.0.1:40062")).unwrap();
39 }
40}
More examples
11fn main()
12{
13 let manager = Builder::init()
14 .socket(String::from("0.0.0.0:40062"))
15 //.use_ids(false)
16 .start::<JSON>().unwrap();
17
18 let manager = Arc::from(manager);
19 let man = manager.clone();
20
21 std::thread::spawn(move || {
22 let man = man.clone();
23
24 loop {
25 match man.get::<Message>() {
26 Ok((_,obj)) => println!("{} says: {}", obj.user, obj.message),
27 Err(_) => {}
28 }
29 }
30 });
31
32 loop {
33 let mut message = String::new();
34 std::io::stdin().read_line(&mut message).expect("Did not enter a correct string");
35
36 let m = Message{user: String::from("User1"), message};
37
38 manager.send(m, String::from("127.0.0.1:40061")).unwrap();
39 }
40}
18fn main() {
19 //source_ip and dest_ip are the same so we don't have to spin up a server and client
20 let source_ip = String::from("0.0.0.0:12000");
21 let net_msg = Builder::init().socket(source_ip).start::<JSON>().unwrap();
22
23 let dest_ip = String::from("127.0.0.1:12000");
24
25 let new_entity = CreateEntity {entity_type: String::from("Boss"), location: (50f32, 15f32, 17f32)};
26
27 match net_msg.send(new_entity, &dest_ip) {
28 Ok(_) => println!("datagram sent!"),
29 Err(e) => println!("datagram failed to send because: {}", e)
30 }
31
32 thread::sleep(time::Duration::from_millis(100));
33
34 let (from_addr, create_entity_message) = net_msg.get::<CreateEntity>().unwrap();
35
36 println!("Message Received!: {:?}", create_entity_message);
37
38 let move_entity = UpdatePos {x: 16f32, y: 17f32, z: 20f32, text: String::from("Hello! I Moved")};
39
40 match net_msg.send(move_entity, from_addr) {
41 Ok(_) => println!("datagram sent!"),
42 Err(e) => println!("datagram failed to send because: {}", e)
43 }
44
45 thread::sleep(time::Duration::from_millis(100));
46
47 let (_, update_pos_message) = net_msg.get::<UpdatePos>().unwrap();
48
49 println!("Message Received!: {:?}", update_pos_message);
50}
Sourcepub fn set_id<F>(&self, id: u64)where
F: 'static,
pub fn set_id<F>(&self, id: u64)where
F: 'static,
Allows the header id of a particular struct to be specified rather than be automatically generated.
Generally, the struct ID is automatically created using a hash of the TypeID. This method allows the struct id to be set by the user. This should be called before any attempt to send or receive a datagram is made. This is commonly used if interacting with a socket that does not use this crate and is expecting a specific ID for the type of message you are sending.
§Panics
This will panic if the lock becomes poisioned.
Trait Implementations§
Source§impl<T> Drop for UdpManager<T>where
T: SerDesType,
Allows the background thread to safely shutdown when the struct loses scope or program performs a shutdown.
impl<T> Drop for UdpManager<T>where
T: SerDesType,
Allows the background thread to safely shutdown when the struct loses scope or program performs a shutdown.