Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions rust/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct KeepAliveState {
}

impl KeepAliveState {
pub fn new() -> KeepAliveState {
pub fn new(current_time: f64) -> KeepAliveState {
KeepAliveState {
last_sent: 0.0,
last_response: 0.0
last_sent: current_time,
last_response: current_time
}
}

Expand Down Expand Up @@ -65,9 +65,10 @@ impl Channel {
addr: &SocketAddr,
protocol_id: u64,
client_idx: usize,
max_clients: usize) -> Channel {
max_clients: usize,
time: f64) -> Channel {
Channel {
keep_alive: KeepAliveState::new(),
keep_alive: KeepAliveState::new(time),
send_key: send_key.clone(),
recv_key: recv_key.clone(),
replay_protection: ReplayProtection::new(),
Expand Down Expand Up @@ -132,4 +133,4 @@ impl Channel {
pub fn get_addr(&self) -> &SocketAddr {
&self.addr
}
}
}
22 changes: 12 additions & 10 deletions rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ impl<I,S> ClientData<I,S> where I: SocketProvider<I,S> {
addr,
self.token.protocol,
0,
0)
0,
self.time)
},
None => ()
}
Expand Down Expand Up @@ -178,7 +179,7 @@ impl<I,S> ClientData<I,S> where I: SocketProvider<I,S> {

fn send_connect_token(&mut self) -> Result<usize, SendError> {
let packet = packet::ConnectionRequestPacket::from_token(&self.token);

self.channel.send(self.time, &packet::Packet::ConnectionRequest(packet), None, &mut self.socket)
}

Expand Down Expand Up @@ -212,7 +213,8 @@ impl<I,S> Client<I,S> where I: SocketProvider<I,S> {
&token.hosts.get().next().unwrap(),
token.protocol,
0,
0);
0,
0.0);

