private void ProcessDispatch(EventPayload response)
        {
            if (response.Command != Command.Dispatch)
            {
                return;
            }
            if (!response.Event.HasValue)
            {
                return;
            }

            switch (response.Event.Value)
            {
            //We are to join the server
            case ServerEvent.ActivitySpectate:
                var spectate = response.GetObject <SpectateMessage>();
                EnqueueMessage(spectate);
                break;

            case ServerEvent.ActivityJoin:
                var join = response.GetObject <JoinMessage>();
                EnqueueMessage(join);
                break;

            case ServerEvent.ActivityJoinRequest:
                var request = response.GetObject <JoinRequestMessage>();
                EnqueueMessage(request);
                break;

            //Unkown dispatch event received. We should just ignore it.
            default:
                Logger.Warning("Ignoring {0}", response.Event.Value);
                break;
            }
        }
예제 #2
0
        void ProcessEvent(EventPayload a_event)
        {
            if (a_event.Event.HasValue && a_event.Event == EventPayload.e_ServerEvent.Error)
            {
                ErrorMessage error = a_event.GetObject <ErrorMessage>();

                InternalConsole.Error(string.Format("Discord responded with error: ({0}) {1}", error.ErrorCode, error.Message));

                EnqueueMessage(error);
            }

            if (m_state == e_RPCState.Connecting)
            {
                if (a_event.Command == e_Command.Dispatch && a_event.Event.HasValue && a_event.Event.Value == EventPayload.e_ServerEvent.Ready)
                {
                    m_state = e_RPCState.Connected;

                    ReadyMessage ready = a_event.GetObject <ReadyMessage>();

                    m_config = ready.Configuration;

                    User user = ready.User;

                    user.Configuration = ready.Configuration;

                    ready.User = user;
                    m_user     = user;

                    EnqueueMessage(ready);

                    return;
                }
            }

            // TODO: Implement Connected Commands
            if (m_state == e_RPCState.Connected)
            {
                switch (a_event.Command)
                {
                case e_Command.Dispatch:
                {
                    break;
                }
                }
            }
        }
        private void ProcessDispatch(EventPayload response)
        {
            if (response.Command != Command.Dispatch)
            {
                return;
            }
            ServerEvent?nullable = response.Event;

            if (!nullable.HasValue)
            {
                return;
            }
            nullable = response.Event;
            switch (nullable.Value)
            {
            case ServerEvent.ActivityJoin:
                this.EnqueueMessage((IMessage)response.GetObject <JoinMessage>());
                break;

            case ServerEvent.ActivitySpectate:
                this.EnqueueMessage((IMessage)response.GetObject <SpectateMessage>());
                break;

            case ServerEvent.ActivityJoinRequest:
                this.EnqueueMessage((IMessage)response.GetObject <JoinRequestMessage>());
                break;

            default:
                ILogger  logger   = this.Logger;
                object[] objArray = new object[1];
                nullable    = response.Event;
                objArray[0] = (object)nullable.Value;
                logger.Warning("Ignoring {0}", objArray);
                break;
            }
        }
예제 #4
0
 public static bool TryParseResponse(string json, out RichPresence response)
 {
     try
     {
         EventPayload eventPayload = JsonConvert.DeserializeObject <EventPayload>(json);
         if (eventPayload != null)
         {
             response = (RichPresence)eventPayload.GetObject <RichPresenceResponse>();
             return(true);
         }
     }
     catch (Exception ex)
     {
     }
     response = (RichPresence)null;
     return(false);
 }
