Author: alanmc
Date: 2008-01-11 19:40:22 -0500 (Fri, 11 Jan 2008)
New Revision: 92702
Added:
trunk/bitsharp/src/MonoTorrent/Client/FileStreamBuffer.cs
Modified:
trunk/bitsharp/src/MonoTorrent/Client/Managers/DiskManager.cs
trunk/bitsharp/src/MonoTorrent/Makefile
Log:
Added filestream buffering - Can now keep a user settable amount of files open
at the same time.
Added: trunk/bitsharp/src/MonoTorrent/Client/FileStreamBuffer.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/FileStreamBuffer.cs 2008-01-12
00:37:21 UTC (rev 92701)
+++ trunk/bitsharp/src/MonoTorrent/Client/FileStreamBuffer.cs 2008-01-12
00:40:22 UTC (rev 92702)
@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using MonoTorrent.Common;
+using System.IO;
+
+namespace MonoTorrent.Client
+{
+ class FileStreamBuffer : IDisposable
+ {
+ // A list of currently open filestreams. Note: The least recently used
is at position 0
+ // The most recently used is at the last position in the array
+ private List<TorrentFileStream> list;
+ private int maxStreams;
+ public FileStreamBuffer(int maxStreams)
+ {
+ this.maxStreams = maxStreams;
+ list = new List<TorrentFileStream>(maxStreams);
+ }
+
+ private void Add(TorrentFileStream stream)
+ {
+ Console.WriteLine("Opening filestream: {0}", stream.File.Path);
+
+ // If we have our maximum number of streams open, just dispose and
dump the least recently used one
+ if (maxStreams != 0 && list.Count >= list.Capacity)
+ {
+ Console.WriteLine("We've reached capacity: {0}", list.Count);
+ CloseAndRemove(list[0]);
+ }
+ list.Add(stream);
+ }
+
+ internal TorrentFileStream GetStream(TorrentFile file, string
filePath, FileAccess access)
+ {
+ TorrentFileStream s = list.Find(delegate(TorrentFileStream stream)
{
+ return stream.File == file;
+ });
+
+ if (s != null)
+ {
+ // If we are requesting write access and the current stream
does not have it
+ if (((access & FileAccess.Write) == FileAccess.Write) &&
!s.CanWrite)
+ {
+ Console.WriteLine("Didn't have write permission -
reopening");
+ CloseAndRemove(s);
+ s = null;
+ }
+ else
+ {
+ // Place the filestream at the end so we know it's been
recently used
+ list.Remove(s);
+ list.Add(s);
+ }
+ }
+
+ if (s == null)
+ {
+ s = new TorrentFileStream(file, filePath,
FileMode.OpenOrCreate, access, FileShare.Read);
+ Add(s);
+ }
+
+ return s;
+ }
+
+ #region IDisposable Members
+
+ public void Dispose()
+ {
+ foreach (Stream s in list)
+ s.Dispose();
+ }
+
+ #endregion
+
+ internal bool CloseStream(TorrentFile file)
+ {
+ TorrentFileStream s = list.Find(delegate(TorrentFileStream stream)
{ return stream.File == file; });
+ if (s != null)
+ CloseAndRemove(s);
+
+ return s != null;
+ }
+
+ private void CloseAndRemove(TorrentFileStream s)
+ {
+ Console.WriteLine("Closing and removing: {0}", s.File.Path);
+ int index = list.FindIndex(delegate(TorrentFileStream stream) {
+ return stream == s;
+ });
+
+ list[index].Dispose();
+ list.RemoveAt(index);
+ }
+ }
+}
Property changes on: trunk/bitsharp/src/MonoTorrent/Client/FileStreamBuffer.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/bitsharp/src/MonoTorrent/Client/Managers/DiskManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/Managers/DiskManager.cs
2008-01-12 00:37:21 UTC (rev 92701)
+++ trunk/bitsharp/src/MonoTorrent/Client/Managers/DiskManager.cs
2008-01-12 00:40:22 UTC (rev 92702)
@@ -19,9 +19,9 @@
private ConnectionMonitor monitor;
internal RateLimiter rateLimiter;
+ private FileStreamBuffer streamsBuffer;
private int openStreams;
- //private Dictionary<TorrentManager, List<TorrentFileStream>> streams;
#endregion Member Variables
@@ -89,6 +89,7 @@
this.monitor = new ConnectionMonitor();
this.queueLock = new object();
this.rateLimiter = new RateLimiter();
+ this.streamsBuffer = new
FileStreamBuffer(engine.Settings.MaxOpenFiles);
this.streamsLock = new ReaderWriterLock();
this.threadWait = new ManualResetEvent(false);
this.ioThread.Start();
@@ -99,55 +100,25 @@
#region Methods
- /// <summary>
- /// Closes all the filestreams
- /// </summary>
- internal void CloseFileStreams()
+ internal void CloseFileStreams(TorrentManager manager)
{
- // Setting this boolean true allows the IO thread to terminate
gracefully
- this.ioActive = false;
-
- // Allow the IO thread to run.
- SetHandleState(true);
- this.ioThread.Join(150);
- //FIXME: #warning Buffer the filestreams
- /*
- foreach (List<TorrentFileStream> list in streams.Values)
- foreach (TorrentFileStream stream in list)
- stream.Dispose();*/
+ foreach (TorrentFile file in manager.Torrent.Files)
+ streamsBuffer.CloseStream(file);
}
/// <summary>
- ///
- /// </summary>
- /// <param name="torrentManager"></param>
- internal void CloseFileStreams(TorrentManager torrentManager)
- {
- //FIXME: #warning Buffer the Filestreams
- return;
- }
-
-
- /// <summary>
/// Disposes all necessary objects
/// </summary>
internal void Dispose()
{
- CloseFileStreams();
+ ioActive = false;
+ threadWait.Set();
+ streamsBuffer.Dispose();
}
/// <summary>
- /// Flushes all data in the FileStreams to disk
- /// </summary>
- internal void FlushAll()
- {
- //FIXME: #warning Buffer the filestreams
- }
-
-
- /// <summary>
/// Generates the full path to the supplied TorrentFile
/// </summary>
/// <param name="file">The TorrentFile to generate the full path
to</param>
@@ -169,31 +140,13 @@
/// <summary>
- ///
- /// </summary>
- /// <param name="s"></param>
- internal void FreeStream(Stream s)
- {
- if (s != null)
- {
- s.Dispose();
- Interlocked.Decrement(ref openStreams);
- }
- }
-
-
- /// <summary>
/// Opens all the filestreams with the specified file access
/// </summary>
/// <param name="fileAccess"></param>
- internal Stream GetStream(FileManager manager, TorrentFile file,
FileAccess access)
+ internal TorrentFileStream GetStream(FileManager manager, TorrentFile
file, FileAccess access)
{
string filePath = GenerateFilePath(file, manager.BaseDirectory,
manager.SavePath);
-
- TorrentFileStream stream = new TorrentFileStream(file, filePath,
FileMode.OpenOrCreate, access, FileShare.Read);
-
- Interlocked.Increment(ref openStreams);
- return stream;
+ return streamsBuffer.GetStream(file, filePath, access);
}
@@ -334,20 +287,12 @@
lock (manager.Files[i])
{
- Stream s = null;
- try
- {
- s = GetStream(manager, manager.Files[i],
FileAccess.Read);
- s.Seek(offset, SeekOrigin.Begin);
- offset = 0; // Any further files need to be read from
the beginning
- bytesRead = s.Read(buffer, bufferOffset + totalRead,
count - totalRead);
- totalRead += bytesRead;
- i++;
- }
- finally
- {
- FreeStream(s);
- }
+ TorrentFileStream s = GetStream(manager, manager.Files[i],
FileAccess.Read);
+ s.Seek(offset, SeekOrigin.Begin);
+ offset = 0; // Any further files need to be read from the
beginning
+ bytesRead = s.Read(buffer, bufferOffset + totalRead, count
- totalRead);
+ totalRead += bytesRead;
+ i++;
}
}
monitor.BytesSent(totalRead, TransferType.Data);
@@ -448,33 +393,25 @@
{
lock (manager.Files[i])
{
- Stream stream = null;
- try
- {
- stream = GetStream(manager, manager.Files[i],
FileAccess.Write);
- stream.Seek(offset, SeekOrigin.Begin);
+ TorrentFileStream stream = GetStream(manager,
manager.Files[i], FileAccess.ReadWrite);
+ stream.Seek(offset, SeekOrigin.Begin);
- // Find the maximum number of bytes we can write
before we reach the end of the file
- bytesWeCanWrite = manager.Files[i].Length - offset;
+ // Find the maximum number of bytes we can write before we
reach the end of the file
+ bytesWeCanWrite = manager.Files[i].Length - offset;
- // Any further files need to be written from the
beginning of the file
- offset = 0;
+ // Any further files need to be written from the beginning
of the file
+ offset = 0;
- // If the amount of data we are going to write is
larger than the amount we can write, just write the allowed
- // amount and let the rest of the data be written with
the next filestream
- bytesWritten = ((count - totalWritten) >
bytesWeCanWrite) ? bytesWeCanWrite : (count - totalWritten);
+ // If the amount of data we are going to write is larger
than the amount we can write, just write the allowed
+ // amount and let the rest of the data be written with the
next filestream
+ bytesWritten = ((count - totalWritten) > bytesWeCanWrite)
? bytesWeCanWrite : (count - totalWritten);
- // Write the data
- stream.Write(buffer, bufferOffset + (int)totalWritten,
(int)bytesWritten);
+ // Write the data
+ stream.Write(buffer, bufferOffset + (int)totalWritten,
(int)bytesWritten);
- // Any further data should be written to the next
available file
- totalWritten += bytesWritten;
- i++;
- }
- finally
- {
- FreeStream(stream);
- }
+ // Any further data should be written to the next
available file
+ totalWritten += bytesWritten;
+ i++;
}
}
@@ -482,6 +419,5 @@
}
#endregion
-
}
}
Modified: trunk/bitsharp/src/MonoTorrent/Makefile
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Makefile 2008-01-12 00:37:21 UTC (rev
92701)
+++ trunk/bitsharp/src/MonoTorrent/Makefile 2008-01-12 00:40:22 UTC (rev
92702)
@@ -33,6 +33,7 @@
Client/BufferedFileRead.cs \
Client/BufferedFileWrite.cs \
Client/ClientEngine.cs \
+ Client/FileStreamBuffer.cs \
Client/Logger.cs \
Client/MessageHandler.cs \
Client/Piece.cs \
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches