private void CheckAndRequest(object sender, EventArgs e)
 {
     Debug.WriteLine("CheckAndRequest");
     while (lastChunk != fs.ChunksState.Length && fs.ChunksState.Get(lastChunk))
     {
         lastChunk++;
     }
     lock (endPointManagers) {
         int c = lastChunk;
         foreach (EndPointManager epm in endPointManagers.Values)
         {
             if (epm.ClientFileState != null && epm.AllChunksFinished)
             {
                 Dictionary <int, Chunk> chunks = new Dictionary <int, Chunk>();
                 for (; c < fs.NumberOfChunks && chunks.Count < 8; c++)
                 {
                     if (fs.ChunksState.Get(c) == false &&
                         this.requested.Get(c) == false &&
                         epm.ClientFileState.Get(c) == true)
                     {
                         int chunkPartCount, chunkSize;
                         if (c != fs.NumberOfChunks - 1)
                         {
                             chunkPartCount = Sizes.ChunkPartCount;
                             chunkSize      = Sizes.ChunkSize;
                         }
                         else
                         {
                             chunkPartCount = Sizes.LastChunkPartsCount(fs.FileDescription.FileSize);
                             chunkSize      = Sizes.LastChunkSize(fs.FileDescription.FileSize);
                         }
                         chunks.Add(c, new Chunk(c, epm, chunkPartCount, chunkSize));
                         this.requested.Set(c, true);
                     }
                 }
                 if (chunks.Count != 0)
                 {
                     epm.AssignNewChunks(chunks);
                     FileTransferCenter.ftc.ndm.RequestChunks(epm.EndPointId, this.fileId, chunks.Keys.ToArray());
                 }
             }
         }
     }
 }
        private void SendLastChunk(int requester, int chunk)
        {
            byte[] chunkData       = fs.FileManager.ReadChunkData(chunk);
            int    chunkPartsCount = Sizes.LastChunkPartsCount(fs.FileDescription.FileSize);

            for (int i = 0; i < chunkPartsCount - 1; i++)
            {
                SendChunkPart(requester, chunk, i, chunkData, i * Sizes.ChunkPartSize, Sizes.ChunkPartSize);
            }
            int lastChunkPartSize = Sizes.LastChunkPartSize(fs.FileDescription.FileSize);

            SendChunkPart(
                requester,
                chunk,
                chunkPartsCount - 1,
                chunkData,
                (chunkPartsCount - 1) * Sizes.ChunkPartSize,
                lastChunkPartSize);
        }
        public void ResendChunkPart(int requester, int chunkId, int chunkPartId)
        {
            if (paused)
            {
                return;
            }
            byte[] chunkPart;

            if (chunkId == fs.NumberOfChunks - 1)
            {
                if (Sizes.LastChunkPartsCount(fs.FileDescription.FileSize) - 1 == chunkPartId)
                {
                    chunkPart = fs.FileManager.ReadLastChunkLastPartData(chunkId, chunkPartId);
                    SendChunkPart(requester, chunkId, chunkPartId, chunkPart, 0, chunkPart.Length);
                    return;
                }
            }
            chunkPart = fs.FileManager.GetDataOfChunkPart(chunkId, chunkPartId);
            SendChunkPart(requester, chunkId, chunkPartId, chunkPart, 0, chunkPart.Length);
        }