Example #1
0
        /// <summary>
        /// Processes the packet within the context. Returns true whether the packet was processed or throttled.
        /// </summary>
        /// <param name="channel">The through which the packet is coming/going out.</param>
        /// <param name="context">The packet context for this operation.</param>
        /// <returns>True whether the packet was processed or throttled, false otherwise.</returns>
        public static ProcessingState Process(Connection channel, ProcessingContext context)
        {
            // It can only process packets
            var packet = context.Packet as MqttPacket;

            if (packet == null)
            {
                return(ProcessingState.Failure);
            }

            // Check the transport type currently used for sending our packet
            if (channel.Client.TransportType != TransportType.Tcp)
            {
                return(ProcessingState.Failure);
            }

            // Acquire a buffer which will be released once everything is sent
            var buffer = Buffers.Acquire();

            // If we have protocol version set, add it to the packet
            var mqttCtx = channel.Client.Context;

            if (mqttCtx != null)
            {
                packet.ProtocolVersion = mqttCtx.Version;
                packet.IsEmitter       = mqttCtx.IsEmitter;
            }

            // Write the packet to the buffer
            buffer.Size = packet.TryWrite(buffer.AsSegment());

            // Set the buffer to send out
            context.SwitchBuffer(buffer);
            return(ProcessingState.Success);
        }
Example #2
0
        /// <summary>
        /// Processes the packet within the context. Returns true whether the packet was processed or throttled.
        /// </summary>
        /// <param name="channel">The through which the packet is coming/going out.</param>
        /// <param name="context">The packet context for this operation.</param>
        /// <returns>True whether the packet was processed or throttled, false otherwise.</returns>
        public static ProcessingState Process(Emitter.Connection channel, ProcessingContext context)
        {
            // Get the buffer to decompress
            var input = context.Buffer.AsSegment();

            try
            {
                // Reserve the buffer, we know exactly how many bytes to decompress
                var output = context.BufferReserve(
                    (int)VarInt.UvarInt(input.Array, input.Offset).Value
                    );

                // Decompress
                var length = Snappy.Decode(
                    input.Array, input.Offset, input.Count,
                    output.Array, output.Offset, output.Size
                    );

                // Switch the buffer to the decompressed one
                context.SwitchBuffer(output);
            }
            catch (Exception ex)
            {
                ex.Log();
            }

            return(ProcessingState.Success);
        }
Example #3
0
        /// <summary>
        /// Processes the packet within the context. Returns true whether the packet was processed or throttled.
        /// </summary>
        /// <param name="channel">The through which the packet is coming/going out.</param>
        /// <param name="context">The packet context for this operation.</param>
        /// <returns>True whether the packet was processed or throttled, false otherwise.</returns>
        public static ProcessingState Process(Emitter.Connection channel, ProcessingContext context)
        {
            // It can only process StringPacket objects
            var packet = context.Packet as BytePacket;

            if (packet == null)
            {
                return(ProcessingState.Failure);
            }

            // Write the string to the buffer
            context.SwitchBuffer(
                context.BufferWrite(packet.Buffer, packet.Offset, packet.Count)
                );

            return(ProcessingState.Success);
        }
Example #4
0
        /// <summary>
        /// Processes the packet within the context. Returns true whether the packet was processed or throttled.
        /// </summary>
        /// <param name="channel">The through which the packet is coming/going out.</param>
        /// <param name="context">The packet context for this operation.</param>
        /// <returns>True whether the packet was processed or throttled, false otherwise.</returns>
        public static ProcessingState Process(Emitter.Connection channel, ProcessingContext context)
        {
            // It can only process StringPacket objects
            var packet = context.Packet as StringPacket;

            if (packet == null)
            {
                return(ProcessingState.Failure);
            }

            // Get the encoding and the string
            var encoding = packet.Encoding;
            var payload  = packet.StringBuilder.ToString();

            // Write the string to the buffer
            context.SwitchBuffer(
                context.BufferWrite(encoding.GetBytes(payload))
                );

            return(ProcessingState.Success);
        }