Skip to content

Commit 2205b0b

Browse files
Add read and write timeout properties (#4)
***NO_CI***
1 parent 79224fb commit 2205b0b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

System.Device.UsbStream/UsbStream.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.IO;
55
using System.Runtime.CompilerServices;
6+
using System.Threading;
67

78
namespace System.Device.UsbClient
89
{
@@ -11,8 +12,13 @@ namespace System.Device.UsbClient
1112
/// </summary>
1213
public sealed class UsbStream : System.IO.Stream
1314
{
15+
#pragma warning disable IDE0052 // required at native code
1416
private readonly int _streamIndex;
17+
#pragma warning restore IDE0052 // Remove unread private members
18+
1519
private bool _disposed;
20+
private int _writeTimeout = Timeout.Infinite;
21+
private int _readTimeout = Timeout.Infinite;
1622

1723
/// <inheritdoc/>
1824
public override bool CanRead => true;
@@ -31,6 +37,40 @@ public sealed class UsbStream : System.IO.Stream
3137
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
3238
public override long Position { get => throw new PlatformNotSupportedException(); set => throw new PlatformNotSupportedException(); }
3339

40+
/// <summary>
41+
/// Gets or sets the number of milliseconds before a time-out occurs when a read operation does not finish.
42+
/// </summary>
43+
/// <exception cref="IOException">If the USB device is not connected.</exception>
44+
/// <exception cref="ArgumentOutOfRangeException">The read time-out value is less than zero and not equal to <see cref="Timeout.Infinite"/>.</exception>
45+
public override int ReadTimeout
46+
{
47+
get => _readTimeout;
48+
49+
set
50+
{
51+
CheckValidTimeout(value);
52+
53+
_readTimeout = value;
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Gets or sets the number of milliseconds before a time-out occurs when a write operation does not finish.
59+
/// </summary>
60+
/// <exception cref="IOException">If the USB device is not connected.</exception>
61+
/// <exception cref="ArgumentOutOfRangeException">The read time-out value is less than zero and not equal to <see cref="Timeout.Infinite"/>.</exception>
62+
public override int WriteTimeout
63+
{
64+
get => _writeTimeout;
65+
66+
set
67+
{
68+
CheckValidTimeout(value);
69+
70+
_writeTimeout = value;
71+
}
72+
}
73+
3474
internal UsbStream(
3575
Guid classId,
3676
string name)
@@ -120,6 +160,14 @@ public override void Write(
120160
count);
121161
}
122162

163+
private static void CheckValidTimeout(int value)
164+
{
165+
if (value < 0 && value != Timeout.Infinite)
166+
{
167+
throw new ArgumentOutOfRangeException();
168+
}
169+
}
170+
123171
#region Native Methods
124172

125173
[MethodImpl(MethodImplOptions.InternalCall)]

System.Device.UsbStream/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
<package id="nanoFramework.System.Text" version="1.2.7" targetFramework="netnano1.0" />
77
<package id="Nerdbank.GitVersioning" version="3.5.113" targetFramework="netnano1.0" developmentDependency="true" />
88
<package id="StyleCop.MSBuild" version="6.2.0" targetFramework="netnano1.0" developmentDependency="true" />
9-
</packages>
9+
</packages>

0 commit comments

Comments
 (0)