I've been working with network protocol specification targeted for implementation in C language and defined respectively in C style like:
struct some_header {
uint8_t version;
uint8_t type;
uint16_t length;
uint32_t id; /
};
struct some_struct {
struct some_header header;
uint8_t field1;
uint8_t pad[3]; /* Pad to 32 bits */
uint32_t field2;
};
There are around hundred structures like "some_struct" defined in the specification.
Now the goal is to replicate the definitions in C# and grow some functionallity around those structures.
What is the best primitive to use in C# to represent the structures like in example above, assuming that it will be nice to have them represented as an object?
Use unsafe clause to replicate C-like definition and then use class wrapper around every structure
Use StructLayout attribute to guide the layout inside the managed structure and use wrapper class for each such structure
Use full-fledged class and only go to wire representation with Memory or Network Streams
Any other ideas, except managed c++?
Secondly, obviously the task by itself is very labor intensive and error-prone in any case listed. Is there any automation to help converting C header into one of the solutions above? Could someone recommend one, please?
UPD.
Some drawings to describe the problem better.
(BoxA) --- wire protocol running over TCP/IP --- (BoxB)
Assume BoxA runs some code written in pure C, BoxB ought to run code written in C#.
And wire protocol represents this huge specification with 100+ structures to communicate BoxA and BoxB. It could have been much easier if protocol had been defined with Google Protocol Buffers or Apache Thrift, but unfortunately I'm dealing with standard defined as it is.
use struct for plain-old-data structures without any class-like features; use class when you make use of features such as private or protected members, non-default constructors and operators, etc.
Classes have the advantage of being reference types — passing a reference is more efficient than passing a structure variable with all its data.
Both structure and union are user-defined data types in C programming that hold multiple members of different data types. Structures are used when we need to store distinct values for all the members in a unique memory location, while unions help manage memory efficiently.
A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.
IMHO it's better to manually convert them as follows:
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct Header
{
public Byte version;
public Byte type;
public UInt16 length;
public UInt32 id;
}
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct Struct
{
public Header header;
public Byte field1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public Byte[] pad;
public UInt32 field2;
}
You will maybe have to use them in together with native functions in the future... so it' better to keep them as structures and use them directly in your .NET code. They will be very useful if you have to pass them as parameters for native methods or to convert result IntPtrs back to them (Marshal.PtrToStructure).
Is there any automation to help converting C header into one of the solutions above?
Use C preprocessor to get one big file, then use script to cut struct definitions, then apply search & replace to convert types and add attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With