let mut data = ClientData {
time: 0.0,
Expand Down Expand Up @@ -356,7 +358,7 @@ impl<I,S> Client<I,S> where I: SocketProvider<I,S> {
fn set_read_timeout(&mut self, duration: Option<Duration>) -> Result<(), io::Error> {
self.data.socket.set_recv_timeout(duration)
}

#[cfg(test)]
pub fn get_socket_state(&mut self) -> &mut S {
&mut self.data.socket_state
Expand All @@ -380,7 +382,7 @@ mod test {
server: Option<Server<I,S>>
}


#[allow(dead_code)]
fn enable_logging() {
use env_logger::LogBuilder;
Expand Down Expand Up @@ -446,7 +448,7 @@ mod test {
}
}
}

#[test]
fn test_client_connect() {
let mut harness = TestHarness::<UdpSocket,()>::new(None);
Expand All @@ -461,7 +463,7 @@ mod test {
ClientEvent::NewState(State::SendingConnectionResponse) => (),
s => assert!(false, "{:?}", s)
}

harness.update_server();
match harness.update_client().unwrap() {
ClientEvent::NewState(State::Connected) => (),
Expand All @@ -476,14 +478,14 @@ mod test {
//Pending response
harness.update_server();
harness.update_client().unwrap();

//Connected
harness.update_server();
match harness.update_client().unwrap() {
ClientEvent::NewState(State::Connected) => (),
s => assert!(false, "{:?}", s)
}

for i in 1..NETCODE_MAX_PAYLOAD_SIZE {
let mut data = [0; NETCODE_MAX_PAYLOAD_SIZE];
for d in 0..i {
Expand Down Expand Up @@ -529,4 +531,4 @@ mod test {

}
}
}
}
8 changes: 4 additions & 4 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Rust implementation of netcode.io protocol.
//!
//! This crate contains [Server](struct.Server.html), [Client](struct.Client.html) and [ConnectToken](struct.ConnectToken.html) used to establish a netcode.io session.
//!
//!
//! # Connect Token
//! Each netcode.io session starts with a [ConnectToken](struct.ConnectToken.html). This token is handed out by a HTTPS webserver, authentication server or other *private* avenue
//! to allow a client to establish a connection with a netcode.io based server. Rather than specifying an address the list of hosts are contained within
Expand Down Expand Up @@ -104,7 +104,7 @@
//! let client_id = get_client_id(); //Unique u64 client id.
//! let sequence = get_next_sequence(); //sequence passed to generate() must
//! //be a monotically increasing u64
//! //to prevent replay attacks.
//! //to prevent replay attacks.
//! let user_data = None; //Any custom user data, can be up to 256 bytes.
//! //Will be encrypted and returned to sever on connect.
//!
Expand Down Expand Up @@ -160,9 +160,9 @@ mod token;
mod packet;
mod socket;

pub use token::{ConnectToken};
pub use token::{ConnectToken, DecodeError};
pub use common::{NETCODE_MAX_PACKET_SIZE, NETCODE_MAX_PAYLOAD_SIZE, NETCODE_USER_DATA_BYTES};
pub use server::{UdpServer, Server, ServerEvent};
pub use client::{UdpClient, Client, ClientEvent, State as ClientState};
pub use crypto::{generate_key};
pub use error::*;
pub use error::*;
6 changes: 3 additions & 3 deletions rust/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn encode(out: &mut [u8], protocol_id: u64, packet: &Packet, crypt_info: Opt

Ok(writer.position() as usize)
} else {
if let Some((sequence,private_key)) = crypt_info {
if let Some((sequence, private_key)) = crypt_info {
let (prefix_byte, offset) = {
let mut write = &mut io::Cursor::new(&mut out[..]);

Expand Down Expand Up @@ -727,11 +727,11 @@ fn test_decode_challenge_token() {
capi_scratch.as_mut_ptr(),
capi_scratch.len() as i32,
&mut native_token);

assert_eq!(serialize, 1);
assert_eq!(native_token.client_id, client_id);
for i in 0..user_data.len() {
assert_eq!(user_data[i], native_token.user_data[i]);
}
}
}
}
16 changes: 8 additions & 8 deletions rust/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ enum TickResult {

impl<I,S> Server<I,S> where I: SocketProvider<I,S> {
/// Constructs a new Server bound to `local_addr` with `max_clients` and supplied `private_key` for authentication.
pub fn new<A>(local_addr: A, max_clients: usize, protocol_id: u64, private_key: &[u8; NETCODE_KEY_BYTES])
pub fn new<A>(local_addr: A, max_clients: usize, protocol_id: u64, private_key: &[u8; NETCODE_KEY_BYTES])
-> Result<Server<I,S>, CreateError>
where A: ToSocketAddrs {
let bind_addr = local_addr.to_socket_addrs().unwrap().next().unwrap();
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<I,S> Server<I,S> where I: SocketProvider<I,S> {
pub fn update(&mut self, elapsed: f64) {
self.internal.update(elapsed);
}

/// Checks for incoming packets, client connection and disconnections. Returns `None` when no more events
/// are pending.
pub fn next_event(&mut self, out_packet: &mut [u8; NETCODE_MAX_PAYLOAD_SIZE]) -> Result<Option<ServerEvent>, UpdateError> {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<I,S> Server<I,S> where I: SocketProvider<I,S> {
//If we didn't have an existing client then handle a new client
result.unwrap_or_else(|| self.internal.handle_new_client(addr, data, payload, &mut self.clients))
}

fn find_client_by_id<'a>(clients: &'a mut ClientVec, id: ClientId) -> Option<&'a mut Connection> {
clients.iter_mut().map(|c| c.as_mut()).find(|c| {
if let &Some(ref c) = c {
Expand Down Expand Up @@ -363,7 +363,7 @@ impl<I,S> ServerInternal<I,S> where I: SocketProvider<I,S> {
Some(self.send_client_challenge(client, private_data).map(|_| None))
} else {
None
};
};

existing_client_result.unwrap_or_else(|| {
//Find open index
Expand All @@ -372,7 +372,7 @@ impl<I,S> ServerInternal<I,S> where I: SocketProvider<I,S> {
let mut conn = Connection {
client_id: private_data.client_id,
state: ConnectionState::PendingResponse,
channel: Channel::new(&private_data.server_to_client_key, &private_data.client_to_server_key, addr, self.protocol_id, idx, clients.len())
channel: Channel::new(&private_data.server_to_client_key, &private_data.client_to_server_key, addr, self.protocol_id, idx, clients.len(), self.time)
};

self.send_client_challenge(&mut conn, private_data)?;
Expand Down Expand Up @@ -426,7 +426,7 @@ impl<I,S> ServerInternal<I,S> where I: SocketProvider<I,S> {

fn validate_client_token(&mut self, req: &packet::ConnectionRequestPacket) -> Option<token::PrivateData> {
if req.version != *NETCODE_VERSION_STRING {
trace!("Version mismatch expected {:?} but got {:?}",
trace!("Version mismatch expected {:?} but got {:?}",
NETCODE_VERSION_STRING, req.version);

return None;
Expand Down Expand Up @@ -686,7 +686,7 @@ mod test {
let len = packet::encode(&mut data, PROTOCOL_ID, &packet, None, None).unwrap();
self.socket.send_to(&data[..len], &self.server.get_local_addr().unwrap()).unwrap();
}

fn validate_challenge(&mut self) {
let mut data = [0; NETCODE_MAX_PAYLOAD_SIZE];
self.server.update(0.0);
Expand Down Expand Up @@ -796,7 +796,7 @@ mod test {
Ok((_,p)) => assert!(false, "unexpected packet type {}", p.get_type_id()),
Err(o) => assert!(false, "unexpected {:?}", o)
}

}
}

Expand Down