Пример #1
0
        /// <summary>
        /// Read and return the next IPacket from the stream
        /// </summary>
        public IPacket Read()
        {
#if ADD_GUARD_BYTES
            byte[] preamble = new byte[4];
            m_Stream.Read(preamble, 0, 4);

            //now compare it against the preamble.
            CompareArrays(preamble, PacketWriter.s_PreambleGuardPattern);
#endif

            int packetSize = (int)m_Reader.ReadUInt64();

            //if the packet size is less than one, that's obviously wrong
            if (packetSize < 1)
            {
                throw new GibraltarSerializationException("The size of the next packet is smaller than 1 byte or negative, which can't be correct.  The packet stream is corrupted.", true);
            }

            // TODO: There's got to be a more efficient way to get this done
            byte[] buffer = new byte[packetSize];
            m_Stream.Read(buffer, 0, packetSize);
            IFieldReader bufferReader = new FieldReader(new MemoryStream(buffer), m_Reader.Strings, m_MajorVersion, m_MinorVersion);

            PacketDefinition definition;
            int typeIndex = (int)bufferReader.ReadUInt32();
            if (typeIndex >= m_cachedTypes.Count)
            {
                definition = PacketDefinition.ReadPacketDefinition(bufferReader);
                if (string.IsNullOrEmpty(definition.TypeName))
                {
                    //we're hosed...  we won't be able to parse this packet.
                    throw new GibraltarSerializationException("The type name of the definition is null, which can't be correct.  The packet stream is corrupted.", true);
                }

                m_cachedTypes.Add(definition);
                m_cachedTypes.Commit();
            }
            else
            {
                definition = m_cachedTypes[typeIndex];
            }

            IPacketFactory factory = m_PacketFactory.GetPacketFactory(definition.TypeName);
            IPacket        packet  = factory.CreatePacket(definition, bufferReader);

            //we used to populate a packet cache here, but a cached packet should be read just once - it shouldn't be in the stream
            //(and I changed PacketWriter to enforce that)

#if ADD_GUARD_BYTES
            byte[] postamble = new byte[4];
            m_Stream.Read(postamble, 0, 4);

            //now compare it against the preamble.
            CompareArrays(postamble, PacketWriter.s_PostambleGuardPattern);
#endif

            return(packet);
        }
Пример #2
0
 private void Commit()
 {
     m_CachedTypes.Commit();
     m_Writer.Commit();
 }