Mercurial > hg > ucis.core
annotate USBLib/Communication/UsbPipeStream.cs @ 102:1474f92cf7e7
Regression fix in ThreadPool class
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 01 Oct 2014 23:16:06 +0200 |
parents | 2d16447eff12 |
children |
rev | line source |
---|---|
21 | 1 ???using System; |
2 using System.IO; | |
3 | |
4 namespace UCIS.USBLib.Communication { | |
5 public class UsbPipeStream : Stream { | |
6 public IUsbInterface Device { get; private set; } | |
7 public Byte Endpoint { get; private set; } | |
8 | |
67
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
9 public UsbPipeStream(IUsbInterface device, Byte endpoint) { |
21 | 10 this.Device = device; |
11 this.Endpoint = endpoint; | |
12 } | |
13 | |
14 public override bool CanRead { | |
15 get { return (Endpoint & 0x80) != 0; } | |
16 } | |
17 | |
18 public override bool CanSeek { | |
19 get { return false; } | |
20 } | |
21 | |
22 public override bool CanWrite { | |
23 get { return (Endpoint & 0x80) == 0; } | |
24 } | |
25 | |
26 public override void Flush() { | |
27 } | |
28 | |
29 public override long Length { get { return 0; } } | |
30 | |
31 public override long Position { | |
32 get { return 0; } | |
33 set { throw new NotImplementedException(); } | |
34 } | |
35 | |
67
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
36 public void Abort() { |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
37 Device.PipeAbort(Endpoint); |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
38 } |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
39 |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
40 public void ClearHalt() { |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
41 Device.PipeReset(Endpoint); |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
42 } |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
43 |
21 | 44 public override int Read(byte[] buffer, int offset, int count) { |
67
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
45 if (!CanRead) throw new InvalidOperationException("Can not read from an output endpoint"); |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
46 return Device.PipeTransfer(Endpoint, buffer, offset, count); |
21 | 47 } |
48 | |
49 public override long Seek(long offset, SeekOrigin origin) { | |
50 throw new NotImplementedException(); | |
51 } | |
52 | |
53 public override void SetLength(long value) { | |
54 throw new NotImplementedException(); | |
55 } | |
56 | |
57 public override void Write(byte[] buffer, int offset, int count) { | |
67
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
58 if (!CanWrite) throw new InvalidOperationException("Can not write to an input endpoint"); |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
59 int written = Device.PipeTransfer(Endpoint, buffer, offset, count); |
21 | 60 if (written != count) throw new EndOfStreamException("Could not write all data"); |
61 } | |
67
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
62 |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
63 protected override void Dispose(bool disposing) { |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
64 if (disposing) try { Abort(); } catch { } |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
65 base.Dispose(disposing); |
2d16447eff12
Simplified USB communication code, added functions to abort pipe transfers
Ivo Smits <Ivo@UCIS.nl>
parents:
21
diff
changeset
|
66 } |
21 | 67 } |
68 } |