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
|
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.ExtensionMethods;
using Tango.Core.Threading;
using Tango.Logging;
using Tango.PMR;
using Tango.PMR.Common;
namespace Tango.Transport
{
/// <summary>
/// Represents a generic transport message.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <seealso cref="Tango.Transport.TransportMessageBase" />
internal class TransportMessage<T> : TransportMessageBase
{
private bool exceptionRaised;
private TaskCompletionSource<T> _completionSource;
public Subject<T> ContinuesResponseSubject { get; set; }
public DateTime LastResponseTime { get; set; }
public bool Completed { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="TransportMessage{T}"/> class.
/// </summary>
/// <param name="token">The token.</param>
/// <param name="message">The message.</param>
/// <param name="direction">The direction.</param>
/// <param name="toBytes">To bytes.</param>
/// <param name="completionSource">The completion source.</param>
public TransportMessage(string token, object message, TransportMessageDirection direction, Func<byte[]> toBytes, TaskCompletionSource<T> completionSource) : base(token, message, direction, toBytes)
{
_completionSource = completionSource;
}
/// <summary>
/// Notifies the message observer of the new result.
/// </summary>
/// <param name="result">The result.</param>
public override void SetResult(object result, bool completed)
{
Completed = completed;
ThreadFactory.StartNew(() =>
{
try
{
if (!IsContinuous)
{
if (!_completionSource.Task.IsCompleted)
{
if (_completionSource.GetType() == typeof(TaskCompletionSource<IMessage>))
{
_completionSource.SetResult((T)result.GetType().GetProperty("Message").GetValue(result));
}
else if (_completionSource.GetType() == typeof(TaskCompletionSource<MessageContainer>))
{
_completionSource.SetResult((T)result.GetType().GetProperty("Container").GetValue(result));
}
else
{
_completionSource.SetResult((T)result);
}
}
}
else
{
LastResponseTime = DateTime.Now;
AtLeastOneResponseReceived = true;
if (ContinuesResponseSubject.GetType() == typeof(Subject<IMessage>))
{
ContinuesResponseSubject.OnNext((T)result.GetType().GetProperty("Message").GetValue(result));
}
else if (ContinuesResponseSubject.GetType() == typeof(Subject<MessageContainer>))
{
ContinuesResponseSubject.OnNext((T)result.GetType().GetProperty("Container").GetValue(result));
}
else
{
ContinuesResponseSubject.OnNext((T)result);
}
if (completed)
{
ContinuesResponseSubject.OnCompleted();
}
}
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error while settings result for message.");
try
{
SetException(ex);
}
catch (Exception e)
{
LogManager.Log(e, $"Error while settings exception for message.");
}
}
});
}
/// <summary>
/// Notifies the message observer of an exception.
/// </summary>
/// <param name="ex">The ex.</param>
public override void SetException(Exception ex)
{
if (exceptionRaised)
{
return;
}
Completed = true;
exceptionRaised = true;
ThreadFactory.StartNew(() =>
{
if (!IsContinuous)
{
if (!_completionSource.Task.IsCompleted)
{
if (!(ex is ContinuousResponseAbortedException) && !(ex is TransporterDisconnectedException))
{
LogManager.Log($"{TransportComponentName}: Request failed '{GetActualMessageTypeName()}'...\n{ex.FlattenException()}", LogCategory.Error);
}
_completionSource.SetException(ex);
}
}
else
{
if (!(ex is ContinuousResponseAbortedException) && !(ex is TransporterDisconnectedException))
{
LogManager.Log($"{TransportComponentName}: Request failed '{GetActualMessageTypeName()}'...\n{ex.FlattenException()}", LogCategory.Error);
}
AtLeastOneResponseReceived = true;
ContinuesResponseSubject.OnError(ex);
}
});
}
public override string GetActualMessageTypeName()
{
String name = String.Empty;
if (Message is ITangoMessage)
{
name = Message.GetType().GetProperty("Message").GetValue(Message).GetType().Name;
}
else if (Message is MessageContainer)
{
name = (Message as MessageContainer).Type.ToString();
}
else
{
name = Message.GetType().Name;
}
return name;
}
public override object GetActualMessage()
{
object obj = null;
if (Message is ITangoMessage)
{
obj = Message.GetType().GetProperty("Message").GetValue(Message);
}
else if (Message is MessageContainer)
{
obj = Message;
}
else
{
obj = Message;
}
return obj;
}
}
}
|