示例#1
0
        //-------------------------------------------------------------
        //
        //	Event() - called each time interval that one of the
        //			  channels within our bank changes with ALL
        //			  channelValues
        //
        //			  to insure that this is called for EVERY event
        //			  the user CAN add an unused channel that changes
        //			  every event.
        //
        //-------------------------------------------------------------

        public void Event(byte[] channelValues)
        {
            var stopWatch = Stopwatch.StartNew();

            _eventCnt++;

            foreach (var uE in _universeTable.Where(uE => uE.Active))
            {
                if (_eventRepeatCount > 0)
                {
                    if (uE.EventRepeatCount-- > 0)
                    {
                        if (E131Pkt.CompareSlots(uE.PhyBuffer, channelValues, uE.Start, uE.Size))
                        {
                            continue;
                        }
                    }
                }

                E131Pkt.CopySeqNumSlots(uE.PhyBuffer, channelValues, uE.Start, uE.Size, _seqNum++);
                uE.Socket.SendTo(uE.PhyBuffer, uE.DestIPEndPoint);
                uE.EventRepeatCount = _eventRepeatCount;

                uE.PktCount++;
                uE.SlotCount += uE.Size;
            }

            stopWatch.Stop();

            _totalTicks += stopWatch.ElapsedTicks;
        }
示例#2
0
        //-------------------------------------------------------------
        //
        //	Startup() - called when a sequence is executed
        //
        //
        //	todo:
        //
        //		1) probably add error checking on all 'new' operations
        //		and system calls
        //
        //		2) better error reporting and logging
        //
        //-------------------------------------------------------------


        public void Startup()
        {
            // a single socket to use for unicast (if needed)
            Socket unicastSocket = null;

            // a sortedlist containing the multicast sockets we've already done
            var nicSockets = new SortedList <string, Socket>();

            // reload all of our xml into working objects
            LoadSetupNodeInfo();

            // initialize plugin wide stats
            _eventCnt   = 0;
            _totalTicks = 0;

            // initialize sequence # for E1.31 packet (should it be per universe?)
            _seqNum = 0;

            // initialize messageTexts stringbuilder to hold all warnings/errors
            _messageTexts = new StringBuilder();

            // check for configured from/to
            if (_pluginChannelsFrom == 0 && _pluginChannelsTo == 0)
            {
                foreach (var uE in _universeTable)
                {
                    uE.Active = false;
                }

                _messageTexts.AppendLine("Plugin Channels From/To Configuration Error!!");
            }

            // now we need to scan the universeTable
            foreach (var uE in _universeTable)
            {
                // active? - check universeentry start and size
                if (uE.Active)
                {
                    // is start out of range?
                    if (_pluginChannelsFrom + uE.Start > _pluginChannelsTo)
                    {
                        _messageTexts.AppendLine("Start Error - " + uE.InfoToText);
                        uE.Active = false;
                    }

                    // is size (end) out of range?
                    if (_pluginChannelsFrom + uE.Start + uE.Size - 1 > _pluginChannelsTo)
                    {
                        _messageTexts.AppendLine("Start/Size Error - " + uE.InfoToText);
                        uE.Active = false;
                    }
                }

                // if it's still active we'll look into making a socket for it
                if (!uE.Active)
                {
                    continue;
                }
                // if it's unicast it's fairly easy to do
                IPAddress ipAddress;
                if (uE.Unicast != null)
                {
                    // is this the first unicast universe?
                    if (unicastSocket == null)
                    {
                        // yes - make a new socket to use for ALL unicasts
                        unicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    }

                    // use the common unicastsocket
                    uE.Socket = unicastSocket;

                    // try to parse our ip address
                    if (!IPAddress.TryParse(uE.Unicast, out ipAddress))
                    {
                        // oops - bad ip, fuss and deactivate
                        uE.Active = false;
                        uE.Socket = null;
                        _messageTexts.AppendLine("Invalid Unicast IP: " + uE.Unicast + " - " + uE.RowUnivToText);
                    }

                    else
                    {
                        // if good, make our destination endpoint
                        uE.DestIPEndPoint = new IPEndPoint(ipAddress, 5568);
                    }
                }

                // if it's multicast roll up your sleeves we've got work to do
                if (uE.Multicast != null)
                {
                    // create an ipaddress object based on multicast universe ip rules
                    var multicastIPAddress = new IPAddress(new byte[] { 239, 255, (byte)(uE.Universe >> 8), (byte)(uE.Universe & 0xff) });
                    // create an ipendpoint object based on multicast universe ip/port rules
                    var multicastIPEndPoint = new IPEndPoint(multicastIPAddress, 5568);

                    // first check for multicast id in nictable
                    if (!_nicTable.ContainsKey(uE.Multicast))
                    {
                        // no - deactivate and scream & yell!!
                        uE.Active = false;
                        _messageTexts.AppendLine("Invalid Multicast NIC ID: " + uE.Multicast + " - " + uE.RowUnivToText);
                    }

                    else
                    {
                        // yes - let's get a working networkinterface object
                        var networkInterface = _nicTable[uE.Multicast];

                        // have we done this multicast id before?
                        if (nicSockets.ContainsKey(uE.Multicast))
                        {
                            // yes - easy to do - use existing socket
                            uE.Socket = nicSockets[uE.Multicast];

                            // setup destipendpoint based on multicast universe ip rules
                            uE.DestIPEndPoint = multicastIPEndPoint;
                        }

                        // is the interface up?
                        else if (networkInterface.OperationalStatus != OperationalStatus.Up)
                        {
                            // no - deactivate and scream & yell!!
                            uE.Active = false;
                            _messageTexts.AppendLine("Multicast Interface Down: " + networkInterface.Name + " - " + uE.RowUnivToText);
                        }

                        else
                        {
                            // new interface in 'up' status - let's make a new udp socket
                            uE.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                            // get a working copy of ipproperties
                            var ipProperties = networkInterface.GetIPProperties();

                            // get a working copy of the ipv4interfaceproperties
                            ipProperties.GetIPv4Properties();

                            // get a working copy of all unicasts
                            var unicasts = ipProperties.UnicastAddresses;

                            ipAddress = null;

                            foreach (var unicast in unicasts.Where(unicast => unicast.Address.AddressFamily == AddressFamily.InterNetwork))
                            {
                                ipAddress = unicast.Address;
                            }

                            if (ipAddress == null)
                            {
                                _messageTexts.AppendLine("No IP On Multicast Interface: " + networkInterface.Name + " - " + uE.InfoToText);
                            }

                            else
                            {
                                // set the multicastinterface option
                                uE.Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ipAddress.GetAddressBytes());
                                // set the multicasttimetolive option
                                uE.Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, uE.Ttl);

                                // setup destipendpoint based on multicast universe ip rules
                                uE.DestIPEndPoint = multicastIPEndPoint;

                                // add this socket to the socket table for reuse
                                nicSockets.Add(uE.Multicast, uE.Socket);
                            }
                        }
                    }
                }

                // if still active we need to create an empty packet
                if (!uE.Active)
                {
                    continue;
                }
                var zeroBfr = new byte[uE.Size];
                var e131Pkt = new E131Pkt(_guid, "", 0, (ushort)uE.Universe, zeroBfr, 0, uE.Size);
                uE.PhyBuffer = e131Pkt.PhyBuffer;
            }

            // any warnings/errors recorded?
            if (_messageTexts.Length > 0)
            {
                // should we display them
                if (_warningsOption)
                {
                    // show our warnings/errors
                    J1MsgBox.ShowMsg("The following warnings and errors were detected during startup:", _messageTexts.ToString(),
                                     "Startup Warnings/Errors", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // discard warning/errors after reporting them
                    _messageTexts = new StringBuilder();
                }
            }
        }
