2004-03-15 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / FileStreamAsyncResult.cs
1 //
2 // System.IO/FileStreamAsyncResult.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2004 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System.Threading;
11
12 namespace System.IO
13 {
14         class FileStreamAsyncResult : IAsyncResult {
15                 /* Same structure in the runtime */
16                 object state;
17                 bool completed;
18                 bool done;
19                 Exception exc;
20                 ManualResetEvent wh;
21                 AsyncCallback cb;
22                 bool completedSynch;
23
24                 public byte [] Buffer;
25                 public int Offset;
26                 public int Count;
27                 public int OriginalCount;
28                 public int BytesRead;
29
30                 AsyncCallback realcb;
31
32                 public FileStreamAsyncResult (AsyncCallback cb, object state)
33                 {
34                         this.state = state;
35                         this.realcb = cb;
36                         if (realcb != null)
37                                 this.cb = new AsyncCallback (CBWrapper);
38                         wh = new ManualResetEvent (false);
39                 }
40
41                 static void CBWrapper (IAsyncResult ares)
42                 {
43                         FileStreamAsyncResult res = (FileStreamAsyncResult) ares;
44                         res.realcb.BeginInvoke (ares, null, null);
45                 }
46
47                 public void SetComplete (Exception e)
48                 {
49                         exc = e;
50                         completed = true;
51                         wh.Set ();
52                 }
53                 
54                 public void SetComplete (Exception e, int nbytes)
55                 {
56                         this.BytesRead = nbytes;
57                         SetComplete (e);
58                 }
59
60                 public void SetComplete (Exception e, int nbytes, bool synch)
61                 {
62                         completedSynch = synch;
63                         SetComplete (e, nbytes);
64                 }
65
66                 public object AsyncState {
67                         get { return state; }
68                 }
69
70                 public bool CompletedSynchronously {
71                         get { return completedSynch; }
72                 }
73
74                 public WaitHandle AsyncWaitHandle {
75                         get { return wh; }
76                 }
77
78                 public bool IsCompleted {
79                         get { return completed; }
80                 }
81
82                 public Exception Exception {
83                         get { return exc; }
84                 }
85
86                 public bool Done {
87                         get { return done; }
88                         set { done = value; }
89                 }
90         }
91 }
92