Cakewalk.Shared.Packets.CoalescedData.TryAddPacket C# (CSharp) Метод

TryAddPacket() публичный Метод

Try to add a packet into the buffer. Returns true if the packet was successfully copied.
public TryAddPacket ( IPacketBase packet ) : bool
packet IPacketBase
Результат bool
        public bool TryAddPacket(IPacketBase packet)
        {
            fixed (byte* buf = DataBuffer)
            {
                int packetSize = packet.Header.SizeInBytes;

                if (m_usedBytes + packetSize < BUFFER_SIZE)
                {
                    //Copy packet into buffer
                    Marshal.StructureToPtr(packet, (IntPtr)(buf + m_usedBytes), false);

                    //Update used bytes and packet count
                    m_usedBytes += (short)packetSize;
                    m_packetCount++;

                    //Update actual size
                    m_header.SizeInBytes = (short)(Marshal.SizeOf(this) - BUFFER_SIZE + m_usedBytes);

                    return true;
                }
            }

            return false;
        }

Usage Example

Пример #1
0
 /// <summary>
 /// Queues a packet to be send on the next update. Will coalesce these packets together.
 /// </summary>
 public void DeferredSendPacket(IPacketBase packet)
 {
     if (!m_currentDeferredPacket.TryAddPacket(packet))
     {
         m_deferredSendList.Add(m_currentDeferredPacket);
         m_currentDeferredPacket = PacketFactory.CreatePacket<CoalescedData>();
         m_currentDeferredPacket.TryAddPacket(packet);
     }
 }