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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
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;
using Tango.PMR.Integration;
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;
private bool taskCompleted;
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;
Action setResultAction = () =>
{
try
{
if (!IsContinuous)
{
if (!taskCompleted)
{
taskCompleted = true;
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.");
}
}
};
if (ThreadingMode == TransportThreadingMode.NewThread)
{
ThreadFactory.StartNew(() =>
{
setResultAction();
});
}
else
{
Task.Factory.StartNew(() =>
{
setResultAction();
});
}
}
/// <summary>
/// Notifies the message observer of an exception.
/// </summary>
/// <param name="ex">The ex.</param>
public override void SetException(Exception ex)
{
if (exceptionRaised || taskCompleted)
{
return;
}
Completed = true;
exceptionRaised = true;
Action setExceptionAction = () =>
{
if (!IsContinuous)
{
if (!taskCompleted)
{
taskCompleted = true;
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);
}
};
if (ThreadingMode == TransportThreadingMode.NewThread)
{
ThreadFactory.StartNew(() =>
{
setExceptionAction();
});
}
else
{
Task.Factory.StartNew(() =>
{
setExceptionAction();
});
}
}
public override string GetActualMessageTypeName()
{
return GetActualMessageTypeName(Message);
}
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;
}
}
}
|