예제 #5
0
        /// <summary>
        /// Attempts to parse the response of a Web Request to a rich presence
        /// </summary>
        /// <param name="json">The json data received by the client</param>
        /// <param name="response">The parsed rich presence</param>
        /// <returns>True if the parse was succesfull</returns>
        public static bool TryParseResponse(string json, out RichPresence response)
        {
            try
            {
                //Try to parse the JSON into a event
                EventPayload ev = JsonConvert.DeserializeObject <EventPayload>(json);

                //We have a result, so parse the rich presence response and return it.
                if (ev != null)
                {
                    //Parse the response into a rich presence response
                    response = ev.GetObject <RichPresenceResponse>();
                    return(true);
                }
            }catch (Exception) { }

            //We failed.
            response = null;
            return(false);
        }
        /// <summary>Handles the response from the pipe and calls appropriate events and changes states.</summary>
        /// <param name="response">The response received by the server.</param>
        private void ProcessFrame(EventPayload response)
        {
            Logger.Info("Handling Response. Cmd: {0}, Event: {1}", response.Command, response.Event);

            //Check if it is an error
            if (response.Event.HasValue && response.Event.Value == ServerEvent.Error)
            {
                //We have an error
                Logger.Error("Error received from the RPC");

                //Create the event objetc and push it to the queue
                ErrorMessage err = response.GetObject <ErrorMessage>();
                Logger.Error("Server responded with an error message: ({0}) {1}", err.Code.ToString(), err.Message);

                //Enqueue the messsage and then end
                EnqueueMessage(err);
                return;
            }

            //Check if its a handshake
            if (State == RpcState.Connecting)
            {
                if (response.Command == Command.Dispatch && response.Event.HasValue && response.Event.Value == ServerEvent.Ready)
                {
                    Logger.Info("Connection established with the RPC");
                    SetConnectionState(RpcState.Connected);
                    delay.Reset();

                    //Prepare the object
                    ReadyMessage ready = response.GetObject <ReadyMessage>();
                    lock (l_config)
                    {
                        _configuration = ready.Configuration;
                        ready.User.SetConfiguration(_configuration);
                    }

                    //Enqueue the message
                    EnqueueMessage(ready);
                    return;
                }
            }

            if (State == RpcState.Connected)
            {
                switch (response.Command)
                {
                //We were sent a dispatch, better process it
                case Command.Dispatch:
                    ProcessDispatch(response);
                    break;

                //We were sent a Activity Update, better enqueue it
                case Command.SetActivity:
                    if (response.Data == null)
                    {
                        EnqueueMessage(new PresenceMessage());
                    }
                    else
                    {
                        RichPresenceResponse rp = response.GetObject <RichPresenceResponse>();
                        EnqueueMessage(new PresenceMessage(rp));
                    }
                    break;

                case Command.Unsubscribe:
                case Command.Subscribe:

                    //Prepare a serializer that can account for snake_case enums.
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Converters.Add(new Converters.EnumSnakeCaseConverter());

                    //Go through the data, looking for the evt property, casting it to a server event
                    var evt = response.GetObject <EventPayload>().Event.Value;

                    //Enqueue the appropriate message.
                    if (response.Command == Command.Subscribe)
                    {
                        EnqueueMessage(new SubscribeMessage(evt));
                    }
                    else
                    {
                        EnqueueMessage(new UnsubscribeMessage(evt));
                    }

                    break;


                case Command.SendActivityJoinInvite:
                    Logger.Info("Got invite response ack.");
                    break;

                case Command.CloseActivityJoinRequest:
                    Logger.Info("Got invite response reject ack.");
                    break;

                //we have no idea what we were sent
                default:
                    Logger.Error("Unkown frame was received! {0}", response.Command);
                    return;
                }
                return;
            }

            Logger.Info("Received a frame while we are disconnected. Ignoring. Cmd: {0}, Event: {1}", response.Command, response.Event);
        }
