Ejemplo n.º 1
0
        internal void ReceiveChunk(FileChunkMessage chunk)
        {
            if (chunk.IsStartChunk)
            {
                PrepareForBuild(chunk);
            }

            _stream.Write(chunk.Chunk, 0, chunk.Chunk.Length);

            if (chunk.IsEndChunk)
            {
                Debug.Write("Finished");
                CompleteBuild();
            }
        }
        private void SendFileChunk(FileDescriptor description, FileChunkMessage chunk)
        {
            AddDataToMessage(chunk, chunk.Chunk);

            SendMessage(chunk);
        }
 public FileTransferEventArgs(FileChunkMessage chunk)
 {
     FileChunk = chunk;
 }
        internal void FileChunkReceived(FileChunkMessage chunkMessage)
        {
            FileServerNegotiator negotiator = _activeNegotiators.Where(n => n.Id == chunkMessage.Header.Id).FirstOrDefault();

            if (negotiator == null)
            {
                //Implement as event?
                throw new InvalidOperationException("File Chunk received, though no negotiator was available to accept it");
            }

            negotiator.ChunkReceived(chunkMessage);
            negotiator.TimedOut-= new EventHandler(FileServiceNegotiator_TimedOut);
            _activeNegotiators.Remove(negotiator);
        }
Ejemplo n.º 5
0
        public void ReceiveChunk(FileChunkMessage message)
        {
            if (_timerHandle != null)
            {
                _timerHandle.Cancel();

                _waitingChunks.Enqueue(message);

                _timerHandle = _host.Reactor.AddTimer(DateTime.Now, CheckForBuildTask, null);
            }
        }
Ejemplo n.º 6
0
        private bool WriteChunkToFile(FileChunkMessage chunk)
        {
            if (_host == null) throw new InvalidOperationException("Service not started");

            if (chunk.IsStartChunk)
            {
                PrepareForTransfer(chunk.Header.Hash);
            }

            if (!_fileBuilders.ContainsKey(chunk.Header.Hash))
            {
                return false;
            }

            FileRebuilder builder = _fileBuilders[chunk.Header.Hash];

            builder.ReceiveChunk(chunk);

            if (chunk.IsEndChunk && TransferCompleted != null)
            {
                FileTransferCompletedEventArgs args = new FileTransferCompletedEventArgs(chunk.Header.Hash, builder.FileName);

                //Close so subscribers can move file
                _fileBuilders[chunk.Header.Hash].Close();

                TransferCompleted(this, args);

                _fileBuilders[chunk.Header.Hash].Dispose();
                _fileBuilders.Remove(chunk.Header.Hash);
            }
            else
            {
                if (NextChunkRequested != null)
                {
                    FileRequestEventArgs args = new FileRequestEventArgs(null);
                    args.Hash = chunk.Header.Hash;
                    args.ChunkIndex = chunk.ChunkIndex + 1;

                    NextChunkRequested(this, args);
                }
            }

            return true;
        }
 internal void HandleFileChunk(FileChunkMessage message)
 {
     _service.FileChunkReceived(message);
 }
        private void HandleFileSend(FileRequestMessage requestMessage)
        {
            if (!requestMessage.RequestedFile.Exists)
            {
                ClientProtocol.SendFileTransferFailure(requestMessage.RequestedFile);
                return;
            }

            long chunkIndex = requestMessage.Header.ChunkIndex;

            using (FileStream file = requestMessage.RequestedFile.OpenForRead())
            {
                FileChunkHeader header = new FileChunkHeader();

                header.ChunkCount = (int)Math.Ceiling((double)file.Length / Settings.FileChunkSize);
                header.ChunkIndex = chunkIndex;
                header.Id = requestMessage.Header.Id;
                header.Hash = requestMessage.RequestedFile.Hash;

                using (FileChunkMessage message = new FileChunkMessage(header))
                {
                    long offset = chunkIndex * Settings.FileChunkSize;
                    int length = (int) (offset + Settings.FileChunkSize > file.Length ? file.Length - offset : Settings.FileChunkSize);

                    byte[] chunk = new byte[length];

                    file.Seek(offset, SeekOrigin.Begin);

                    file.Read(chunk, 0, chunk.Length);

                    try
                    {
                        ClientProtocol.AddDataToMessage(message, chunk);

                        ClientProtocol.SendMessage(message);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }