mycroes / Sally7

C# implementation of Siemens S7 connections with a focus on performance
MIT License
57 stars 23 forks source link

Expose protocol struct sizes #16

Open mycroes opened 3 years ago

mycroes commented 3 years ago

Sample fix for #14.

@scamille Let me know what you think (didn't have the time to finish this right now).

scamille commented 3 years ago

Sally7.Protocol.SizeOf looks nice. I would just blindly assume that my available payload in bytes for a read request is:

var sizeAvailable = connection.Parameters.MaxPDU - SizeOf.ReadRequest - NumberOfDataItems * SizeOf.DataItem;

Do I need to do something with SizeOf.Tpkt?

mycroes commented 3 years ago

The assumption is incorrect unfortunately. There's a two (I think) additional structs that are part of the message and there's the TPKT header, although I doubt that the TPKT is part of the PDU constraint. I'm not sure how I'm going to clarify the different message parts, but I'll make sure there's also some convenient property that'll let you worry about DataItems only.

scamille commented 3 years ago

Let me know if I should do part of the work for such a "Max Payload" calculator. I mainly wanted some of the internal sizes exposed so I don't have to use some magic numbers.

In the end I think I want:

public static int MaximumReadRequestPayload(int maximumPduSize, int numberOfDataItems);
public static int MaximumWriteRequestPayload(int maximumPduSize, int numberOfDataItems);

public static int MaximumReadRequestPayload(this IS7Connection connection, int numberOfDataItems) =>
  MaximumReadRequestPayload(connection.Parameters.MaximumPduSize, numberOfDataItems);
public static int MaximumWriteRequestPayload(this IS7Connection connection, int numberOfDataItems) =>
  MaximumWriteRequestPayload(connection.Parameters.MaximumPduSize, numberOfDataItems);
mycroes commented 3 years ago

I'm almost done, actually tested the maximums as well because I wasn't sure what the boundaries were. I think I'll have time to round it all up tomorrow.

mycroes commented 3 years ago

The changes were kinda done, but I wasn't able to commit them earlier. I didn't implement the extensions per your suggestion, because I fail to see a good usecase for it. I can image you'd want to know the maximum available length in bytes for a single DataItem, but I don't see how it matters what the available bytes is for 2, because logically you'd only want to cut the size of one of the DataItems. I'm not yet using this myself either, so I'm not sure what would be a nice solution.