Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
en committed Jun 17, 2017
1 parent 4390a73 commit 188e1a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
35 changes: 21 additions & 14 deletions src/kcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::cmp;
use std::collections::VecDeque;
use std::io::{self, Error, ErrorKind, Write};

const KCP_RTO_NDL: u32 = 30; // no delay min rto
const KCP_RTO_MIN: u32 = 100; // normal min rto
const KCP_RTO_NDL: u32 = 30; // no delay min rto
const KCP_RTO_MIN: u32 = 100; // normal min rto
const KCP_RTO_DEF: u32 = 200;
const KCP_RTO_MAX: u32 = 60000;
const KCP_CMD_PUSH: u32 = 81; // cmd: push data
const KCP_CMD_ACK: u32 = 82; // cmd: ack
const KCP_CMD_WASK: u32 = 83; // cmd: window probe (ask)
const KCP_CMD_WINS: u32 = 84; // cmd: window size (tell)
const KCP_ASK_SEND: u32 = 1; // need to send KCP_CMD_WASK
const KCP_ASK_TELL: u32 = 2; // need to send KCP_CMD_WINS
const KCP_CMD_PUSH: u32 = 81; // cmd: push data
const KCP_CMD_ACK: u32 = 82; // cmd: ack
const KCP_CMD_WASK: u32 = 83; // cmd: window probe (ask)
const KCP_CMD_WINS: u32 = 84; // cmd: window size (tell)
const KCP_ASK_SEND: u32 = 1; // need to send KCP_CMD_WASK
const KCP_ASK_TELL: u32 = 2; // need to send KCP_CMD_WINS
const KCP_WND_SND: u32 = 32;
const KCP_WND_RCV: u32 = 32;
const KCP_MTU_DEF: u32 = 1400;
Expand All @@ -21,8 +21,8 @@ const KCP_OVERHEAD: u32 = 24;
// const KCP_DEADLINK: u32 = 20; // never used
const KCP_THRESH_INIT: u32 = 2;
const KCP_THRESH_MIN: u32 = 2;
const KCP_PROBE_INIT: u32 = 7000; // 7 secs to probe window size
const KCP_PROBE_LIMIT: u32 = 120000; // up to 120 secs to probe window
const KCP_PROBE_INIT: u32 = 7000; // 7 secs to probe window size
const KCP_PROBE_LIMIT: u32 = 120000; // up to 120 secs to probe window

#[derive(Default)]
struct Segment {
Expand All @@ -42,7 +42,10 @@ struct Segment {

impl Segment {
fn new(size: usize) -> Segment {
Segment { data: Vec::with_capacity(size), ..Default::default() }
Segment {
data: Vec::with_capacity(size),
..Default::default()
}
}
}

Expand Down Expand Up @@ -139,7 +142,10 @@ impl KCP {
ts_flush: KCP_INTERVAL,
ssthresh: KCP_THRESH_INIT, // dead_link: KCP_DEADLINK,
};
kcp.buffer.resize(((KCP_MTU_DEF + KCP_OVERHEAD) * 3) as usize, 0);
kcp.buffer.resize(
((KCP_MTU_DEF + KCP_OVERHEAD) * 3) as usize,
0,
);
kcp
}

Expand Down Expand Up @@ -432,7 +438,8 @@ impl KCP {

let cmd = cmd as u32;
if cmd != KCP_CMD_PUSH && cmd != KCP_CMD_ACK && cmd != KCP_CMD_WASK &&
cmd != KCP_CMD_WINS {
cmd != KCP_CMD_WINS
{
return Err(Error::new(ErrorKind::InvalidData, "invalid data"));
}

Expand Down Expand Up @@ -876,7 +883,7 @@ fn encode32u(buf: &mut [u8], p: &mut usize, n: u32) {
#[inline]
fn decode32u(buf: &[u8], p: &mut usize) -> u32 {
let n = (buf[*p] as u32) | (buf[*p + 1] as u32) << 8 | (buf[*p + 2] as u32) << 16 |
(buf[*p + 3] as u32) << 24;
(buf[*p + 3] as u32) << 24;
*p += 4;
u32::from_le(n)
}
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ impl KcpStream {
let conv = rand::random::<u32>();
let kcp = KCP::new(conv);
KcpStreamConnect::Waiting(KcpStream {
io: socket,
kcp: kcp,
peer_addr: *addr,
})
.boxed()
io: socket,
kcp: kcp,
peer_addr: *addr,
}).boxed()
}

pub fn connect_stream(// stream: net::TcpStream,
addr: &SocketAddr,
handle: &Handle)
-> IoFuture<KcpStream> {
pub fn connect_stream(
// stream: net::TcpStream,
addr: &SocketAddr,
handle: &Handle,
) -> IoFuture<KcpStream> {
unimplemented!()
}

Expand Down
18 changes: 12 additions & 6 deletions tests/kcp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ impl Write for LatencySimulator {
return Err(io::Error::new(io::ErrorKind::Other, "lost"));
}
if self.delay_tunnel.len() >= self.nmax as usize {
return Err(io::Error::new(io::ErrorKind::Other,
format!("exceeded nmax: {}", self.delay_tunnel.len())));
return Err(io::Error::new(
io::ErrorKind::Other,
format!("exceeded nmax: {}", self.delay_tunnel.len()),
));
}

self.current = clock();
Expand Down Expand Up @@ -92,13 +94,17 @@ impl Read for LatencySimulator {
if let Some(pkt) = self.delay_tunnel.front() {
self.current = clock();
if self.current < pkt.ts {
return Err(io::Error::new(io::ErrorKind::Other,
format!("current({}) < ts({})", self.current, pkt.ts)));
return Err(io::Error::new(
io::ErrorKind::Other,
format!("current({}) < ts({})", self.current, pkt.ts),
));
}
len = pkt.data.len();
if len > buf.len() {
return Err(io::Error::new(io::ErrorKind::Other,
format!("buf_size({}) < pkt_size({})", buf.len(), len)));
return Err(io::Error::new(
io::ErrorKind::Other,
format!("buf_size({}) < pkt_size({})", buf.len(), len),
));
}
let buf = &mut buf[..len];
buf.copy_from_slice(&pkt.data[..]);
Expand Down

0 comments on commit 188e1a9

Please sign in to comment.