Esempio n. 1
0
 void Handler(ICommunicationElement <TAccessPoint, ICommunicationElementParameters> connection, TAccessPoint remote, ReadOnlyMemory <byte> message)
 {
     if (check(remote, message, out result))
     {
         success = true;
         resetEvent.Set();
     }
 }
Esempio n. 2
0
        public static async Task <ReadOnlyMemory <byte> > WaitAnswer <TAccessPoint>(this ICommunicationElement <TAccessPoint, ICommunicationElementParameters> element, Func <TAccessPoint, ReadOnlyMemory <byte>, bool> check, TimeSpan?span = null)
            where TAccessPoint : class, IAccessPoint
        {
            await Task.Yield();

            ManualResetEventSlim  resetEvent = new(false);
            ReadOnlyMemory <byte> result     = null;
            IConnection <TAccessPoint, ICommunicationElementParameters>?connection = element as IConnection <TAccessPoint, ICommunicationElementParameters>;

            void Handler(ICommunicationElement <TAccessPoint, ICommunicationElementParameters> connection, TAccessPoint remote, ReadOnlyMemory <byte> message)
            {
                if (check(remote, message))
                {
                    result = message;
                    resetEvent.Set();
                }
            }

            void Interrupted(IConnection <TAccessPoint, ICommunicationElementParameters> connection)
            {
                resetEvent.Set();
            }

            element.MessageReceived += Handler;
            if (connection != null)
            {
                connection.ConnectionInterrupted += Interrupted;
            }
            if (span == null)
            {
                resetEvent.Wait();
            }
            else
            {
                resetEvent.Wait(span.Value);
            }
            element.MessageReceived -= Handler;
            if (connection != null)
            {
                connection.ConnectionInterrupted -= Interrupted;
            }
            return(result);
        }
Esempio n. 3
0
 public static ValueTask Send <TAccessPoint>(this ICommunicationElement <TAccessPoint, ICommunicationElementParameters> element, TAccessPoint remote, ReadOnlyMemory <byte> data)
     where TAccessPoint : class, IAccessPoint
 {
     return(element.Send(remote, data));
 }
Esempio n. 4
0
 public static async Task <(T?Value, bool Success)> WaitAnswer <TAccessPoint, T>(this ICommunicationElement <TAccessPoint, ICommunicationElementParameters> element, ComputationalPredicate <TAccessPoint, ReadOnlyMemory <byte>, T> check, TimeSpan?span)
 public static TCommunicationElement Create <TAccessPoint, TCommunicationElement, TParameters>(this ICommunicationElement <TAccessPoint, TParameters> .ICommunicationElementProvider <TCommunicationElement> provider, TAccessPoint?accessPoint = default, TParameters?parameters = default)
     where TParameters : class, ICommunicationElementParameters
     where TAccessPoint : class, IAccessPoint
     where TCommunicationElement : class, ICommunicationElement <TAccessPoint, TParameters>
 {
     return(provider.Create(accessPoint, parameters));
 }