BACnet.Ethernet.EthernetPort.SendNetgram C# (CSharp) Method

SendNetgram() public method

Sends a netgram out of this port
public SendNetgram ( OutboundNetgram netgram ) : void
netgram BACnet.Core.Datalink.OutboundNetgram The netgram to send
return void
        public void SendNetgram(OutboundNetgram netgram)
        {
            Contract.Requires(_deviceMac.Length == 6);
            Contract.Requires(netgram.Destination.IsBroadcast() || netgram.Destination.Length == 6);

            byte[] buffer = new byte[1500];
            int lengthOffset = 0;
            int offset = 0;
            int length;
            var destination = netgram.Destination.IsBroadcast() ? _ethernetBroadcastMac : netgram.Destination;

            // write the destination mac address bytes
            for(int i = 0; i < 6; i++)
            {
                buffer[offset++] = destination[i];
            }

            // write the source mac address bytes
            for(int i = 0; i < 6; i++)
            {
                buffer[offset++] = _deviceMac[i];
            }

            // the next 2 bytes are used for the packet length, so
            // we skip them until we know what they are
            lengthOffset = offset;
            offset += 2;

            // DSAP and SSAP
            buffer[offset++] = 0x82;
            buffer[offset++] = 0x82;

            // LLC control
            buffer[offset++] = 0x03;

            // serialize the netgram content
            offset = netgram.Content.Serialize(buffer, offset);

            // now that we have the full packet length, we backfill
            // the length field
            length = offset - lengthOffset - 2;
            buffer.WriteUInt16(lengthOffset, (ushort)length);

            lock(_device)
            {
                _device.SendPacket(buffer, offset);
            }
        }