-
Notifications
You must be signed in to change notification settings - Fork 34
refactor codec.rs #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pull Request Test Coverage Report for Build 98
💛 - Coveralls |
src/toxcore/dht_new/dht_impl.rs
Outdated
| Ok(None) | ||
| } | ||
| }, | ||
| _ => Ok(None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add log! or smt like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
src/toxcore/dht_new/dht_impl.rs
Outdated
| */ | ||
| // Because UDP codec and tokio use DhtBase for send/receive packet, | ||
| // this function returns DhtBase type object | ||
| pub fn ping_response(&self, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this method should be located inside DHT Node but not inside DhtPacket
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
I moved this function to DhtPacket.rs
src/toxcore/dht_new/dht_impl.rs
Outdated
| } | ||
| } | ||
|
|
||
| impl GetNodes { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reading DhtRequest and then appeared GetNodes. It is unexpected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that the appearing order is not good?
Or impl GetNodes{...} should be other file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, you may either move it to change the order or move it to another file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, dht_impl.rs has collection of impl of Types.
This concept should be discussed more.
src/toxcore/dht_new/dht_impl.rs
Outdated
| } | ||
|
|
||
| /// Serialize Public, Secret Keys to u8 array | ||
| unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the solution to keep PrecomputedKey in the cache as ... serialized slice. But keeping PrecomputedKey in the cache is OK!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
Now, the hasher for SecrectKey, PublicKey, PrecomputedKey is not provided by sodium-oxide.
I am trying to implement custom Hasher.
src/toxcore/dht_new/dht_impl.rs
Outdated
| Ok(d) => d, | ||
| Err(_) => { | ||
| debug!("Decrypting DhtPacket failed!"); | ||
| return Ok(None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that Ok(None) is a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.
If the result of this function is Err, it should contain Err info.
I will change this.
| pub type DhtRecvUdpPacket = (SocketAddr, Option<DhtBase>); | ||
|
|
||
| /** | ||
| SendNodes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be explained better why we use SendNodes here to estimate maximum size of DhtPacket.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
src/toxcore/dht_new/dht_impl.rs
Outdated
| let shared_secret = &encrypt_precompute(key_pair.1, key_pair.0); | ||
| let shared_arr = unsafe { PrecomputedKeys::any_as_u8_slice(&shared_secret) }; | ||
| self.cache.insert (key.clone(), shared_arr.to_vec()); | ||
| return Some(shared_secret.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use return here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
src/toxcore/dht_new/dht_impl.rs
Outdated
| Some(k) => { | ||
| return PrecomputedKey::from_slice(k); | ||
| }, | ||
| None => { ; }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just { } should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
src/toxcore/dht_new/dht_impl.rs
Outdated
|
|
||
| /// Get precomputed keys | ||
| /// If the Key is not found in cache, create symmetric key and insert it into cache for later use. | ||
| pub fn get_symmetric_key (&mut self, key_pair: (&SecretKey, &PublicKey)) -> Option<PrecomputedKey> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change return type from Option to Result to handle error better.
src/toxcore/dht_new/packet_kind.rs
Outdated
| */ | ||
| impl FromBytes for PacketKind { | ||
| named!(from_bytes<PacketKind>, switch!(le_u8, | ||
| 0 => value!(PacketKind::PingReq) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add indent
Cargo.toml
Outdated
| tokio-io = "0.1" | ||
| nom = "3.2" | ||
| cookie-factory = "0.2.2" | ||
| bincode = "0.9.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need bincode?
src/toxcore/dht_new/codec.rs
Outdated
| Ok((*src, None)) | ||
| }, | ||
| IResult::Error(_) => { | ||
| Ok((*src, None)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Ok?
src/toxcore/dht_new/codec.rs
Outdated
| fn encode(&mut self, (addr, dp): Self::Out, into: &mut Vec<u8>) -> SocketAddr { | ||
| let mut buf = [0; MAX_DHT_PACKET_SIZE]; | ||
| if let Ok((_, size)) = dp.to_bytes((&mut buf, 0)) { | ||
| into.extend(&buf[..size]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indent
src/toxcore/dht_new/codec.rs
Outdated
| into.extend(&buf[..size]); | ||
| into.extend(&buf[..size]); | ||
| // TODO: move from tokio-core to tokio and return error instead of panic | ||
| } else {panic!("DhtBase to_bytes error {:?}", dp);} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway you have to fix indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO is about panic!, So It's indent is adjusted to else {
src/toxcore/dht_new/packet.rs
Outdated
| DhtBase::DhtPacket(DhtPacket::arbitrary(g)), | ||
| 1 => | ||
| DhtBase::DhtRequest(DhtRequest::arbitrary(g)), | ||
| _ => panic!("Arbitrary for DhtBase - should not have happened!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is unreachable! macro for such cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if choice == 0 {} else {} will work too.
src/toxcore/dht_new/packet.rs
Outdated
| DhtPacket::new(&precomputed, &pk, DhtPacketPayload::GetNodes(GetNodes::arbitrary(g))), | ||
| 3 => | ||
| DhtPacket::new(&precomputed, &pk, DhtPacketPayload::SendNodes(SendNodes::arbitrary(g))), | ||
| _ => panic!("Arbitrary for DhtPacket - should not have happened!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unreachable!
src/toxcore/dht_new/codec.rs
Outdated
| use std::io::{Error, ErrorKind}; | ||
| use tokio_core::net::UdpCodec; | ||
| use std::net::SocketAddr; | ||
| //use std::io::{Error, ErrorKind}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already imported, so remove.
src/toxcore/dht_new/packet.rs
Outdated
| let ping_kind = match stringify!($np) { | ||
| "NatPingRequest" => NAT_PING_REQUEST as u8, | ||
| "NatPingResponse" => NAT_PING_RESPONSE as u8, | ||
| e => panic!("can not occur {:?}", e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unreachable!
| impl ToBytes for DhtRequest { | ||
| fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> { | ||
| do_gen!(buf, | ||
| gen_be_u8!(0x20) >> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use PacketKind::DhtRequest as u8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let it be 0x20
DHT server initial implementation
Refactor codec.rs for UDP packet.