Help with ws-rs and prost decoding messages [Solved]


#1

I’m looking for some guidance or pointers. Let me first start by saying I have a very basic understanding of Rust and would consider myself a novice (maybe I’m just in over my head). The project I’m working on connects to a websocket server using ws-rs and I would like to decode the ws::Message with prost::Message. I have successfully generated all the protocols from the *.proto files but I’m failing to understand how I would go about decoding the messages.

An example of the generated sc2api_protocol on build can be found here: https://github.com/ttdonovan/sc2-api-rs/blob/master/examples/sc2api_protocol.rs

To run the example I’ve been using a local git clone of pysc2, editing the source to manually setting the port to 5000: https://github.com/deepmind/pysc2/blob/master/pysc2/lib/sc_process.py#L62

cargo run --example client

Any tips or references to other projects would greatly be appreciated.


#2

Is your question about how to decode a given type from the ws::Message that you get or how to know what type to decode into?


#3

Both I guess… In the on_message function it’s a binary ws::Message so it’s unclear to me how I know which prost::Message to decode it into. I’ve tried looking through the prost test source and I found an example they called RoundTrip but it’s explicitly defining the prost Message.


#4

How does the Starcraft protocol work? What does it send you on connect, if anything? Prost will expect you to know what type of message you’re going to deserialize which means you’ll need to know how the protocol works and what/when messages are exchanged.


#5

Oh Ok that didn’t even cross my mind - I’m thinking all the responses from StartCraft are Response. So I’m guessing I’ll need to use that to decode the ws binary into a prost message.

https://github.com/ttdonovan/sc2-api-rs/blob/master/examples/sc2api_protocol.rs#L1630

I believe these two links outline the usage of the StarCraft protocol:


#6

Ok so let’s assume it’s Response. You’d then deserialize it like this I suspect:

if let Message::Binary(ref v) = msg {
  let response = Response::decode(v);
}

Or you can use let response = Response::decode(msg.into_data()) if you want to consume the message and you know it’s binary.

Note that response in both cases is a Result<Response, DecodeError> so you’ll want to handle the errors.

Disclaimer: I don’t know anything about the SC protocol nor have I used Prost or ws-rs. I’m just going by their API docs.


#7

Thank you for the help I’m now able to decode the Response prost::Message.

make connection...
on_open
Response { error: [], status: Some(3), response: Some(Action(ResponseAction { result: [1] })) }
Response { error: [], status: Some(3), response: Some(Step(ResponseStep)) }
Response { error: [], status: Some(3), response: Some(Observation(ResponseObservation { actions: [Action { action_raw: ...
Response { error: [], status: Some(3), response: Some(Action(ResponseAction { result: [1] })) }
Response { error: [], status: Some(3), response: Some(Step(ResponseStep)) }
Response { error: [], status: Some(3), response: Some(Observation(ResponseObservation { actions: [Action { action_raw: ...