示例#1
0
        void DiscordLoop()
        {
            bool connected = false;

            m_join = false;

            for (int i = 0; i < 10; ++i)
            {
                if (AttemptConnection(i))
                {
                    connected = true;

                    break;
                }

                if (m_shutdown)
                {
                    break;
                }
            }

            if (!connected)
            {
                InternalConsole.Error("Failed to connect to Discord");

                m_shutdown = true;
            }
            else
            {
                m_internalClient.BeginRead();

                EnqueueMessage(new ConnectionEstablishedMessage(m_connectedPipe));

                Handshake();

                while (!m_shutdown)
                {
                    Frame frame;

                    if (ReadFrame(out frame))
                    {
                        switch (frame.OpCode)
                        {
                        case Frame.e_OpCode.Close:
                        {
                            ClosePayload close = frame.GetObject <ClosePayload>();

                            InternalConsole.AddMessage("Discord Client Remotely Terminated");

                            EnqueueMessage(new CloseMessage(close.Code, close.Reason));

                            m_shutdown = true;

                            break;
                        }

                        case Frame.e_OpCode.Ping:
                        {
                            WriteFrame(new Frame(Frame.e_OpCode.Pong, frame.Data));

                            break;
                        }

                        case Frame.e_OpCode.Pong:
                        {
                            InternalConsole.Warning("Got a pong from Discord?");

                            break;
                        }

                        case Frame.e_OpCode.Frame:
                        {
                            if (m_shutdown)
                            {
                                break;
                            }

                            if (frame.Data == null)
                            {
                                InternalConsole.Error("Discord Client: No data in frame");
                            }

                            EventPayload response = frame.GetObject <EventPayload>();
                            ProcessEvent(response);

                            break;
                        }

                        default:
                        {
                            InternalConsole.Error("Discord Client: Invalid Operation");
                            m_shutdown = true;

                            break;
                        }
                        }
                    }

                    ProcessCommandQueue();
                }

                ProcessCommandQueue();
            }

            m_join = true;
        }