wasSharpNET – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.IO;
9 using System.Threading;
10  
11 namespace wasSharpNET.IO
12 {
13 /// <summary>
14 /// This is a wrapper aroung a FileStream. While it is not a Stream itself, it can be cast to
15 /// one (keep in mind that this might throw an exception).
16 /// </summary>
17 public class SafeFileStream : IDisposable
18 {
19 #region Constructors
20  
21 public SafeFileStream(string path, FileMode mode, FileAccess access, FileShare share, uint milliscondsTimeout)
22 {
27 office 23 m_mutex = new Mutex(false, $"Global\\{path.Replace('\\', '/')}");
1 office 24 m_path = path;
25 m_fileMode = mode;
26 m_fileAccess = access;
27 m_fileShare = share;
28 m_millisecondsTimeout = milliscondsTimeout;
29 }
30  
31 #endregion//Constructors
32  
33 #region Private Members
34  
35 private readonly Mutex m_mutex;
36 private Stream m_stream;
37 private readonly string m_path;
38 private readonly FileMode m_fileMode;
39 private readonly FileAccess m_fileAccess;
40 private readonly FileShare m_fileShare;
41 private readonly uint m_millisecondsTimeout;
42  
43 #endregion//Private Members
44  
45 #region Properties
46  
47 public Stream Stream
48 {
49 get
50 {
51 if (!IsOpen && !TryOpen(TimeSpan.FromMilliseconds(m_millisecondsTimeout)))
52 throw new InvalidOperationException("Timeout opening stream.");
53 return m_stream;
54 }
55 }
56  
57 private bool IsOpen => m_stream != null;
58  
59 #endregion//Properties
60  
61 #region Functions
62  
63 /// <summary>
64 /// Opens the stream when it is not locked. If the file is locked, then
65 /// </summary>
66 public void Open()
67 {
68 if (m_stream != null)
69 throw new InvalidOperationException("The stream is already open.");
70 m_mutex.WaitOne();
71 m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare);
72 }
73  
74 public bool TryOpen(TimeSpan span)
75 {
76 if (m_stream != null)
77 throw new InvalidOperationException("The stream is already open.");
78 if (m_mutex.WaitOne(span))
79 {
80 m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare);
81 return true;
82 }
83 return false;
84 }
85  
86 public void Close()
87 {
88 if (m_stream == null)
89 return;
90 m_stream.Close();
91 m_stream = null;
92 m_mutex.ReleaseMutex();
93 }
94  
95 public void Dispose()
96 {
97 Close();
98 GC.SuppressFinalize(this);
99 }
100  
101 public static implicit operator Stream(SafeFileStream sfs)
102 {
103 return sfs.Stream;
104 }
105  
106 #endregion//Functions
107 }
108 }