Mercurial > hg > ucis.core
annotate Util/CrossStream.cs @ 49:7e4dae99f919
USBLib: fix unhandled exception in WinUSB shutdown after failure to open device
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 19 Aug 2013 16:56:50 +0200 |
parents | 28dc7d535036 |
children |
rev | line source |
---|---|
0 | 1 ???using System; |
2 | |
3 namespace UCIS.Util { | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
4 public class CrossStream : QueuedPacketStream { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
5 protected CrossStream otherPart; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
6 private Boolean otherClosed = false; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
7 |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
8 public CrossStream OtherSide { get { return otherPart; } } |
0 | 9 |
10 public static CrossStream CreatePair(out CrossStream stream1, out CrossStream stream2) { | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
11 return stream1 = CreatePair(out stream2); |
0 | 12 } |
13 public static CrossStream CreatePair(out CrossStream stream2) { | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
14 stream2 = new CrossStream(); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
15 return stream2.otherPart; |
0 | 16 } |
17 | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
18 public CrossStream() { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
19 otherPart = new CrossStream(this); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
20 } |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
21 protected CrossStream(CrossStream other) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
22 otherPart = other; |
0 | 23 } |
24 | |
25 public override bool CanRead { get { return true; } } | |
26 public override bool CanWrite { get { return true; } } | |
27 public override void Flush() { } | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
28 |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
29 public override void Write(byte[] buffer, int offset, int count) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
30 if (otherClosed) throw new ObjectDisposedException("CrossStream", "The stream has been closed"); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
31 otherPart.AddReadBufferCopy(buffer, offset, count); |
0 | 32 } |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
33 |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
34 public override void Close() { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
35 CloseBase(); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
36 otherPart.CloseBase(); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
37 } |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
38 private void CloseBase() { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
39 otherClosed = true; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
40 otherPart = null; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
41 base.Close(); |
0 | 42 } |
43 } | |
44 } |