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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
23 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 public override bool CanRead { get { return true; } }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 public override bool CanWrite { get { return true; } }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 }