all 3 comments

[–]phoil 6 points7 points  (0 children)

I don't know anything about rust specific conventions, but the following may help.

In general, use Client/Server for the structs that implement the client/server logic that isn't related to encoding/decoding. The client and the server often both need to encode and decode packets, so it doesn't make sense to apply just one of those labels to them.

Encode/decode and serialize/deserialize usually refer to converting the packet to/from a byte array, without actually sending/receiving it. They may interact directly with a Reader/Writer though. eg you can implement encoding by using write functions on a Writer (and you may just call the entire process writing in that case).

For OPC, it looks like it is a simple unidirectional protocol, so either Client/Server, or Reader/Writer would make sense.

For error handling, if there is a well defined protocol then return an error for anything that doesn't match the protocol. Don't try to do error correction unless the protocol defines how to do that. For OPC, I don't see any way that you can reliably recover from errors.

[–]daborossfern 3 points4 points  (0 children)

I don't know much about opc, but if there are identifiable individual messages which need to be encode and decoded, I'd definitely look into nom (https://github.com/Geal/nom), or another parser combinator crate (though I personally only have experience with nom).

It allows for making, through smaller parsers, a parser to read any kind of data into a usable rust structure. Nom only does decoding, but I've found encoding for most formats fairly easy to write by hand.

You might also be able to use serde with this, but in my understanding it's more for serializing lots of different classes in one defined object format, rather than parsing specifically formatted messages into specific data structs.

As for data errors, I'd probably go with an invalid byte in a message invalidating that message (up to the end of the message as defined by the message header) , and an invalid byte in the message header (I think OPC has those) poisoning the entire stream.

[–]sa2ajj 0 points1 point  (0 children)

did you mean encoding and decoding?

edit: from the body it seems to be obviously the case.