lnobad / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

Patch to add functions to write an unsigned integer or signed integer at a specific offset. #160

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
public void WriteAt(Int32 offset, Int32 source)

This is useful when writing a message with unknown counts or chunk sizes.

Example: Write unknown count of items
 // This writes the count of items at the beginning of the items
 // after the items have been written to the message 
 int countPosition = netOutgoingMessage.LengthBits;
 netOutgoingMessage.Write((int)0);
 int itemCount = WriteItems(netOutgoingMessage);
 netOutgoingMessage.WriteAt(countPosition, itemCount);

Example: Write chunk size
 // This writes the size of the chunk at the beginning of the chunk
 // after the chunk has been written to the message 
 // This allows the client to skip a chunk if needed without having to parse the contents
 int chunkBitSizePosition = netOutgoingMessage.LengthBits;
 netOutgoingMessage.Write((int)0);
 WriteChunk(netOutgoingMessage);
 int intBitSize = (4 * 8);
 int chunkBitSize = netOutgoingMessage.LengthBits - chunkBitSizePosition - intBitSize;
 netOutgoingMessage.WriteAt(chunkBitSizePosition, chunkBitSize);

Original issue reported on code.google.com by tal...@gmail.com on 12 Jan 2014 at 6:51

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks; added in rev 347

Original comment by lidg...@gmail.com on 13 Jan 2014 at 8:03

GoogleCodeExporter commented 9 years ago
Thanks for patching this in the source.  Great library!
I found a couple of issues with my patch and wanted to follow up.

Fixed issue with previous patch: Buffer was getting set larger than needed for 
the write
- int newBitLength = Math.Max(m_bitLength + 32, offset + 32);
+ int newBitLength = Math.Max(m_bitLength, offset + 32);

Added [CLSCompliant(false)] attribute on WriteAt() methods using unsigned 
values to remove compile warnings.

Added WriteAt() methods for UInt16, Int16 and UInt64.

While this issue is currently marked fixed, I figured it would be best to track 
the changes with the WriteAt() patch together.  If you want I can create 
another issue, just let me know!

Original comment by tal...@gmail.com on 24 Jan 2014 at 3:06

Attachments:

GoogleCodeExporter commented 9 years ago
Patched in rev 350; thanks.

Original comment by lidg...@gmail.com on 25 Jan 2014 at 5:51