Exemplo n.º 1
0
        public void TestPacketSerialization()
        {
            var data      = new ArtNetData();
            var pollReply = new ArtPollReply
            {
                IP   = IPAddress.Parse("127.0.0.1").GetAddressBytes(),
                Port = 0x1936,
                Mac  = NetworkInterface.GetAllNetworkInterfaces()
                       .Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                       .Select(nic => nic.GetPhysicalAddress().GetAddressBytes())
                       .FirstOrDefault(),

                GoodInput   = new byte[] { 0x08, 0x08, 0x08, 0x08 },
                GoodOutput  = new byte[] { 0x80, 0x80, 0x80, 0x80 },
                PortTypes   = new byte[] { 0xc0, 0xc0, 0xc0, 0xc0 },
                ShortName   = "ShortName Test",
                LongName    = "Art.Net LongName Test",
                EstaManLo   = 0,
                VersInfoH   = 6,
                VersInfoL   = 9,
                SubSwitch   = 0,
                OemHi       = 0,
                Oem         = 0xFF,
                UbeaVersion = 0,
                Status1     = 0xd2,
                SwMacro     = 0,
                SwRemote    = 0,
                Style       = StyleCodes.StNode,
                NumPortsHi  = 0,
                NumPortsLo  = 4,
                Status2     = 0x08,
                BindIp      = IPAddress.Parse("127.0.0.1").GetAddressBytes(),
                SwIn        = new byte[] { 0x01, 0x02, 0x03, 0x04 },
                SwOut       = new byte[] { 0x01, 0x02, 0x03, 0x04 },
                GoodOutput2 = new byte[] { 0x80, 0x80, 0x80, 0x80 },

                NodeReport = "NodeReport Test",
                Filler     = new byte[168]
            };

            data.Buffer = pollReply.ToArray();

            var altPacket = ArtNetPacket.FromData(data);

            Assert.AreEqual(altPacket.OpCode, pollReply.OpCode);

            var pollReplyPacket = altPacket.Cast <ArtPollReply>();

            Assert.AreEqual(pollReplyPacket.ShortName, pollReply.ShortName);
            Assert.AreEqual(pollReplyPacket.LongName, pollReply.LongName);

            Assert.AreEqual(pollReplyPacket.NodeReport, pollReply.NodeReport);

            Assert.Pass();
        }
Exemplo n.º 2
0
        private void OnRecieve(IAsyncResult state)
        {
            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            if (!open)
            {
                return;
            }

            try
            {
                ArtNetData recieveState = (ArtNetData)(state.AsyncState);

                if (recieveState == null)
                {
                    // Packet was invalid continue receiving
                    ReceiveArtNet();
                }
                recieveState.DataLength = EndReceiveFrom(state, ref remoteEndPoint);

                // Protect against UDP loopback where we recieve our own packets.
                if (LocalEndPoint != remoteEndPoint && recieveState.Valid)
                {
                    LastPacket = DateTime.Now;
                    var packet = ArtNetPacket.FromData(recieveState);
                    if (packet == null)
                    {
                        ReceiveArtNet();
                    }

                    NewPacket?.Invoke(this, new NewPacketEventArgs <ArtNetPacket>((IPEndPoint)remoteEndPoint, packet));
                }
            }
            catch (Exception ex)
            {
                UnhandledException?.Invoke(this, new UnhandledExceptionEventArgs(ex, false));
            }
            ReceiveArtNet();
        }