public IPacket Clone() { var packet = new ChunkedDataPacket { UniqueId = UniqueId, Offset = Offset, TotalChunks = TotalChunks, Data = new byte[Data?.Length ?? 0], DataLength = DataLength }; if (Data != null) { Array.Copy(Data, packet.Data, Data.Length); } return(packet); }
/// <summary> /// Send a new file /// </summary> /// <param name="file">The file to send</param> /// <param name="sendPacket">The method that will actually send generated packets.</param> /// <example>fileTransfer.SendFile(fileStream, client.Send);</example> /// <exception cref="ArgumentException">Thrown if the <paramref name="file"/> is too large (<see cref="uint.MaxValue"/> bytes)</exception> public void SendFile(FileStream file, Action <IPacket> sendPacket) { if (file.Length > uint.MaxValue) { throw new ArgumentException("The stream is larger than 4294967295 bytes", nameof(file)); } _id++; var id = _id; var length = file.Length; file.Position = 0; var buffer = new byte[ChunkSize]; var totalChunks = (ushort)Math.Ceiling(length / (double)ChunkSize); uint offset = 0; int read; var i = 0; while ((read = file.Read(buffer, 0, ChunkSize)) > 0) { i++; Logger.Debug("Sending file chunk {chunk}/{totalChunks} ({pct})", i, totalChunks, (float)i / totalChunks); var packet = new ChunkedDataPacket { UniqueId = id, Offset = offset, TotalChunks = totalChunks, Data = buffer, DataLength = read }; sendPacket.Invoke(packet); offset += (ushort)read; } }