コード例 #1
0
ファイル: BTreeDb4.cs プロジェクト: SharpStar/Star
        public byte[] GetLeafValue(BTreeLeaf leaf, Key key)
        {
            var reader = new LeafReader(this, leaf);

            var unpacked = DataConverter.Unpack("^i", reader.Read(4), 0);

            var numKeys = (int)unpacked[0];

            for (int i = 0; i < numKeys; i++)
            {
                byte[] curKey = reader.Read(KeySize);

                int  pos;
                bool success;
                int  length = (int)VLQ.FromFunc(_ => reader.Read(1)[0], ctr => true, out pos, out success);

                if (!success)
                {
                    throw new Exception("Could not parse VLQ!");
                }

                byte[] value = reader.Read(length);

                if (curKey.SequenceEqual(key.TheKey))
                {
                    return(value);
                }
            }

            return(null);
        }
コード例 #2
0
ファイル: Document.cs プロジェクト: SharpStar/Star
        public static List <Document> ListFromBuffer(byte[] data)
        {
            var documents = new List <Document>();

            using (StarReader reader = new StarReader(data))
            {
                int  pos;
                bool success;
                int  len = (int)VLQ.FromBuffer(data, 0, data.Length, out pos, out success);

                if (!success)
                {
                    return(null);
                }

                reader.BaseStream.Seek(pos, SeekOrigin.Begin);

                for (int i = 0; i < len; i++)
                {
                    documents.Add(ReadDocument(reader));
                }
            }

            return(documents);
        }
コード例 #3
0
ファイル: StarReader.cs プロジェクト: SharpStar/Star
        /// <summary>
        /// Reads a VLQ from the stream
        /// </summary>
        /// <returns>A VLQ</returns>
        public ulong ReadVLQ(out int length, out bool success)
        {
            ulong result = VLQ.FromFunc(_ => ReadByte(), ctr => true, out length, out success);

            if (!success)
            {
                return(0);
            }

            return(result);
        }
コード例 #4
0
        protected virtual void FlushPacket(Packet packet)
        {
            if (ConnectionClient == null || !ConnectionClient.Connected)
            {
                return;
            }

            try
            {
                packet.Direction = Direction;

                EventHandler <PacketEventArgs> packetSending = PacketSending;
                if (packetSending != null)
                {
                    packetSending(this, new PacketEventArgs(Proxy, packet));
                }

                if (packet.Ignore)
                {
                    return;
                }

                byte[] buffer;

                GenericPacket gp = packet as GenericPacket;

                if (gp != null)
                {
                    buffer = gp.Data;
                }
                else
                {
                    buffer = PacketSerializer.Serialize(packet);
                }

                bool compressed = buffer.Length >= 4096 || packet.AlwaysCompress;

                if (compressed)
                {
                    buffer = ZlibStream.CompressBuffer(buffer);
                }

                int length = compressed ? -buffer.Length : buffer.Length;

                byte[] lenBuf = VLQ.CreateSigned(length);

                byte[] finalBuffer = new byte[1 + lenBuf.Length + buffer.Length];
                finalBuffer[0] = packet.PacketId;
                Buffer.BlockCopy(lenBuf, 0, finalBuffer, 1, lenBuf.Length);
                Buffer.BlockCopy(buffer, 0, finalBuffer, 1 + lenBuf.Length, buffer.Length);

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken  = packet;
                args.Completed += Operation_Completed;
                args.SetBuffer(finalBuffer, 0, finalBuffer.Length);

                if (ConnectionClient != null && !ConnectionClient.SendAsync(args))
                {
                    Operation_Completed(this, args);
                }
            }
            catch
            {
                CloseAsync().Wait();
            }
        }
コード例 #5
0
        /// <summary>
        /// Processes the next segment of data and gives the packet id and packet data (if any)
        /// </summary>
        /// <param name="nextSegment">The next segment to be processed</param>
        /// <param name="offset">The offset to start at</param>
        /// <param name="len">The length of the segment to process</param>
        /// <param name="completed">Set to true if the segment processed was a complete packet, otherwise false</param>
        /// <returns>True if more needs to be processed, false if complete</returns>
        public bool ProcessNextSegment(byte[] nextSegment, int offset, int len, out bool completed)
        {
            CurrentPacketData = null;
            completed         = false;

            if (nextSegment.Length > 0)
            {
                PacketBuffer.AddRange(new ByteArraySegment(nextSegment, offset, len));
            }

            if (PacketBuffer.Count <= 1)
            {
                return(false);
            }

            CurrentPacketId = PacketBuffer[0];

            bool compressed = _length.HasValue && _length.Value < 0;

            if (!_length.HasValue)
            {
                bool success;
                _length = VLQ.FromEnumerableSigned(PacketBuffer, 1, PacketBuffer.Count, out _position, out success); //the length of the packet
                _position++;                                                                                         //account for the packet id

                if (!success)
                {
                    _length = null;

                    return(false);
                }

                compressed = _length.Value < 0;
            }

            int lenPos = Math.Abs((int)_length.Value);

            if (PacketBuffer.Count < lenPos + _position)
            {
                return(false);
            }

            byte[] data = PacketBuffer.Skip(_position).Take(lenPos).ToArray();

            _position += data.Length;

            if (compressed) //decompress the packet if it has been compressed
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    using (MemoryStream outStream = new MemoryStream())
                    {
                        using (ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress))
                        {
                            zs.CopyTo(outStream);
                        }

                        data = outStream.ToArray();
                    }
                }
            }

            CurrentPacketData = data;

            //remove the data already processed
            PacketBuffer.RemoveRange(0, _position);

            //reset length
            _length = null;

            completed = true;

            //return true if there are any more packets needing to be processed
            return(PacketBuffer.Count > 0);
        }
コード例 #6
0
        /// <summary>
        /// Write a Signed VLQ to the stream
        /// </summary>
        /// <param name="vlq">The VLQ to write</param>
        public void WriteSignedVLQ(long vlq)
        {
            byte[] buffer = VLQ.CreateSigned(vlq);

            Write(buffer);
        }
コード例 #7
0
        /// <summary>
        /// Write a VLQ to the stream
        /// </summary>
        /// <param name="vlq">The VLQ to write</param>
        public void WriteVlq(ulong vlq)
        {
            byte[] buffer = VLQ.Create(vlq);

            Write(buffer);
        }