0
|
1 ???using System; |
|
2 using System.IO; |
|
3 |
|
4 namespace UCIS.Util { |
|
5 public class AsyncStreamWrapper : Stream { |
|
6 ReadDelegate readDelegate; |
|
7 WriteDelegate writeDelegate; |
|
8 |
|
9 public Stream BaseStream { get; private set; } |
|
10 |
|
11 public AsyncStreamWrapper(Stream stream) { |
|
12 BaseStream = stream; |
|
13 readDelegate = (ReadDelegate)Read; |
|
14 writeDelegate = (WriteDelegate)Write; |
|
15 } |
|
16 |
|
17 public override int Read(byte[] buffer, int offset, int count) { return BaseStream.Read(buffer, offset, count); } |
|
18 public override void Write(byte[] buffer, int offset, int count) { BaseStream.Write(buffer, offset, count); } |
|
19 public override void Close() { BaseStream.Close(); } |
|
20 |
|
21 public override bool CanRead { get { return BaseStream.CanRead; } } |
|
22 public override bool CanSeek { get { return BaseStream.CanSeek; } } |
|
23 public override bool CanTimeout { get { return BaseStream.CanTimeout ; } } |
|
24 public override bool CanWrite { get { return BaseStream.CanWrite ; } } |
|
25 |
|
26 public override int ReadTimeout { |
|
27 get { return BaseStream.ReadTimeout; } |
|
28 set { BaseStream.ReadTimeout = value; } |
|
29 } |
|
30 public override int WriteTimeout { |
|
31 get { return BaseStream.WriteTimeout; } |
|
32 set { BaseStream.WriteTimeout = value; } |
|
33 } |
|
34 |
|
35 public override void Flush() { BaseStream.Flush(); } |
|
36 public override long Length { get { return BaseStream.Length; } } |
|
37 public override long Position { |
|
38 get { return BaseStream.Position; } |
|
39 set { BaseStream.Position = value; } |
|
40 } |
|
41 public override long Seek(long offset, SeekOrigin origin) { return BaseStream.Seek(offset, origin); } |
|
42 public override void SetLength(long value) { BaseStream.SetLength(value); } |
|
43 |
|
44 public override int ReadByte() { return base.ReadByte(); } |
|
45 public override void WriteByte(byte value) { BaseStream.WriteByte(value); } |
|
46 |
|
47 private delegate int ReadDelegate(byte[] buffer, int offset, int count); |
|
48 private delegate void WriteDelegate(byte[] buffer, int offset, int count); |
|
49 |
|
50 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { |
|
51 return readDelegate.BeginInvoke(buffer, offset, count, callback, state); |
|
52 } |
|
53 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { |
|
54 return writeDelegate.BeginInvoke(buffer, offset, count, callback, state); |
|
55 } |
|
56 public override int EndRead(IAsyncResult asyncResult) { |
|
57 return readDelegate.EndInvoke(asyncResult); |
|
58 } |
|
59 public override void EndWrite(IAsyncResult asyncResult) { |
|
60 writeDelegate.EndInvoke(asyncResult); |
|
61 } |
|
62 } |
|
63 public abstract class AsyncStream : Stream { |
|
64 ReadDelegate readDelegate; |
|
65 WriteDelegate writeDelegate; |
|
66 |
|
67 public AsyncStream() { |
|
68 readDelegate = (ReadDelegate)Read; |
|
69 writeDelegate = (WriteDelegate)Write; |
|
70 } |
|
71 |
|
72 private delegate int ReadDelegate(byte[] buffer, int offset, int count); |
|
73 private delegate void WriteDelegate(byte[] buffer, int offset, int count); |
|
74 |
|
75 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { |
|
76 return readDelegate.BeginInvoke(buffer, offset, count, callback, state); |
|
77 } |
|
78 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { |
|
79 return writeDelegate.BeginInvoke(buffer, offset, count, callback, state); |
|
80 } |
|
81 public override int EndRead(IAsyncResult asyncResult) { |
|
82 return readDelegate.EndInvoke(asyncResult); |
|
83 } |
|
84 public override void EndWrite(IAsyncResult asyncResult) { |
|
85 writeDelegate.EndInvoke(asyncResult); |
|
86 } |
|
87 } |
|
88 } |