Example #1
0
        /// <summary>
        /// Attempts to generate the key and returns the result.
        /// </summary>
        /// <param name="client">The remote client.</param>
        /// <param name="info">The full channel string.</param>
        /// <param name="message">The message to publish.</param>
        public static bool TryProcess(IClient client, EmitterChannel info, ArraySegment <byte> message)
        {
            // Is this a special api request?
            if (info.Key != EmitterConst.ApiPrefix)
            {
                return(false);
            }

            // The response to send out, default to bad request
            EmitterResponse response = EmitterError.BadRequest;

            try
            {
                switch (info.Target)
                {
                case 548658350:
                    // Handle the 'keygen' request.
                    response = HandleKeyGen.Process(client, info, message.As <KeyGenRequest>());
                    return(true);

                case 3869262148:
                    // Handle the 'presence' request.
                    response = HandlePresence.Process(client, info, message.As <PresenceRequest>());
                    return(true);

                default:
                    // We've got a bad request
                    response = EmitterError.BadRequest;
                    return(true);
                }
            }
            catch (NotImplementedException)
            {
                // We've got a not implemented exception
                response = EmitterError.NotImplemented;
                return(true);
            }
            catch (Exception ex)
            {
                // We've got a an internal error
                Service.Logger.Log(ex);
                response = EmitterError.ServerError;
                return(true);
            }
            finally
            {
                // Send the response, always
                if (response != null)
                {
                    SendResponse(client, "emitter/" + info.Channel, response);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Sends a response to the client.
        /// </summary>
        /// <param name="client">The client to reply to.</param>
        /// <param name="response">The emitte response to send.</param>
        private static void SendResponse(IClient client, string channel, EmitterResponse response)
        {
            // Serialize the response
            var serialized = JsonConvert.SerializeObject(response, Formatting.Indented);

            if (serialized == null)
            {
                return;
            }

            // Send the message out
            var msg = MqttPublishPacket.Acquire();

            msg.Channel = channel;
            msg.Message = serialized.AsUTF8().AsSegment();
            client.Send(msg);
        }