Beispiel #1
0
        public void Subscribe(string destination, HeaderFrame Headers, Action <StompFrame> CallBack)
        {
            int id = _subID++;

            if (Headers == null)
            {
                Headers = new SubscribeHeaderFrame($"sub-{id}", destination);
            }

            _sub_tracker.Add(
                Headers.GetHeaderFromProperty(new IdHeader().Property).Setting,
                CallBack
                );
            _dest_tracker.Add(
                Headers.GetHeaderFromProperty(new DestinationHeader().Property).Setting,
                CallBack
                );

            if (_sock_connected)
            {
                Send(new StompFrame(new SubscribeCommand(), Headers));
            }
            else
            {
                _subscriptions.Enqueue(new StompFrame(new SubscribeCommand(), Headers));
            }
        }
Beispiel #2
0
        public void Send(string destination, HeaderFrame Headers, object obj)
        {
            if (Headers == null)
            {
                Headers = new SendHeaderFrame(destination, obj, ContentTypeHeader.Inputs.JSON);
            }

            Send(new StompFrame(new SendCommand(), Headers));
        }
Beispiel #3
0
        private HeaderFrame DecodeHeaderFrame(DecodingContext context, int payloadLength, byte flags,
                                              int streamIdentifier)
        {
            var paddingSize = GetPaddingSize(context, flags);
            var isPriority  = (flags & 0x20) == 0x20;

            if (isPriority)
            {
                var isExclusive        = (context.Buffer[context.Offset] & Bit32) == Bit32;
                var streamDependencyId = BitConverter.ToInt32(context.Buffer, context.Offset) & ~Bit32;
                context.Offset += 4;
                var weight = context.Buffer[context.Offset++];
                context.BytesLeftToProcess -= 5;
            }


            _headerFrame = new HeaderFrame
            {
                FrameFlags       = flags,
                StreamIdentifier = streamIdentifier
            };
            _headerReceiver = args => _headerFrame.Add(args.Name, args.Value, args.IsIndexingAllowed);
            _headerDecoder.Decode(context.Buffer, ref context.Offset, ref context.BytesLeftToProcess);
            _headerReceiver = null;

            // skip padding
            context.BytesLeftToProcess -= paddingSize;
            context.Offset             += paddingSize;

            // A HEADERS frame without the END_HEADERS flag set MUST be followed by a CONTINUATION frame for the same stream
            if ((flags & 0x4) == 0x4)
            {
                _continuationHandlers[streamIdentifier] = ResumeHeaderFrame;
                return(null);
            }

            var frame = _headerFrame;

            _headerFrame = null;
            return(frame);
        }
Beispiel #4
0
        /// <summary>
        ///     CONTINUATION frame for the HEADERS frame
        /// </summary>
        private HttpFrame ResumeHeaderFrame(DecodingContext context, int contentlength, byte flags, int streamIdentifier)
        {
            if (streamIdentifier == 0)
            {
                throw new DecoderException(Http2ErrorCode.ProtocolError,
                                           "CONTINUATION frames require a stream identifier.");
            }

            _headerReceiver = args => _headerFrame.Add(args.Name, args.Value, args.IsIndexingAllowed);
            _headerDecoder.Decode(context.Buffer, ref context.Offset, ref context.BytesLeftToProcess);
            _headerReceiver = null;

            // A HEADERS frame without the END_HEADERS flag set MUST be followed by a CONTINUATION frame for the same stream
            if ((flags & 0x4) == 0x4)
            {
                _continuationHandlers[streamIdentifier] = ResumeHeaderFrame;
                return(null);
            }

            var frame = _headerFrame;

            _headerFrame = null;
            return(frame);
        }
Beispiel #5
0
        /// <summary>
        /// Handles incoming commands from devices connected over the command channel.
        /// </summary>
        /// <param name="clientID">Guid of client that sent the command.</param>
        /// <param name="commandBuffer">Data buffer received from connected client device.</param>
        /// <param name="length">Valid length of data within the buffer.</param>
        protected override void DeviceCommandHandler(Guid clientID, byte[] commandBuffer, int length)
        {
            try
            {
                // Interpret data received from a client as a command frame
                CommandFrame commandFrame = new CommandFrame(commandBuffer, 0, length);
                TcpServer commandChannel = CommandChannel;

                switch (commandFrame.Command)
                {
                    case DeviceCommand.SendConfigurationFrame1:
                    case DeviceCommand.SendConfigurationFrame2:
                        if (commandChannel != null)
                        {
                            commandChannel.SendToAsync(clientID, m_configurationFrame.BinaryImage);
                            OnStatusMessage("Received device command \"{0}\" - frame was returned.", commandFrame.Command);
                        }
                        break;
                    case DeviceCommand.SendHeaderFrame:
                        if (commandChannel != null)
                        {
                            HeaderFrame headerFrame = new HeaderFrame("IEEE C37.118 Concentrator Status:\r\n\r\n" + Status);
                            commandChannel.SendToAsync(clientID, headerFrame.BinaryImage);
                            OnStatusMessage("Received device command \"SendHeaderFrame\" - frame was returned.");
                        }
                        break;
                    case DeviceCommand.EnableRealTimeData:
                        // Only responding to stream control command if auto-start data channel is false
                        if (!AutoStartDataChannel)
                        {
                            StartDataChannel();
                            OnStatusMessage("Received device command \"EnableRealTimeData\" - concentrator real-time data stream was started.");
                        }
                        break;
                    case DeviceCommand.DisableRealTimeData:
                        // Only responding to stream control command if auto-start data channel is false
                        if (!AutoStartDataChannel)
                        {
                            StopDataChannel();
                            OnStatusMessage("Received device command \"DisableRealTimeData\" - concentrator real-time data stream was stopped.");
                        }
                        break;
                    default:
                        OnStatusMessage("Received unsupported device command: {0}", commandFrame.Command);
                        break;
                }
            }
            catch (Exception ex)
            {
                OnProcessException(new InvalidOperationException(string.Format("Encountered an exception while processing received client data: {0}", ex.Message), ex));
            }
        }
