Example #1
0
            public void OnCompleted(FileWrite data)
            {
                if (data.Count > 0)
                {
                    long offset   = data.Position + data.Count;
                    long position = completed + data.Count + routine.offset - routine.entries[index].Start;

                    Receiver            receiver = new Receiver(routine, index, completed + data.Count);
                    RepositoryViewEntry entry    = routine.entries[index];

                    if (routine.entries.Length > index + 1 && position >= routine.entries[index].Size)
                    {
                        receiver = new Receiver(routine, index + 1, completed + data.Count);
                        entry    = routine.entries[index + 1];
                        position = 0;
                        offset   = 0;
                    }

                    if (completed + data.Count == routine.buffer.Count)
                    {
                        routine.callback.Invoke(new RepositoryViewWrite
                        {
                            Piece  = routine.piece,
                            Block  = routine.block,
                            Buffer = routine.buffer,
                            Count  = routine.buffer.Count
                        });
                    }
                    else
                    {
                        int  left  = routine.buffer.Count - completed - data.Count;
                        long count = entry.Size - position;

                        if (count > left)
                        {
                            count = left;
                        }

                        entry.File.Write(offset, new FileBuffer(routine.buffer.Data, routine.buffer.Offset + completed + data.Count, (int)count), receiver.OnCompleted);
                    }
                }
                else
                {
                    routine.callback.Invoke(new RepositoryViewWrite
                    {
                        Piece  = routine.piece,
                        Block  = routine.block,
                        Buffer = routine.buffer,
                        Count  = completed
                    });
                }
            }
Example #2
0
        public RepositoryViewCache Allocate(string destination, MetainfoEntry[] entries, int pieceSize, int blockSize)
        {
            long position = 0;

            RepositoryViewEntry[] data = new RepositoryViewEntry[entries.Length];

            for (int i = 0; i < entries.Length; i++)
            {
                data[i] = new RepositoryViewEntry
                {
                    Start = position,
                    End   = position + entries[i].Size,
                    Size  = entries[i].Size,
                    File  = factory.OpenOrCreate(entries[i].GetPath(destination))
                };

                data[i].File.SetLength(entries[i].Size);
                position = position + entries[i].Size;
            }

            return(new RepositoryViewCache(data, pieceSize, blockSize));
        }