Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t// socket.rs
- pub mod socket
- {
- use std::net::{UdpSocket, SocketAddr};
- pub struct Conn
- {
- addr: SocketAddr,
- socket: UdpSocket
- }
- impl Conn
- {
- pub fn new(ip_port: &str) -> Conn
- {
- Conn {
- addr: ip_port.parse().unwrap(),
- socket: UdpSocket::bind(ip_port).unwrap()
- }
- }
- pub fn listen(&self) -> (Vec<u8>, SocketAddr)
- {
- let mut buf = vec![0; 200];
- let (_, src) = &self.socket.recv_from(&mut buf).expect("no data");
- (buf, *src)
- }
- pub fn send(&self, data: &str, dest: &str)
- {
- &self.socket.send_to(&data.to_string().into_bytes(), dest)
- .expect("cannot send");
- }
- }
- }
- // main.rs
- use socket::Conn;
- use cron::Cron;
- fn main()
- {
- let srv = Conn::new("0.0.0.0:9999");
- println!("Server is running");
- let (_, src) = srv.listen();
- println!("listening to {:?}", src);
- Cron::job(1);
- }
- // cronjob.rs
- pub mod cron
- {
- use super::socket::Conn;
- pub struct Cron
- {
- time: u16
- }
- impl Cron
- {
- pub fn job(delay: u16)
- {
- for _ in 0..10 // with dealy of course.
- {
- /*
- this might fix by doing the Conn::new(...) like what i did in main.rs.
- >> let srv = Conn::new("0.0.0.0:9999");
- >> srv.send("message", "192.168.24.51:8545");
- But that create the server bind again and again. which i dont wish to happen.
- */
- Conn::send("message", "192.168.24.51:8545"); // e.g conected peer ip is 192.168.24.51:8545
- /*
- i wanna send the message from this module(cronjob.rs),
- Instead of returning the message data & do srv.send(data) it in the main.rs.
- */
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement