Пример #1
0
        public IdpPacket ProcessPayload(UInt16 nodeAddress, IdpPacket packet)
        {
            var incoming = new IncomingTransaction(packet);
            var outgoing = new OutgoingTransaction(0xA000, incoming.TransactionId, IdpCommandFlags.None);

            if (_commandHandlers.ContainsKey(incoming.CommandId))
            {
                outgoing.Write((byte)IdpResponseCode.OK);
                outgoing.Write(incoming.CommandId);

                var responseCode = _commandHandlers[incoming.CommandId](incoming, outgoing);

                if (incoming.Flags.HasFlag(IdpCommandFlags.ResponseExpected))
                {
                    outgoing.WithResponseCode(responseCode);

                    return(outgoing.ToPacket(nodeAddress, packet.Source));
                }
            }
            else
            {
                outgoing.Write((byte)IdpResponseCode.UnknownCommand);
                outgoing.Write(incoming.CommandId);

                return(outgoing.ToPacket(nodeAddress, packet.Source));
            }

            return(null);
        }
        public IdpPacket ToPacket(UInt16 source, UInt16 destination)
        {
            var result = new IdpPacket((uint)_data.Count, IdpFlags.None, source, destination);

            result.Write(_data.ToArray());
            result.Seal();

            return(result);
        }
Пример #3
0
        internal IncomingTransaction(IdpPacket packet)
        {
            _packet = packet;

            Data       = packet.Data;
            _readIndex = 10;

            CommandId     = Read <UInt16>();
            TransactionId = Read <UInt32>();
            Flags         = Read <IdpCommandFlags>();

            Source      = packet.Source;
            Destination = packet.Destination;
        }
Пример #4
0
        public bool Transmit(ushort adaptorId, IdpPacket packet)
        {
            var source = packet.Source;

            if (source != UnassignedAddress && adaptorId != 0xFFFF)
            {
                //if (!_adaptors[adaptorId].IsEnumerated)
                {
                    _lastAdaptorId = adaptorId;
                }

                if (!(source == 1 && _routingTable.ContainsKey(source)))
                {
                    _routingTable[source] = adaptorId;
                }
            }

            return(Route(packet));
        }
Пример #5
0
        public override bool Transmit(IdpPacket packet)
        {
            if (!_isDisposing && OutputStream != null)
            {
                try
                {
                    OutputStream.Write(packet.Data, 0, packet.Data.Length);
                    OutputStream.Flush();
                }
                catch (Exception e)
                {
                    ConnectionError?.Invoke(this, EventArgs.Empty);
                    return(false);
                }


                return(true);
            }

            return(false);
        }
Пример #6
0
 public bool Transmit(IdpPacket packet)
 {
     return(Transmit(0xFFFF, packet));
 }
Пример #7
0
        public bool Route(IdpPacket packet)
        {
            var destination = packet.Destination;
            var source      = packet.Source;

            packet.ResetReadToPayload();
            var command = packet.Read <UInt16>();

            packet.ResetRead();

            //Debug.WriteLine($"Router: 0x{Address.ToString("X4")} from: 0x{source.ToString("X4")}, to: 0x{ destination.ToString("X4")} Command: 0x{ command.ToString("X4")} ({(NodeCommand)command})");

            if (destination == 0 && Address != UnassignedAddress)
            {
                foreach (var adaptor in _adaptors.Values)
                {
                    if (!_routingTable.ContainsKey(source) || adaptor != _adaptors[_routingTable[source]])
                    {
                        packet.ResetRead();

                        adaptor.Transmit(packet);
                    }
                }

                foreach (var node in _enumeratedNodes.Values)
                {
                    packet.ResetRead();

                    var packetResponse = node.ProcessPacket(packet);

                    if (packetResponse != null)
                    {
                        Route(packetResponse);
                    }
                }


                packet.ResetRead();

                var response = ProcessPacket(packet);

                if (response != null)
                {
                    Route(response);
                }

                return(true);
            }
            else if (destination == RouterPollAddress && Address != UnassignedAddress)
            {
                var response = ProcessPacket(packet);

                if (response != null)
                {
                    return(Route(response));
                }

                return(false);
            }
            else
            {
                IdpNode node = null;

                if (destination == Address)
                {
                    node = this;
                }
                else
                {
                    node = FindNode(destination);
                }

                if (node != null)
                {
                    var responsePacket = node.ProcessPacket(packet);

                    if (responsePacket != null)
                    {
                        return(Route(responsePacket));
                    }

                    return(true);
                }
                else
                {
                    if (_routingTable.ContainsKey(destination))
                    {
                        return(_adaptors[_routingTable[destination]].Transmit(packet));
                    }
                    else
                    {
                        if (destination == 0xFFFF && Address != 0xFFFF)
                        {
                            return(false);
                        }

                        return(_adaptors.FirstOrDefault().Value?.Transmit(packet) ?? false);
                    }
                }
            }
        }
 public PacketParsedEventArgs(IdpPacket packet)
 {
     Packet = packet;
 }
Пример #9
0
 public static void SendPacket(this Stream stream, IdpPacket packet)
 {
     stream.Write(packet.Data, 0, packet.Data.Length);
 }