Beispiel #1
0
 public AHProtocol(ushort UID, int timeOut, MessageArriveDelegate messageArrive, IPhysicalProtocolCouple physicalCouple)
 {
     _UID = UID;
     TimeOut = timeOut;
     MessageArrive = messageArrive;
     _messageID = 0;
     _messageStack = new List<MessageStore>();
     _messageQueue = new List<MessageStore>();
     _messageWaiting = new List<MessageStore>();
     _physicalCouple = physicalCouple;
     _physicalCouple.Receiver += Receiver;
     Task.Run(new Action(RunSenderStack));
 }
Beispiel #2
0
        public void Send(ushort receiverUID, byte[] messageBody, MessageArriveDelegate callBack = null)
        {
            var messsageID = _messageID++;

            var configuration = MessageConfigurationEnum.None;
            if (callBack != null)
                configuration |= MessageConfigurationEnum.NeedConfirmation;

            var package = new MessagePackage(_UID, receiverUID, messsageID, configuration, messageBody);

            var messageStore = new MessageStore
            {
                Package = package,
                Callback = callBack
            };

            var time = new Timer(WaitingTimeoutCallback, messageStore, TimeOut, Timeout.Infinite);
            messageStore.TimeoutTimer = time;

            lock (_messageskLock)
            {
                var hasInStack = _messageStack
                    .Where(ms => ms.Package.ReceiverUID == receiverUID);

                if (hasInStack.Any())
                {
                    _messageQueue.Add(messageStore);
                }
                else
                {
                    _messageStack.Add(messageStore);
                }

                Monitor.Pulse(_messageskLock);
            }
        }