NetworkCommsDotNet.Tools.PacketBuilder.RemoveMostRecentPartialPacket C# (CSharp) Method

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[]
        public byte[] RemoveMostRecentPartialPacket(ref int lastPacketBytesRead)
        {
            lock (Locker)
            {
                if (packets.Count > 0)
                {
                    int lastPacketIndex = packets.Count - 1;

                    lastPacketBytesRead = packetActualBytes[lastPacketIndex];
                    byte[] returnArray = packets[lastPacketIndex];

                    totalBytesCached -= packetActualBytes[lastPacketIndex];

                    packets.RemoveAt(lastPacketIndex);
                    packetActualBytes.RemoveAt(lastPacketIndex);

                    if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Trace(" ... reusing byte[" + returnArray.Length + "] from packetBuilder which contains " + lastPacketBytesRead + " existing bytes.");

                    return returnArray;
                }
                else
                    throw new Exception("Unable to remove most recent packet as packet list is empty.");
            }
        }