C# Class NetworkCommsDotNet.Tools.PacketBuilder

Packet data is generally broken into multiple variable sized byte chunks or 'partial packets'. This class provides features to effortlessly rebuild whole packets.
Show file Open project: MarcFletcher/NetworkComms.Net Class Usage Examples

Public Methods

Method Description
AddPartialPacket ( int packetBytes, byte partialPacket ) : void

Add a partial packet to the end of the cache by reference.

ClearNTopBytes ( int numBytesToRemove ) : void

Clear N bytes from cache, starting with oldest bytes first.

FirstByte ( ) : byte

Returns the value of the first cached byte.

GetAllData ( ) : byte[]

Copies all cached bytes into a single array and returns. Original data is left unchanged.

NumUnusedBytesMostRecentPartialPacket ( ) : int

Returns the number of unused bytes in the most recently cached partial packet.

PacketBuilder ( ) : System

Create a new instance of the ConnectionPacketBuilder class

ReadDataSection ( int startIndex, int length ) : MemoryStream

Copies the requested cached bytes into a single array and returns. Original data is left unchanged.

RemoveMostRecentPartialPacket ( int &lastPacketBytesRead ) : byte[]

Returns the most recently cached partial packet and removes it from the cache. Used to more efficiently utilise allocated memory space.

Method Details

AddPartialPacket() public method

Add a partial packet to the end of the cache by reference.
public AddPartialPacket ( int packetBytes, byte partialPacket ) : void
packetBytes int The number of valid bytes in the provided partial packet
partialPacket byte A buffer which may or may not be full with valid bytes
return void

ClearNTopBytes() public method

Clear N bytes from cache, starting with oldest bytes first.
public ClearNTopBytes ( int numBytesToRemove ) : void
numBytesToRemove int The total number of bytes to be removed.
return void

FirstByte() public method

Returns the value of the first cached byte.
public FirstByte ( ) : byte
return byte

GetAllData() public method

Copies all cached bytes into a single array and returns. Original data is left unchanged.
public GetAllData ( ) : byte[]
return byte[]

NumUnusedBytesMostRecentPartialPacket() public method

Returns the number of unused bytes in the most recently cached partial packet.
public NumUnusedBytesMostRecentPartialPacket ( ) : int
return int

PacketBuilder() public method

Create a new instance of the ConnectionPacketBuilder class
public PacketBuilder ( ) : System
return System

ReadDataSection() public method

Copies the requested cached bytes into a single array and returns. Original data is left unchanged.
public ReadDataSection ( int startIndex, int length ) : MemoryStream
startIndex int The inclusive byte index to use as the starting position.
length int The total number of desired bytes.
return System.IO.MemoryStream

RemoveMostRecentPartialPacket() public method

Returns the most recently cached partial packet and removes it from the cache. Used to more efficiently utilise allocated memory space.
public RemoveMostRecentPartialPacket ( int &lastPacketBytesRead ) : byte[]
lastPacketBytesRead int The number of valid bytes in the last partial packet added
return byte[]