blob: 1f2013fde29cb7b272f4d68b1d9d2c64ebf8df5f (
plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;
namespace Tango.Transport
{
/// <summary>
/// Represents an <see cref="ITransportAdapter"/> base class.
/// </summary>
/// <seealso cref="Tango.Transport.ITransportAdapter" />
public abstract class TransportAdapterBase : ITransportAdapter
{
protected const int MAX_BUFFER_SIZE = 1024; //10 MB.
private LogManager LogManager = LogManager.Default;
#region Events
/// <summary>
/// Occurs when component state changes.
/// </summary>
public event EventHandler<TransportComponentState> StateChanged;
/// <summary>
/// Occurs when new data is available.
/// </summary>
public event EventHandler<byte[]> DataAvailable;
#endregion
#region Properties
private long _totalBytesReceived;
/// <summary>
/// Gets the total bytes received.
/// </summary>
public long TotalBytesReceived
{
get { return _totalBytesReceived; }
protected set { _totalBytesReceived = value; }
}
private long _totalBytesSent;
/// <summary>
/// Gets the total bytes sent.
/// </summary>
public long TotalBytesSent
{
get { return _totalBytesSent; }
protected set { _totalBytesSent = value; }
}
/// <summary>
/// Gets or sets the channel address.
/// </summary>
public String Address { get; set; }
private TransportComponentState _state;
/// <summary>
/// Gets the component state.
/// </summary>
public TransportComponentState State
{
get { return _state; }
protected set
{
_state = value;
OnStateChanged(_state);
}
}
/// <summary>
/// Gets or sets the adapter reading/writing mode.
/// </summary>
public TransportAdapterMode AdapterMode { get; set; }
#endregion
#region Virtual Methods
/// <summary>
/// Called when the adapter has failed.
/// </summary>
/// <param name="ex">The ex.</param>
protected virtual void OnFailed(Exception ex)
{
Disconnect().Wait();
State = TransportComponentState.Failed;
LogManager.Log(ex, "Adapter failed.");
}
/// <summary>
/// Called when there is new data available.
/// </summary>
/// <param name="data">The data.</param>
protected virtual void OnDataAvailable(byte[] data)
{
TotalBytesReceived += data.Length;
DataAvailable?.Invoke(this, data);
}
/// <summary>
/// Called when the adapter state has changed.
/// </summary>
/// <param name="state">The state.</param>
protected virtual void OnStateChanged(TransportComponentState state)
{
StateChanged?.Invoke(this, state);
}
/// <summary>
/// Throws an exception if adapter is in a failed or disposed state.
/// </summary>
protected virtual void ThrowIfDisposed()
{
if (State == TransportComponentState.Disposed)
{
throw LogManager.Log(new ObjectDisposedException("The adapter is in a " + State + " state."));
}
}
/// <summary>
/// Applies any additional headers if required.
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
protected virtual byte[] PostProcessBuffer(byte[] data)
{
byte[] postData = data;
if (AdapterMode == TransportAdapterMode.NO_HEADER)
{
return postData;
}
else
{
postData = BitConverter.GetBytes(data.Length).Concat(data).ToArray();
}
TotalBytesSent += postData.Length;
return postData;
}
#endregion
#region Dispose
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
Disconnect().Wait();
State = TransportComponentState.Disposed;
}
#endregion
#region Abstract Methods
/// <summary>
/// Writes the specified data to the stream.
/// </summary>
/// <param name="data">The data.</param>
public abstract void Write(byte[] data);
/// <summary>
/// Connects the transport component.
/// </summary>
/// <returns></returns>
public abstract Task Connect();
/// <summary>
/// Disconnects the transport component.
/// </summary>
/// <returns></returns>
public abstract Task Disconnect();
#endregion
#region Override Methods
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
return this.GetType().Name + " [" + AdapterMode + "]";
}
#endregion
}
}
|