Beispiel #6
0
        /// <summary>
        /// Handles incoming commands from devices connected over the command channel.
        /// </summary>
        /// <param name="clientID">Guid of client that sent the command.</param>
        /// <param name="connectionID">Remote client connection identification (i.e., IP:Port).</param>
        /// <param name="commandBuffer">Data buffer received from connected client device.</param>
        /// <param name="length">Valid length of data within the buffer.</param>
        protected override void DeviceCommandHandler(Guid clientID, string connectionID, byte[] commandBuffer, int length)
        {
            try
            {
                // Interpret data received from a client as a command frame
                CommandFrame commandFrame   = new CommandFrame(commandBuffer, 0, length);
                IServer      commandChannel = (IServer)CommandChannel ?? DataChannel;

                // Validate incoming ID code if requested
                if (!m_validateIDCode || commandFrame.IDCode == this.IDCode)
                {
                    switch (commandFrame.Command)
                    {
                    case DeviceCommand.SendConfigurationFrame1:
                    case DeviceCommand.SendConfigurationFrame2:
                        if (commandChannel != null)
                        {
                            commandChannel.SendToAsync(clientID, m_configurationFrame.BinaryImage, 0, m_configurationFrame.BinaryLength);
                            OnStatusMessage("Received request for \"{0}\" from \"{1}\" - frame was returned.", commandFrame.Command, connectionID);
                        }
                        break;

                    case DeviceCommand.SendHeaderFrame:
                        if (commandChannel != null)
                        {
                            StringBuilder status = new StringBuilder();
                            status.Append("IEEE C37.118 Concentrator:\r\n\r\n");
                            status.AppendFormat(" Auto-publish config frame: {0}\r\n", AutoPublishConfigurationFrame);
                            status.AppendFormat("   Auto-start data channel: {0}\r\n", AutoStartDataChannel);
                            status.AppendFormat("       Data stream ID code: {0}\r\n", IDCode);
                            status.AppendFormat("       Derived system time: {0} UTC\r\n", ((DateTime)RealTime).ToString("yyyy-MM-dd HH:mm:ss.fff"));

                            HeaderFrame headerFrame = new HeaderFrame(status.ToString());
                            commandChannel.SendToAsync(clientID, headerFrame.BinaryImage, 0, headerFrame.BinaryLength);
                            OnStatusMessage("Received request for \"SendHeaderFrame\" from \"{0}\" - frame was returned.", connectionID);
                        }
                        break;

                    case DeviceCommand.EnableRealTimeData:
                        // Only responding to stream control command if auto-start data channel is false
                        if (!AutoStartDataChannel)
                        {
                            StartDataChannel();
                            OnStatusMessage("Received request for \"EnableRealTimeData\" from \"{0}\" - concentrator real-time data stream was started.", connectionID);
                        }
                        else
                        {
                            OnStatusMessage("Request for \"EnableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID);
                        }

                        break;

                    case DeviceCommand.DisableRealTimeData:
                        // Only responding to stream control command if auto-start data channel is false
                        if (!AutoStartDataChannel)
                        {
                            StopDataChannel();
                            OnStatusMessage("Received request for \"DisableRealTimeData\" from \"{0}\" - concentrator real-time data stream was stopped.", connectionID);
                        }
                        else
                        {
                            OnStatusMessage("Request for \"DisableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID);
                        }

                        break;

                    default:
                        OnStatusMessage("Request for \"{0}\" from \"{1}\" was ignored - device command is unsupported.", commandFrame.Command, connectionID);
                        break;
                    }
                }
                else
                {
                    OnStatusMessage("WARNING: Concentrator ID code validation failed for device command \"{0}\" from \"{1}\" - no action was taken.", commandFrame.Command, connectionID);
                }
            }
            catch (Exception ex)
            {
                OnProcessException(new InvalidOperationException(string.Format("Remotely connected device \"{0}\" sent an unrecognized data sequence to the concentrator, no action was taken. Exception details: {1}", connectionID, ex.Message), ex));
            }
        }
Beispiel #7
0
        private async void lstView_Scrolled(object sender, ScrolledEventArgs e)
        {
            bool   update               = false;
            double heightHeader         = HeaderFrame.HeightRequest;
            double heightHeaderNegative = heightHeader * -1;

            if (lastScroll != e.ScrollY)
            {
                var y = (int)((float)e.ScrollY / 1.5f);

                //UP
                if (y > lastScroll)
                {
                    update = y > (lastScroll + 0.5);
                }
                //DOWN
                else
                {
                    update = y < (lastScroll + 0.5);
                }

                if (update)
                {
                    lastScroll        = y;
                    ScrollYLabel.Text = "y =>" + y.ToString();
                    double value = heightHeader - y;
                    if (value > 0)
                    {
                        if (lstView.TranslationY != value)
                        {
                            lstView.TranslationY = value;
                        }
                    }
                    else if (lstView.TranslationY != 0)
                    {
                        lstView.TranslationY = 0;
                    }
                    if (!animation && showHeader)
                    {
                        value = y * -1;
                        if (value < heightHeaderNegative)
                        {
                            if (HeaderFrame.TranslationY != heightHeaderNegative)
                            {
                                HeaderFrame.TranslationY = heightHeaderNegative;
                                showHeader = false;
                            }
                        }
                        else
                        {
                            if (HeaderFrame.TranslationY != value)
                            {
                                HeaderFrame.TranslationY = value;
                            }
                        }
                        ListY.Text  = "Lista => " + lstView.TranslationY.ToString();
                        StackY.Text = "Header => " + HeaderFrame.TranslationY.ToString();
                    }
                }

                if (previousScrollPosition - 4 > e.ScrollY && HeaderFrame.TranslationY == heightHeaderNegative && !animation)
                {
                    animation  = true;
                    showHeader = false;
                    await HeaderFrame.TranslateTo(HeaderFrame.TranslationX, 0, 100);
                }
                else if (previousScrollPosition + 4 < e.ScrollY && HeaderFrame.TranslationY == 0 && animation)
                {
                    animation  = false;
                    showHeader = true;
                    if (y > heightHeader - 1)
                    {
                        await HeaderFrame.TranslateTo(HeaderFrame.TranslationX, heightHeaderNegative, 100);
                    }
                }

                previousScrollPosition = e.ScrollY;
            }
        }
 /// <summary>
 /// Creates a new <see cref="ConfigurationFrameParsingState"/> from specified parameters.
 /// </summary>
 /// <param name="parsedBinaryLength">Binary length of the <see cref="ConfigurationFrame"/> being parsed.</param>
 /// <param name="headerFrame">Previously parsed header frame that contains needed station name.</param>
 /// <param name="createNewCellFunction">Reference to delegate to create new <see cref="ConfigurationCell"/> instances.</param>
 /// <param name="trustHeaderLength">Determines if header lengths should be trusted over parsed byte count.</param>
 /// <param name="validateCheckSum">Determines if frame's check-sum should be validated.</param>
 public ConfigurationFrameParsingState(int parsedBinaryLength, HeaderFrame headerFrame, CreateNewCellFunction<IConfigurationCell> createNewCellFunction, bool trustHeaderLength, bool validateCheckSum)
     : base(parsedBinaryLength, createNewCellFunction, trustHeaderLength, validateCheckSum, 1)
 {
     m_headerFrame = headerFrame;
 }
Beispiel #9
0
        // Fields

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates a new <see cref="ConfigurationFrameParsingState"/> from specified parameters.
        /// </summary>
        /// <param name="parsedBinaryLength">Binary length of the <see cref="ConfigurationFrame"/> being parsed.</param>
        /// <param name="headerFrame">Previously parsed header frame that contains needed station name.</param>
        /// <param name="createNewCellFunction">Reference to delegate to create new <see cref="ConfigurationCell"/> instances.</param>
        /// <param name="trustHeaderLength">Determines if header lengths should be trusted over parsed byte count.</param>
        /// <param name="validateCheckSum">Determines if frame's check-sum should be validated.</param>
        public ConfigurationFrameParsingState(int parsedBinaryLength, HeaderFrame headerFrame, CreateNewCellFunction <IConfigurationCell> createNewCellFunction, bool trustHeaderLength, bool validateCheckSum)
            : base(parsedBinaryLength, createNewCellFunction, trustHeaderLength, validateCheckSum, 1)
        {
            HeaderFrame = headerFrame;
        }
Beispiel #10
0
        /// <summary>
        /// Handles incoming commands from devices connected over the command channel.
        /// </summary>
        /// <param name="clientID">Guid of client that sent the command.</param>
        /// <param name="connectionID">Remote client connection identification (i.e., IP:Port).</param>
        /// <param name="commandBuffer">Data buffer received from connected client device.</param>
        /// <param name="length">Valid length of data within the buffer.</param>
        protected override void DeviceCommandHandler(Guid clientID, string connectionID, byte[] commandBuffer, int length)
        {
            try
            {
                // Interpret data received from a client as a command frame
                CommandFrame commandFrame = new CommandFrame(commandBuffer, 0, length);
                IServer commandChannel = (IServer)CommandChannel ?? DataChannel;

                // Validate incoming ID code if requested
                if (!m_validateIDCode || commandFrame.IDCode == this.IDCode)
                {
                    switch (commandFrame.Command)
                    {
                        case DeviceCommand.SendConfigurationFrame1:
                        case DeviceCommand.SendConfigurationFrame2:
                            if (commandChannel != null)
                            {
                                commandChannel.SendToAsync(clientID, m_configurationFrame.BinaryImage, 0, m_configurationFrame.BinaryLength);
                                OnStatusMessage("Received request for \"{0}\" from \"{1}\" - frame was returned.", commandFrame.Command, connectionID);
                            }
                            break;
                        case DeviceCommand.SendHeaderFrame:
                            if (commandChannel != null)
                            {
                                StringBuilder status = new StringBuilder();
                                status.Append("IEEE C37.118 Concentrator:\r\n\r\n");
                                status.AppendFormat(" Auto-publish config frame: {0}\r\n", AutoPublishConfigurationFrame);
                                status.AppendFormat("   Auto-start data channel: {0}\r\n", AutoStartDataChannel);
                                status.AppendFormat("       Data stream ID code: {0}\r\n", IDCode);
                                status.AppendFormat("       Derived system time: {0} UTC\r\n", ((DateTime)RealTime).ToString("yyyy-MM-dd HH:mm:ss.fff"));

                                HeaderFrame headerFrame = new HeaderFrame(status.ToString());
                                commandChannel.SendToAsync(clientID, headerFrame.BinaryImage, 0, headerFrame.BinaryLength);
                                OnStatusMessage("Received request for \"SendHeaderFrame\" from \"{0}\" - frame was returned.", connectionID);
                            }
                            break;
                        case DeviceCommand.EnableRealTimeData:
                            // Only responding to stream control command if auto-start data channel is false
                            if (!AutoStartDataChannel)
                            {
                                StartDataChannel();
                                OnStatusMessage("Received request for \"EnableRealTimeData\" from \"{0}\" - concentrator real-time data stream was started.", connectionID);
                            }
                            else
                                OnStatusMessage("Request for \"EnableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID);

                            break;
                        case DeviceCommand.DisableRealTimeData:
                            // Only responding to stream control command if auto-start data channel is false
                            if (!AutoStartDataChannel)
                            {
                                StopDataChannel();
                                OnStatusMessage("Received request for \"DisableRealTimeData\" from \"{0}\" - concentrator real-time data stream was stopped.", connectionID);
                            }
                            else
                                OnStatusMessage("Request for \"DisableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID);

                            break;
                        default:
                            OnStatusMessage("Request for \"{0}\" from \"{1}\" was ignored - device command is unsupported.", commandFrame.Command, connectionID);
                            break;
                    }
                }
                else
                    OnStatusMessage("WARNING: Concentrator ID code validation failed for device command \"{0}\" from \"{1}\" - no action was taken.", commandFrame.Command, connectionID);
            }
            catch (Exception ex)
            {
                OnProcessException(new InvalidOperationException(string.Format("Remotely connected device \"{0}\" sent an unrecognized data sequence to the concentrator, no action was taken. Exception details: {1}", connectionID, ex.Message), ex));
            }
        }