South Africa’s biggest forum. Discuss, discover, and connect with thousands of members.
Umm okayWhat would you have suggested?
Cool thanks![]()
The first method I used for a protocol I wrote myself. Storing the packet structure in a struct and then doing a "Managed Memcopy" - for want of better words - works well. Because when I was developing the protocol, I could just move/add/whatever the fields in the stuct and my Serialization/DeSerialization to send it off via a socket never needed updating. *But* this packet structure never changed in length.
Obviously this won't work for a variable length packet. I think the having lots of byte[]'s for each field and then encapsulating them into the class properties is a good method. Buffer.BlockCopy is kick-ass, I never knew about it originally...
When I get a chance I'm going to benchmark these against each other, in terms of execution time. I'm not concerned with memory usage.
From what I've read online Buffer.BlockCopy is used for arrays of native type (int, byte, short etc) where as Array.Copy you can use on any type.
The big differences are that Array.Copy will do Boxing/UnBoxing on native types (which affects performance) and Buffer.BlockCopy doesn't do this. The other is that in Array.Copy uses array indexes where as Buffer.BlockCopy uses byte offsets.
ArrayList.Add also does boxing/unboxing on native types, rather use a generic List<>, this again doesn't do this.