示例#3
0
        //-------------------------------------------------------------
        //
        //    Startup() - called when a sequence is executed
        //
        //
        //    todo:
        //
        //        1) probably add error checking on all 'new' operations
        //        and system calls
        //
        //        2) better error reporting and logging
        //    
        //-------------------------------------------------------------
        public void Startup()
        {
            // a single socket to use for unicast (if needed)
            Socket unicastSocket = null;

            // a sortedlist containing the multicast sockets we've already done
            var nicSockets = new SortedList<string, Socket>();

            // reload all of our xml into working objects
            LoadSetupNodeInfo();

            // initialize plugin wide stats
            _eventCnt = 0;
            _totalTicks = 0;

            // initialize sequence # for E1.31 packet (should it be per universe?)
            _seqNum = 0;

            // initialize messageTexts stringbuilder to hold all warnings/errors
            _messageTexts = new StringBuilder();

            // check for configured from/to
            if (_pluginChannelsFrom == 0 && _pluginChannelsTo == 0) {
                foreach (var uE in _universeTable) {
                    uE.Active = false;
                }

                _messageTexts.AppendLine("Plugin Channels From/To Configuration Error!!");
            }

            // now we need to scan the universeTable
            foreach (var uE in _universeTable) {
                // active? - check universeentry start and size
                if (uE.Active) {
                    // is start out of range?
                    if (_pluginChannelsFrom + uE.Start > _pluginChannelsTo) {
                        _messageTexts.AppendLine("Start Error - " + uE.InfoToText);
                        uE.Active = false;
                    }

                    // is size (end) out of range?
                    if (_pluginChannelsFrom + uE.Start + uE.Size - 1 > _pluginChannelsTo) {
                        _messageTexts.AppendLine("Start/Size Error - " + uE.InfoToText);
                        uE.Active = false;
                    }
                }

                // if it's still active we'll look into making a socket for it
                if (!uE.Active) {
                    continue;
                }
                // if it's unicast it's fairly easy to do
                IPAddress ipAddress;
                if (uE.Unicast != null) {
                    // is this the first unicast universe?
                    if (unicastSocket == null) {
                        // yes - make a new socket to use for ALL unicasts
                        unicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    }

                    // use the common unicastsocket
                    uE.Socket = unicastSocket;

                    // try to parse our ip address
                    if (!IPAddress.TryParse(uE.Unicast, out ipAddress)) {
                        // oops - bad ip, fuss and deactivate
                        uE.Active = false;
                        uE.Socket = null;
                        _messageTexts.AppendLine("Invalid Unicast IP: " + uE.Unicast + " - " + uE.RowUnivToText);
                    }

                    else {
                        // if good, make our destination endpoint
                        uE.DestIPEndPoint = new IPEndPoint(ipAddress, 5568);
                    }
                }

                // if it's multicast roll up your sleeves we've got work to do
                if (uE.Multicast != null) {
                    // create an ipaddress object based on multicast universe ip rules
                    var multicastIPAddress = new IPAddress(new byte[] {239, 255, (byte) (uE.Universe >> 8), (byte) (uE.Universe & 0xff)});
                    // create an ipendpoint object based on multicast universe ip/port rules
                    var multicastIPEndPoint = new IPEndPoint(multicastIPAddress, 5568);

                    // first check for multicast id in nictable
                    if (!_nicTable.ContainsKey(uE.Multicast)) {
                        // no - deactivate and scream & yell!!
                        uE.Active = false;
                        _messageTexts.AppendLine("Invalid Multicast NIC ID: " + uE.Multicast + " - " + uE.RowUnivToText);
                    }

                    else {
                        // yes - let's get a working networkinterface object
                        var networkInterface = _nicTable[uE.Multicast];

                        // have we done this multicast id before?
                        if (nicSockets.ContainsKey(uE.Multicast)) {
                            // yes - easy to do - use existing socket
                            uE.Socket = nicSockets[uE.Multicast];

                            // setup destipendpoint based on multicast universe ip rules
                            uE.DestIPEndPoint = multicastIPEndPoint;
                        }

                            // is the interface up?
                        else if (networkInterface.OperationalStatus != OperationalStatus.Up) {
                            // no - deactivate and scream & yell!!
                            uE.Active = false;
                            _messageTexts.AppendLine("Multicast Interface Down: " + networkInterface.Name + " - " + uE.RowUnivToText);
                        }

                        else {
                            // new interface in 'up' status - let's make a new udp socket
                            uE.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                            // get a working copy of ipproperties
                            var ipProperties = networkInterface.GetIPProperties();

                            // get a working copy of the ipv4interfaceproperties
                            ipProperties.GetIPv4Properties();

                            // get a working copy of all unicasts
                            var unicasts = ipProperties.UnicastAddresses;

                            ipAddress = null;

                            foreach (var unicast in unicasts.Where(unicast => unicast.Address.AddressFamily == AddressFamily.InterNetwork)) {
                                ipAddress = unicast.Address;
                            }

                            if (ipAddress == null) {
                                _messageTexts.AppendLine("No IP On Multicast Interface: " + networkInterface.Name + " - " + uE.InfoToText);
                            }

                            else {
                                // set the multicastinterface option
                                uE.Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ipAddress.GetAddressBytes());
                                // set the multicasttimetolive option
                                uE.Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, uE.Ttl);

                                // setup destipendpoint based on multicast universe ip rules
                                uE.DestIPEndPoint = multicastIPEndPoint;

                                // add this socket to the socket table for reuse
                                nicSockets.Add(uE.Multicast, uE.Socket);
                            }
                        }
                    }
                }

                // if still active we need to create an empty packet
                if (!uE.Active) {
                    continue;
                }
                var zeroBfr = new byte[uE.Size];
                var e131Pkt = new E131Pkt(_guid, "", 0, (ushort) uE.Universe, zeroBfr, 0, uE.Size);
                uE.PhyBuffer = e131Pkt.PhyBuffer;
            }

            // any warnings/errors recorded?
            if (_messageTexts.Length > 0)
                // should we display them
                if (_warningsOption) {
                    // show our warnings/errors
                    J1MsgBox.ShowMsg("The following warnings and errors were detected during startup:", _messageTexts.ToString(),
                                     "Startup Warnings/Errors", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    // discard warning/errors after reporting them
                    _messageTexts = new StringBuilder();
                }
        }