-
Notifications
You must be signed in to change notification settings - Fork 585
/
ReconnectingNetworkStream.cs
147 lines (121 loc) · 4.29 KB
/
ReconnectingNetworkStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
namespace Iot.Device.Arduino
{
internal class ReconnectingNetworkStream : Stream
{
private readonly Func<Stream> _reconnect;
private readonly object _lock = new object();
private Stream? _streamImplementation;
public ReconnectingNetworkStream(Stream? underlyingStream, Func<Stream> reconnect)
{
_streamImplementation = underlyingStream;
_reconnect = reconnect;
Connect();
}
public ReconnectingNetworkStream(Func<Stream> connect)
: this(null, connect)
{
}
public override bool CanRead => SafeExecute(x => x.CanRead, false);
public override bool CanSeek => SafeExecute(x => x.CanSeek, false);
public override bool CanWrite => SafeExecute(x => x.CanWrite, false);
public override long Length => SafeExecute(x => x.Length, false);
public override long Position
{
get
{
return SafeExecute(x => x.Position, false);
}
set
{
SafeExecute(x => x.Position = value, false);
}
}
public override void Flush() => SafeExecute(x => x.Flush(), true);
public override int Read(byte[] buffer, int offset, int count) => SafeExecute(x => x.Read(buffer, offset, count), true);
public override long Seek(long offset, SeekOrigin origin) => SafeExecute(x => x.Seek(offset, origin), true);
public override void SetLength(long value) => SafeExecute(x => x.SetLength(value), false);
public override void Write(byte[] buffer, int offset, int count) => SafeExecute(x => x.Write(buffer, offset, count), false);
private void Connect()
{
if (_streamImplementation == null)
{
lock (_lock)
{
try
{
_streamImplementation = _reconnect();
}
catch (IOException)
{
// Ignore, cannot currently reconnect. So need to retry later.
}
}
}
}
private T SafeExecute<T>(Func<Stream, T> operation, bool doThrow)
where T : struct
{
try
{
Connect();
if (_streamImplementation == null)
{
if (doThrow)
{
throw new TimeoutException("Stream is disconnected. Retrying next time.");
}
return default(T);
}
return operation(_streamImplementation);
}
catch (IOException x)
{
_streamImplementation?.Dispose();
_streamImplementation = null;
if (doThrow)
{
throw new TimeoutException("Error executing operation. Retrying next time", x);
}
return default(T);
}
}
private void SafeExecute(Action<Stream> operation, bool doThrow)
{
try
{
Connect();
if (_streamImplementation == null)
{
if (doThrow)
{
throw new TimeoutException("Stream is disconnected. Retrying next time.");
}
return;
}
operation(_streamImplementation);
}
catch (IOException x)
{
_streamImplementation?.Dispose();
_streamImplementation = null;
if (doThrow)
{
throw new TimeoutException("Error executing operation. Retrying next time", x);
}
}
}
public override void Close()
{
if (_streamImplementation != null)
{
_streamImplementation.Close();
_streamImplementation = null;
}
base.Close();
}
}
}