/// <inheritdoc /> public void WriteToStream(uint chunk, byte[] fileBytes) { lock (_fileLock) { var idx = chunk - 1; RandomAccessStream.Seek(idx * Constants.FileTransferChunkSize, SeekOrigin.Begin); RandomAccessStream.Write(fileBytes); TimeSinceLastChunk = DateTime.Now; } }
/// <inheritdoc /> public IMessageDto <ProtocolMessage> GetUploadMessageDto(uint index) { var chunkId = index + 1; var startPos = index * Constants.FileTransferChunkSize; var endPos = chunkId * Constants.FileTransferChunkSize; var fileLen = RandomAccessStream.Length; if (endPos > fileLen) { endPos = (uint)fileLen; } var bufferSize = (int)(endPos - startPos); var chunk = new byte[bufferSize]; RandomAccessStream.Seek(startPos, SeekOrigin.Begin); var readTries = 0; var bytesRead = 0; while ((bytesRead += RandomAccessStream.Read(chunk, 0, bufferSize - bytesRead)) < bufferSize) { readTries++; if (readTries >= Constants.FileTransferMaxChunkReadTries) { return(null); } } var transferMessage = new TransferFileBytesRequest { ChunkBytes = ByteString.CopyFrom(chunk), ChunkId = chunkId, CorrelationFileName = CorrelationId.Id.ToByteString() }.ToProtocolMessage(PeerId); return(new MessageDto(transferMessage, RecipientId )); }