wasSharpNET – Diff between revs 1 and 22

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