blob: fa365f805c7bbb788bae1910ed732c6d906e0919 (
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
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Transport.Adapters
{
/// <summary>
/// Represents an in-memory transport adapter.
/// </summary>
/// <seealso cref="Tango.Transport.TransportAdapterBase" />
public class MemoryTransportAdapter : TransportAdapterBase
{
#region Memory Transport Manager
internal static class MemoryTransportManager
{
private static List<MemoryTransportAdapter> _adapters;
static MemoryTransportManager()
{
_adapters = new List<MemoryTransportAdapter>();
}
internal static void Connect(MemoryTransportAdapter adapter)
{
if (adapter == null)
{
throw new NullReferenceException("Cannot connect null adapter.");
}
if (String.IsNullOrWhiteSpace(adapter.Address))
{
throw new InvalidOperationException("Cannot register a memory adapter with null address.");
}
if (_adapters.Where(x => x.Address == adapter.Address).Count() > 1)
{
throw new InvalidOperationException("Cannot register more than two memory adapters with the same address.");
}
if (_adapters.Contains(adapter))
{
throw new InvalidOperationException("The specified memory adapter is already registered.");
}
_adapters.Add(adapter);
}
internal static void Disconnect(MemoryTransportAdapter adapter)
{
if (adapter == null)
{
throw new NullReferenceException("Cannot disconnect null adapter.");
}
_adapters.Remove(adapter);
}
internal static void Write(MemoryTransportAdapter adapter, byte[] data)
{
Task.Factory.StartNew(() =>
{
var other_adapter = _adapters.ToList().SingleOrDefault(x => x.Address == adapter.Address && x != adapter);
if (other_adapter != null)
{
other_adapter.EmulateDataAvailable(data);
}
});
}
}
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="MemoryTransportAdapter"/> class.
/// </summary>
/// <param name="address">The address.</param>
public MemoryTransportAdapter(String address)
{
ComponentName = $"In-Memory Adapter {_component_counter++}";
Address = address;
}
/// <summary>
/// Emulates in coming data.
/// </summary>
/// <param name="data">The data.</param>
internal void EmulateDataAvailable(byte[] data)
{
OnDataAvailable(data);
}
/// <summary>
/// Writes the specified data to the stream.
/// </summary>
/// <param name="data">The data.</param>
public override void Write(byte[] data, bool immidiate = false)
{
ThrowIfDisposed();
try
{
TotalBytesSent += data.Length;
_totalBytes += data.Length;
MemoryTransportManager.Write(this, data);
}
catch (Exception ex)
{
OnFailed(LogManager.Log(ex));
}
}
/// <summary>
/// Connects the transport component.
/// </summary>
/// <returns></returns>
public override Task Connect()
{
MemoryTransportManager.Connect(this);
State = TransportComponentState.Connected;
return Task.FromResult(new object());
}
/// <summary>
/// Disconnects the transport component.
/// </summary>
/// <returns></returns>
public override Task Disconnect()
{
MemoryTransportManager.Disconnect(this);
State = TransportComponentState.Disconnected;
return Task.FromResult(new object());
}
}
}
|