Exemple #1
0
        public IEnumerable <IPacket> UpdateBuffer(bool shouldCopy)
        {
            if (shouldCopy)
            {
                PacketBuffer.AddRange(NetworkBuffer);
            }

            Queue <byte[]> toProcess = new Queue <byte[]>();

            toProcess.Enqueue(PacketBuffer.ToArray());

            while (toProcess.Count > 0)
            {
                byte[] arr = toProcess.Dequeue();

                using (StarboundStream s = new StarboundStream(arr))
                {
                    if (WorkingLength == long.MaxValue && s.Length > 1)
                    {
                        _packetId = s.ReadUInt8();

                        try
                        {
                            WorkingLength = s.ReadSignedVLQ();
                        }
                        catch
                        {
                            WorkingLength = long.MaxValue;

                            yield break;
                        }

                        DataIndex = (int)s.Position;

                        Compressed = WorkingLength < 0;

                        if (Compressed)
                        {
                            WorkingLength = -WorkingLength;
                        }
                    }

                    if (WorkingLength != long.MaxValue)
                    {
                        if (s.Length >= WorkingLength + DataIndex)
                        {
                            if (s.Position != DataIndex)
                            {
                                s.Seek(DataIndex, SeekOrigin.Begin);
                            }

                            byte[] data = s.ReadUInt8Array((int)WorkingLength);

                            if (Compressed)
                            {
                                data = ZlibStream.UncompressBuffer(data);
                            }

                            //todo Omit this after testing
                            //SharpStarLogger.DefaultLogger.Info(string.Format("{0}{1}","ID : ",_packetId));

                            IPacket packet = Decode(_packetId, data);

                            WorkingLength = long.MaxValue;

                            byte[] rest = s.ReadToEnd();
                            PacketBuffer = rest.ToList();

                            if (rest.Length > 0)
                            {
                                toProcess.Enqueue(rest);
                            }

                            yield return(packet);
                        }
                    }
                }
            }
        }