Skip to content
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

Not possible to get discriminant of received packet? #17

Closed
Superhepper opened this issue Jun 23, 2020 · 3 comments
Closed

Not possible to get discriminant of received packet? #17

Superhepper opened this issue Jun 23, 2020 · 3 comments

Comments

@Superhepper
Copy link

Superhepper commented Jun 23, 2020

I am interested in retrieving the discriminant of the packet that I have received. Though it seams as this is not possible. And I only wish to ask if I have understood this correctly?

If we use the example from the documentation. What would hope would be possible would be to do the following:

    loop {
        if let Some(response) = connection.receive_packet().unwrap() {
            println!("{:?}", response.discriminant());
            break;
        }
    }

But to me it seams as the only way one can retrieve discriminators is through the enum that defines the protocol?

#[macro_use] extern crate protocol_derive;
#[macro_use] extern crate protocol;

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Handshake;

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Hello {
    id: i64,
    data: Vec<u8>,
}

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Goodbye {
    id: i64,
    reason: String,
}

#[protocol(discriminant = "integer")]
#[derive(Protocol, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum PacketKind {
    #[protocol(discriminator(0x00))]
    Handshake(Handshake),
    #[protocol(discriminator(0xaa))]
    Hello(Hello),
    #[protocol(discriminator(0xaf))]
    Goodbye(Goodbye),
}

fn main() {
println!("Disc: {}", Packet::Handshake(Handshake)).discriminant());
}

In order to get the discriminant of message do I have to write my own function that checks the message type and return the appropriate discriminator by constructing a temporary object and extract it from the temporary object?

use protocol_derive;
use protocol::{ByteOrder, Enum, Parcel, Settings};

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Handshake;

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Hello {
    id: i64,
    data: Vec<u8>,
}

#[derive(Protocol, Clone, Debug, PartialEq)]
pub struct Goodbye {
    id: i64,
    reason: String,
}

#[protocol(discriminant = "integer")]
#[derive(Protocol, Clone, Debug, PartialEq)]
#[repr(u16)]
pub enum PacketKind {
    #[protocol(discriminator(0x00))]
    Handshake(Handshake),
    #[protocol(discriminator(0xaa))]
    Hello(Hello),
    #[protocol(discriminator(0xaf))]
    Goodbye(Goodbye),
}

impl PacketKind {
pub fn get_discriminator(&self) -> u16 {
match self {
    PacketKind::Handshake(_) => {
        PacketKind::Handshake(Handshake).discriminator()
    },
    PacketKind::Hello(_) => {
       PacketKind::Hello(Hello {id: Default::default(), data: Default::default() }).discriminant()
    },
    PacketKind::GoodBye(_) => {
        PacketKind::Goodbye(GoodBye {id: Default::default(), reason: "".to_string() }).discriminant()
    },
}

It seam really weird that the library user would have to go through all this trouble to get the discriminant of the received package or am I missing something?

@dylanmckay
Copy link
Owner

Added an example in dec6f0e

Note: you're getting bitten by the inconsistent naming of discriminant versus discriminator, a pet peeve of mine. I've raised #18 to track.

@dylanmckay
Copy link
Owner

tl;dr there is a method to grab the discriminator, I've added an example. I will close this for now but feel free to reopen if you still have issues.

@Superhepper
Copy link
Author

Ahh you were so right. I read the examples and how to get the discriminator from an enum and I thought was wierd that I could not get from my received package. But you were absolutely right.

use protocol::Enum;

    loop {
        if let Some(response) = connection.receive_packet().unwrap() {
            println!("{:?}", response.discriminator());
            break;
        }
    }
}

Using this actually worked for received packages as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants