Example #1
0
 protected void writeNpci(ByteStream queue, Address recipient, OctetString link, IAPDU apdu)
 {
     NPDU.NPDU npci;
     if (recipient.IsGlobal)
     {
         npci = new NPDU.NPDU((Address)null);
     }
     else if (isLocal(recipient))
     {
         if (link != null)
         {
             throw new System.Exception("Invalid arguments: link service address provided for a local recipient");
         }
         npci = new NPDU.NPDU(null, null, apdu.expectsReply);
     }
     else
     {
         if (link == null)
         {
             throw new System.Exception(
                       "Invalid arguments: link service address not provided for a remote recipient");
         }
         npci = new NPDU.NPDU(recipient, null, apdu.expectsReply);
     }
     npci.write(queue);
 }
Example #2
0
        private void OnReceived(EndPoint aFrom, byte[] data)
        {
            IPEndPoint from = (IPEndPoint)aFrom;
            ByteStream bs   = new ByteStream(data);

            BVLC.BVLC bvlc = new BVLC.BVLC(bs);

            int packetLength = bvlc.Length;

            NPDU.NPDU npdu = new NPDU.NPDU(bs);

            if (npdu.Version != 1)
            {
                throw new System.Exception("Invalid protocol version: " + npdu.Version);
            }
            if (npdu.IsNetworkMessage)
            {
                return; // throw new MessageValidationAssertionException("Network messages are not supported");
            }
            // Check the destination network number work and do not respond to foreign networks requests
            if (npdu.HasDestinationInfo)
            {
                int destNet = npdu.DestinationNetwork;
                if (destNet > 0 && destNet != 0xffff && localNetworkNumber > 0 && localNetworkNumber != destNet)
                {
                    return;
                }
            }

            Address fromAddress;

            if (npdu.HasSourceInfo)
            {
                fromAddress = new Address((ushort)npdu.SourceNetwork, npdu.SourceAddress);
            }
            else
            {
                fromAddress = new Address(new OctetString(from.Address.GetAddressBytes(), from.Port)); //macAddress
            }
            // Create the APDU.

            NewNPDUReceived?.Invoke(@from, fromAddress, fromAddress.MacAddress, npdu, bs);
        }