Commit 57cb6cbb authored by Dan Robertson's avatar Dan Robertson Committed by GitHub

Merge pull request #18 from dlrobertson/update_use_of_timestamp

Use smoltcp::time types instead of u64 timestamps
parents ff93aabb 76d8feae
This diff is collapsed.
......@@ -5,6 +5,7 @@ use std::fs::File;
use std::io::Write;
use std::rc::Rc;
use smoltcp::time::Instant;
use smoltcp::wire::EthernetAddress;
use buffer_pool::{Buffer, BufferPool};
......@@ -44,7 +45,7 @@ pub struct RxToken {
}
impl smoltcp::phy::RxToken for RxToken {
fn consume<R, F>(self, _timestamp: u64, f: F) -> smoltcp::Result<R>
fn consume<R, F>(self, _timestamp: Instant, f: F) -> smoltcp::Result<R>
where
F: FnOnce(&[u8]) -> smoltcp::Result<R>,
{
......@@ -57,7 +58,7 @@ pub struct TxToken {
}
impl smoltcp::phy::TxToken for TxToken {
fn consume<R, F>(self, _timestamp: u64, len: usize, f: F) -> smoltcp::Result<R>
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> smoltcp::Result<R>
where
F: FnOnce(&mut [u8]) -> smoltcp::Result<R>,
{
......
......@@ -2,6 +2,7 @@ use netutils::getcfg;
use smoltcp;
use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache};
use smoltcp::socket::SocketSet as SmoltcpSocketSet;
use smoltcp::time::{Duration, Instant};
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, IpEndpoint, Ipv4Address};
use std::cell::RefCell;
use std::collections::{BTreeMap, VecDeque};
......@@ -10,7 +11,6 @@ use std::io::{Read, Write};
use std::mem::size_of;
use std::rc::Rc;
use std::str::FromStr;
use std::time::Instant;
use syscall::data::TimeSpec;
use syscall;
......@@ -33,14 +33,16 @@ mod netcfg;
type SocketSet = SmoltcpSocketSet<'static, 'static, 'static>;
type Interface = Rc<RefCell<EthernetInterface<'static, 'static, NetworkDevice>>>;
const MAX_DURATION: Duration = Duration { millis: ::std::u64::MAX };
const MIN_DURATION: Duration = Duration { millis: 0 };
pub struct Smolnetd {
network_file: Rc<RefCell<File>>,
time_file: File,
iface: Interface,
socket_set: Rc<RefCell<SocketSet>>,
startup_time: Instant,
timer: ::std::time::Instant,
ip_scheme: IpScheme,
udp_scheme: UdpScheme,
......@@ -55,8 +57,8 @@ pub struct Smolnetd {
impl Smolnetd {
const MAX_PACKET_SIZE: usize = 2048;
const SOCKET_BUFFER_SIZE: usize = 128; //packets
const MIN_CHECK_TIMEOUT_MS: i64 = 10;
const MAX_CHECK_TIMEOUT_MS: i64 = 500;
const MIN_CHECK_TIMEOUT: Duration = Duration { millis: 10 };
const MAX_CHECK_TIMEOUT: Duration = Duration { millis: 500 };
pub fn new(
network_file: File,
......@@ -98,7 +100,7 @@ impl Smolnetd {
Smolnetd {
iface: Rc::clone(&iface),
socket_set: Rc::clone(&socket_set),
startup_time: Instant::now(),
timer: ::std::time::Instant::now(),
time_file,
ip_scheme: IpScheme::new(Rc::clone(&socket_set), ip_file),
udp_scheme: UdpScheme::new(Rc::clone(&socket_set), udp_file),
......@@ -153,7 +155,7 @@ impl Smolnetd {
Ok(None)
}
fn schedule_time_event(&mut self, timeout: i64) -> Result<()> {
fn schedule_time_event(&mut self, timeout: Duration) -> Result<()> {
let mut time = TimeSpec::default();
if self.time_file.read(&mut time)? < size_of::<TimeSpec>() {
return Err(Error::from_syscall_error(
......@@ -162,7 +164,7 @@ impl Smolnetd {
));
}
let mut time_ms = time.tv_sec * 1000i64 + i64::from(time.tv_nsec) / 1_000_000i64;
time_ms += timeout;
time_ms += timeout.total_millis() as i64;
time.tv_sec = time_ms / 1000;
time.tv_nsec = ((time_ms % 1000) * 1_000_000) as i32;
self.time_file
......@@ -171,37 +173,37 @@ impl Smolnetd {
Ok(())
}
fn poll(&mut self) -> Result<i64> {
fn poll(&mut self) -> Result<Duration> {
let timeout = {
let mut iter_limit = 10usize;
let mut iface = self.iface.borrow_mut();
let mut socket_set = self.socket_set.borrow_mut();
let timestamp = self.get_timestamp();
let timestamp = Instant::from(self.timer);
loop {
if iter_limit == 0 {
break 0;
break MIN_DURATION;
}
iter_limit -= 1;
match iface.poll(&mut socket_set, timestamp) {
Ok(_) | Err(smoltcp::Error::Unrecognized) => (),
Err(e) => {
error!("poll error: {}", e);
break 0;
break MIN_DURATION;
}
}
match iface.poll_at(&socket_set, timestamp) {
Some(n) if n > timestamp => {
break ::std::cmp::min(::std::i64::MAX as u64, n - timestamp) as i64
match iface.poll_delay(&socket_set, timestamp) {
Some(Duration { millis: 0 }) => { }
Some(delay) => {
break ::std::cmp::min(MAX_DURATION, delay)
}
Some(_) => {}
None => break ::std::i64::MAX,
None => break MAX_DURATION
}
}
};
self.notify_sockets()?;
Ok(::std::cmp::min(
::std::cmp::max(Smolnetd::MIN_CHECK_TIMEOUT_MS, timeout),
Smolnetd::MAX_CHECK_TIMEOUT_MS,
::std::cmp::max(Smolnetd::MIN_CHECK_TIMEOUT, timeout),
Smolnetd::MAX_CHECK_TIMEOUT,
))
}
......@@ -223,11 +225,6 @@ impl Smolnetd {
Ok(total_frames)
}
fn get_timestamp(&self) -> u64 {
let duration = Instant::now().duration_since(self.startup_time);
(duration.as_secs() * 1000) + u64::from(duration.subsec_nanos() / 1_000_000)
}
fn notify_sockets(&mut self) -> Result<()> {
self.ip_scheme.notify_sockets()?;
self.udp_scheme.notify_sockets()?;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment