using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using System.Linq; using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using Google.Protobuf; using Tango.Core; using Tango.Core.ExtensionMethods; using Tango.FSE.Procedures; using Tango.PMR; using Tango.Transport; namespace Tango.StubsUtils.Service { public class StubsServiceProcedureContext : ProcedureContext { private ITransporter _transporter; public StubsServiceProcedureContext(ProcedureProject project, ITransporter transporter, IProcedureLogger logger) : base(project, logger) { _transporter = transporter; } public override IMessage Send(IMessage message, int? timeout = null) { TimeSpan? timespan = null; if (timeout != null) { timespan = TimeSpan.FromMilliseconds(timeout.Value); } return _transporter.SendRequest(message, new TransportRequestConfig() { Timeout = timespan, }).Result; } public override void SendContinuous(IMessage message, Action callback, int? timeout = null) { TaskCompletionSource completion = new TaskCompletionSource(); TimeSpan? timespan = null; if (timeout != null) { timespan = TimeSpan.FromMilliseconds(timeout.Value); } _transporter.SendContinuousRequest(message, new TransportContinuousRequestConfig() { Timeout = timespan, ContinuousTimeout = timespan }).ObserveOn(new NewThreadScheduler()).Subscribe((msg) => { try { callback?.Invoke(msg as T); } catch { } }, (ex) => { completion.SetException(ex); }, () => { completion.SetResult(true); }); completion.Task.GetAwaiter().GetResult(); } } }