예제 #7
0
        /// <summary>Handles the response from the pipe and calls appropriate events and changes states.</summary>
        /// <param name="response">The response received by the server.</param>
        private void ProcessFrame(EventPayload response)
        {
            Console.WriteLine($"Handling Response. Cmd: {response.Command}, Event: {response.Event}");

            //Check if it is an error
            if (response.Event.HasValue && response.Event.Value == ServerEvent.Error)
            {
                //We have an error
                Console.WriteLine("Error received from the RPC");

                //Create the event objetc and push it to the queue
                ErrorMessage err = response.GetObject <ErrorMessage>();
                Console.WriteLine($"Server responded with an error message: ({err.Code.ToString()}) {err.Message}");

                //Enqueue the messsage and then end
                EnqueueMessage(err);
                return;
            }

            //Check if its a handshake
            if (State == RpcState.Connecting)
            {
                if (response.Command == Command.Dispatch && response.Event.HasValue && response.Event.Value == ServerEvent.Ready)
                {
                    Console.WriteLine("Connection established with the RPC");
                    SetConnectionState(RpcState.Connected);
                    delay.Reset();

                    //Prepare the object
                    ReadyMessage ready = response.GetObject <ReadyMessage>();
                    lock (l_config)
                    {
                        _configuration = ready.Configuration;
                        ready.User.SetConfiguration(_configuration);
                    }

                    //Enqueue the message
                    EnqueueMessage(ready);
                    return;
                }
            }

            if (State == RpcState.Connected)
            {
                switch (response.Command)
                {
                //We were sent a Activity Update, better enqueue it
                case Command.SetActivity:
                    if (response.Data == null)
                    {
                        EnqueueMessage(new PresenceMessage());
                    }
                    else
                    {
                        RichPresenceResponse rp = response.GetObject <RichPresenceResponse>();
                        EnqueueMessage(new PresenceMessage(rp));
                    }
                    break;


                //we have no idea what we were sent
                default:
                    Console.WriteLine($"Unkown frame was received! {response.Command}");
                    return;
                }
                return;
            }

            Console.WriteLine($"Received a frame while we are disconnected. Ignoring. Cmd: {response.Command}, Event: {response.Event}");
        }
        private void ProcessFrame(EventPayload response)
        {
            this.Logger.Info("Handling Response. Cmd: {0}, Event: {1}", (object)response.Command, (object)response.Event);
            ServerEvent?nullable;

            if (response.Event.HasValue)
            {
                nullable = response.Event;
                if (nullable.Value == ServerEvent.Error)
                {
                    this.Logger.Error("Error received from the RPC");
                    ErrorMessage errorMessage = response.GetObject <ErrorMessage>();
                    this.Logger.Error("Server responded with an error message: ({0}) {1}", (object)errorMessage.Code.ToString(), (object)errorMessage.Message);
                    this.EnqueueMessage((IMessage)errorMessage);
                    return;
                }
            }
            if (this.State == RpcState.Connecting && response.Command == Command.Dispatch)
            {
                nullable = response.Event;
                if (nullable.HasValue)
                {
                    nullable = response.Event;
                    if (nullable.Value == ServerEvent.Ready)
                    {
                        this.Logger.Info("Connection established with the RPC");
                        this.SetConnectionState(RpcState.Connected);
                        this.delay.Reset();
                        ReadyMessage readyMessage = response.GetObject <ReadyMessage>();
                        lock (this.l_config)
                        {
                            this._configuration = readyMessage.Configuration;
                            readyMessage.User.SetConfiguration(this._configuration);
                        }
                        this.EnqueueMessage((IMessage)readyMessage);
                        return;
                    }
                }
            }
            if (this.State == RpcState.Connected)
            {
                switch (response.Command)
                {
                case Command.Dispatch:
                    this.ProcessDispatch(response);
                    break;

                case Command.SetActivity:
                    if (response.Data == null)
                    {
                        this.EnqueueMessage((IMessage) new PresenceMessage());
                        break;
                    }
                    this.EnqueueMessage((IMessage) new PresenceMessage(response.GetObject <RichPresenceResponse>()));
                    break;

                case Command.Subscribe:
                case Command.Unsubscribe:
                    new JsonSerializer().Converters.Add((JsonConverter) new EnumSnakeCaseConverter());
                    nullable = response.GetObject <EventPayload>().Event;
                    ServerEvent evt = nullable.Value;
                    if (response.Command == Command.Subscribe)
                    {
                        this.EnqueueMessage((IMessage) new SubscribeMessage(evt));
                        break;
                    }
                    this.EnqueueMessage((IMessage) new UnsubscribeMessage(evt));
                    break;

                case Command.SendActivityJoinInvite:
                    this.Logger.Info("Got invite response ack.");
                    break;

                case Command.CloseActivityJoinRequest:
                    this.Logger.Info("Got invite response reject ack.");
                    break;

                default:
                    this.Logger.Error("Unkown frame was received! {0}", (object)response.Command);
                    break;
                }
            }
            else
            {
                this.Logger.Info("Received a frame while we are disconnected. Ignoring. Cmd: {0}, Event: {1}", (object)response.Command, (object)response.Event);
            }
        }