Beispiel #1
0
        public override void ReadNext()
        {
            var start = input.Position;

            var left          = input.Length - input.Position;
            var nextBlockSize = blockSize < left ? blockSize : (int)left;

            var block = Block.CreateBlockForCompression(start, nextBlockSize);

            input.Read(block.Content, 0, nextBlockSize);

            ReadQueue.Enqueue(block);

            readBlocks++;
        }
Beispiel #2
0
        public override void ReadNext()
        {
            if (fileMetadata.TryGetNextRecord(out var record))
            {
                var block = Block.CreateBlockForDecompression(record.Start, record.Length, record.StartCompressed, record.LengthCompressed);

                input.Position = record.StartCompressed;
                input.Read(block.Content, 0, block.Size);
                ReadQueue.Enqueue(block);
            }
            else
            {
                _canRead = false;
            }
        }
Beispiel #3
0
        internal async ReusableTask <bool> ReadAsync(ITorrentData manager, long offset, byte [] buffer, int count)
        {
            Interlocked.Add(ref pendingReads, count);
            await IOLoop;

            if (ReadLimiter.TryProcess(count))
            {
                Interlocked.Add(ref pendingReads, -count);
                return(Read(manager, offset, buffer, count));
            }
            else
            {
                var tcs = new ReusableTaskCompletionSource <bool>();
                ReadQueue.Enqueue(new BufferedIO(manager, offset, buffer, count, tcs));
                return(await tcs.Task);
            }
        }
Beispiel #4
0
    private void FinishReadBody(IAsyncResult result)
    {
        try
        {
            var data = result.AsyncState as RsvData;
            if (data == null)
            {
                LogError(Name, "data = null FinishReadBody");
            }
            else
            {
                var stream = data.Stream;
                var size   = stream.EndRead(result);
                data.Offset = data.Offset + size;
                if (data.Offset < data.DataLength)
                {
                    data.Stream.BeginRead(
                        data.Data,
                        data.Offset,
                        data.DataLength - data.Offset,
                        FinishReadBody,
                        data);
                }
                else
                {
                    TmpReadCmdNumber++;
                    Log(Name,
                        string.Format("接收完消息,时间:{0},第  {1} 个命令", DataTime2String(DateTime.Now), TmpReadCmdNumber));
                    LastServerHeartBeat = DateTime.Now;
                    LastClientHeartBeat = DateTime.Now;
                    var cmd = ParseCmd(data.Data);
                    ReadQueue.Enqueue(cmd);

                    if (TmpReadCmdNumber < MaxReadCmdNumberPerFrame)
                    {
                        if (Client.Available > 4)
                        {
                            StartReadHeader();
                        }
                        else
                        {
                            IsReading = false;
                        }
                    }
                    else
                    {
                        LogWarning(Name, string.Format("这一帧不再接收命令了,时间:{0}", DateTime.Now));
                        IsReading = false;
                    }
                }
            }
        }
        catch (Exception exception)
        {
            LogError(Name, string.Format("{0},{1}", exception.Message, exception.StackTrace));
            HasConnectedToServer = false;
            EventQueue.Enqueue(new Event {
                Type = EventType.DisConnect
            });
        }
    }