/// <summary>
        /// Get underline protocol message.
        /// </summary>
        /// <returns>If exist message return it ,or reutrn null.</returns>
        public KeyValuePair <int, object> GetMessagePair()
        {
            KeyValuePair <int, object> msgPair = new KeyValuePair <int, object>(0, null);

            lock (RecvQueue)
            {
                if (RecvQueue.Count > 0)
                {
                    var vMsg = RecvQueue.Dequeue();
                    return(vMsg);
                }
            }
            return(msgPair);
        }
Beispiel #2
0
        Task recvProcess() => Task.Run(async() =>
        {
            var knownPackets = new List <Tuple <ErebusAddress, long> >();
            Log.RecordEvent(this, $"Receive process started for instance at {Address}.", LogEntrySeverity.Info);
            while (process)
            {
                try
                {
                    if (RecvTSM.RunSafe(() =>
                    {
                        if (RecvQueue.Count > 0)
                        {
                            var p = RecvQueue.Dequeue();
                            Log.RecordEvent(this, $"Received packet {p.Identifier} from {p.Source} to {p.Destination} on port {p.Port} ({p.Data.Length} bytes)", LogEntrySeverity.Info);
                            if (knownPackets.Contains(Tuple.Create(p.Source, p.Identifier)))
                            {
                                return(false);
                            }
                            if (p.Destination == Address || p.Destination == ErebusAddress.Broadcast)
                            {
                                switch (p.Port >> 14)
                                {
                                case 0:
                                    Task.Run(() => RawProtocolBase.HandlePacket(p, this));
                                    break;

                                case 1:
                                    Task.Run(() => BCPProtocolBase.HandlePacket(p, this));
                                    break;
                                }
                            }
                            if (p.Destination != Address)
                            {
                                SendTSM.RunSafe(() => SendQueue.Enqueue(p));
                            }
                            knownPackets.Add(Tuple.Create(p.Source, p.Identifier));
                            return(false);
                        }
                        return(true);
                    }))
                    {
                        await Task.Delay(5);
                    }
                }
                catch (Exception e)
                {
                    Log.RecordEvent(this, $"Exception in main instance receive thread: {e.Message}", LogEntrySeverity.Warning);
                }
            }
        });