Last active
August 29, 2015 14:00
-
-
Save Noxivs/11016094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate time; | |
use std::io::{Listener, Acceptor}; | |
use std::io::net::tcp::{TcpListener}; | |
use std::io::net::ip::{SocketAddr, Ipv4Addr}; | |
use std::io::timer; | |
static TICK_TIME: i32 = 60; //ms | |
fn tick(last_tick: &mut i32) { | |
let start = (time::precise_time_s() * 1000.0) as i32; | |
//let update = start - *last_tick; | |
*last_tick = start; | |
on_tick(); | |
let stop = (time::precise_time_s() * 1000.0) as i32; | |
let update_diff = stop - start; | |
if update_diff > TICK_TIME | |
{ | |
tick(last_tick); | |
} | |
else | |
{ | |
let timeout = (start + TICK_TIME) - stop; | |
timer::sleep(timeout as u64); | |
tick(last_tick); | |
} | |
} | |
fn on_tick() { | |
println!("tick"); | |
} | |
fn main() { | |
spawn(proc(){ | |
let mut last_tick = ~((time::precise_time_s() * 1000.0) as i32); | |
tick(last_tick); | |
}); | |
let mut acceptor = TcpListener::bind(SocketAddr { | |
ip: Ipv4Addr(127, 0, 0, 1), | |
port: 443 | |
}).unwrap().listen(); | |
for mut _stream in acceptor.incoming() { | |
// accept client | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment