wasSharpNET – Blame information for rev 2
?pathlinks?
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.Runtime.InteropServices; |
||
10 | using System.Runtime.InteropServices.ComTypes; |
||
11 | using System.Threading; |
||
12 | |||
13 | namespace wasSharpNET.IO |
||
14 | { |
||
15 | /// <summary> |
||
16 | /// 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). |
||
18 | /// </summary> |
||
19 | public class SafeFileStream : IDisposable |
||
20 | { |
||
21 | #region Constructors |
||
22 | |||
23 | public SafeFileStream(string path, FileMode mode, FileAccess access, FileShare share, uint milliscondsTimeout) |
||
24 | { |
||
25 | m_mutex = new Mutex(false, string.Format("Global\\{0}", path.Replace('\\', '/'))); |
||
26 | m_path = path; |
||
27 | m_fileMode = mode; |
||
28 | m_fileAccess = access; |
||
29 | m_fileShare = share; |
||
30 | m_millisecondsTimeout = milliscondsTimeout; |
||
31 | } |
||
32 | |||
33 | #endregion//Constructors |
||
34 | |||
35 | #region Private Members |
||
36 | |||
37 | private readonly Mutex m_mutex; |
||
38 | private Stream m_stream; |
||
39 | private readonly string m_path; |
||
40 | private readonly FileMode m_fileMode; |
||
41 | private readonly FileAccess m_fileAccess; |
||
42 | private readonly FileShare m_fileShare; |
||
43 | private readonly uint m_millisecondsTimeout; |
||
44 | |||
45 | #endregion//Private Members |
||
46 | |||
47 | #region Properties |
||
48 | |||
49 | public Stream Stream |
||
50 | { |
||
51 | get |
||
52 | { |
||
53 | if (!IsOpen && !TryOpen(TimeSpan.FromMilliseconds(m_millisecondsTimeout))) |
||
54 | throw new InvalidOperationException("Timeout opening stream."); |
||
55 | return m_stream; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | private bool IsOpen => m_stream != null; |
||
60 | |||
61 | #endregion//Properties |
||
62 | |||
63 | #region Functions |
||
64 | |||
65 | /// <summary> |
||
66 | /// Opens the stream when it is not locked. If the file is locked, then |
||
67 | /// </summary> |
||
68 | public void Open() |
||
69 | { |
||
70 | if (m_stream != null) |
||
71 | throw new InvalidOperationException("The stream is already open."); |
||
72 | m_mutex.WaitOne(); |
||
73 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
||
74 | } |
||
75 | |||
76 | public bool TryOpen(TimeSpan span) |
||
77 | { |
||
78 | if (m_stream != null) |
||
79 | throw new InvalidOperationException("The stream is already open."); |
||
80 | if (m_mutex.WaitOne(span)) |
||
81 | { |
||
82 | m_stream = File.Open(m_path, m_fileMode, m_fileAccess, m_fileShare); |
||
83 | return true; |
||
84 | } |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | public void Close() |
||
89 | { |
||
90 | if (m_stream == null) |
||
91 | return; |
||
92 | m_stream.Close(); |
||
93 | m_stream = null; |
||
94 | m_mutex.ReleaseMutex(); |
||
95 | } |
||
96 | |||
97 | public void Dispose() |
||
98 | { |
||
99 | Close(); |
||
100 | GC.SuppressFinalize(this); |
||
101 | } |
||
102 | |||
103 | public static implicit operator Stream(SafeFileStream sfs) |
||
104 | { |
||
105 | return sfs.Stream; |
||
106 | } |
||
107 | |||
108 | #endregion//Functions |
||
109 | } |
||
110 | } |