Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. t// socket.rs
  2. pub mod socket
  3. {
  4. use std::net::{UdpSocket, SocketAddr};
  5.  
  6. pub struct Conn
  7. {
  8. addr: SocketAddr,
  9. socket: UdpSocket
  10. }
  11.  
  12. impl Conn
  13. {
  14. pub fn new(ip_port: &str) -> Conn
  15. {
  16. Conn {
  17. addr: ip_port.parse().unwrap(),
  18. socket: UdpSocket::bind(ip_port).unwrap()
  19. }
  20. }
  21.  
  22. pub fn listen(&self) -> (Vec<u8>, SocketAddr)
  23. {
  24. let mut buf = vec![0; 200];
  25. let (_, src) = &self.socket.recv_from(&mut buf).expect("no data");
  26. (buf, *src)
  27. }
  28.  
  29. pub fn send(&self, data: &str, dest: &str)
  30. {
  31. &self.socket.send_to(&data.to_string().into_bytes(), dest)
  32. .expect("cannot send");
  33.  
  34. }
  35. }
  36. }
  37.  
  38.  
  39. // main.rs
  40. use socket::Conn;
  41. use cron::Cron;
  42.  
  43. fn main()
  44. {
  45. let srv = Conn::new("0.0.0.0:9999");
  46. println!("Server is running");
  47.  
  48. let (_, src) = srv.listen();
  49. println!("listening to {:?}", src);
  50.  
  51. Cron::job(1);
  52. }
  53.  
  54.  
  55. // cronjob.rs
  56. pub mod cron
  57. {
  58. use super::socket::Conn;
  59.  
  60. pub struct Cron
  61. {
  62. time: u16
  63. }
  64.  
  65. impl Cron
  66. {
  67. pub fn job(delay: u16)
  68. {
  69. for _ in 0..10 // with dealy of course.
  70. {
  71.  
  72. /*
  73. this might fix by doing the Conn::new(...) like what i did in main.rs.
  74. >> let srv = Conn::new("0.0.0.0:9999");
  75. >> srv.send("message", "192.168.24.51:8545");
  76.  
  77. But that create the server bind again and again. which i dont wish to happen.
  78. */
  79.  
  80. Conn::send("message", "192.168.24.51:8545"); // e.g conected peer ip is 192.168.24.51:8545
  81.  
  82. /*
  83. i wanna send the message from this module(cronjob.rs),
  84. Instead of returning the message data & do srv.send(data) it in the main.rs.
  85. */
  86.  
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement