Esempio n. 1
0
        public static RgcPacket FromByteArray(byte[] bytes)
        {
            RgcPacket pck = new RgcPacket();
            pck.length = Convert.ToInt32(Encoding.ASCII.GetString(bytes, 0, 8));

            int index = 0;
            while (index < pck.length && bytes[8 + index] != ' ')
            {
                index++;
            }
            pck.code = Convert.ToInt32(Encoding.ASCII.GetString(bytes, 8, index));

            pck.encodedbytes = Encoding.ASCII.GetString(bytes);

            if (index < pck.length)
            {
                index = pck.encodedbytes.IndexOf(' ');
                char[] separator = { ' ' };
                string[] texts = Encoding.ASCII.GetString(bytes, index, pck.length + 8 - index).Split(separator, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < texts.Length; i++)
                {
                    pck.AddString(texts[i]);
                }
            }

            return pck;
        }
Esempio n. 2
0
        public static RgcPacket FromByteArray(byte[] bytes)
        {
            RgcPacket pck = new RgcPacket();

            pck.length = Convert.ToInt32(Encoding.ASCII.GetString(bytes, 0, 8));

            int index = 0;

            while (index < pck.length && bytes[8 + index] != ' ')
            {
                index++;
            }
            pck.code = Convert.ToInt32(Encoding.ASCII.GetString(bytes, 8, index));

            pck.encodedbytes = Encoding.ASCII.GetString(bytes);

            if (index < pck.length)
            {
                index = pck.encodedbytes.IndexOf(' ');
                char[]   separator = { ' ' };
                string[] texts     = Encoding.ASCII.GetString(bytes, index, pck.length + 8 - index).Split(separator, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < texts.Length; i++)
                {
                    pck.AddString(texts[i]);
                }
            }

            return(pck);
        }
Esempio n. 3
0
        private void ProcessPacket(byte[] data)
        {
            RgcPacket pck = RgcPacket.FromByteArray(data);

            if (_ignoredpackets.Contains(pck.Code))
            {
                return;
            }

            RgcPacket response1 = null;
            RgcPacket response2 = null;

            if (pck.Code == RGC.PPM_REQUEST_VERSION_VALIDATION)
            {
                response1 = new RgcPacketVersionValidation();
                Globals.Debug("Sending version validation...");
            }
            else if (pck.Code == RGC.BOT_REGISTRATION_REQUEST)
            {
                response1 = new RgcPacketBotRegistration();
                Globals.Debug("Sending bot registration...");
            }
            else if (pck.Code == RGC.CLIENT_VALIDATION_SUCCESS)
            {
                response1 = new RgcPacketLogin(_username, _password);
                Globals.Debug("Logging in as " + _username + "...");
            }
            else if (pck.Code == RGC.CLIENT_LOGIN_SUCCESS)
            {
                response1 = new RgcPacketChatJoinAllChannels();
                Globals.Debug("Joining all channels...");

                foreach (IRgcEventHandler handler in _handlers)
                {
                    handler.HandleLoggedIn(_username);
                }
            }
            else if (pck.Code == RGC.CLIENT_CHAT_CHANNEL_ADD)
            {
                string roomname = RgcPacket.DecodeString(pck.Strings[2]);

                foreach (IRgcEventHandler handler in _handlers)
                {
                    handler.HandleSelfJoinedRoom(pck.Strings[0], roomname);
                }
            }
            else if (pck.Code == RGC.CLIENT_USER_ADD)
            {
                int i = 2;
                while (i < pck.Strings.Count)
                {
                    i += 1; // skip ip
                    string username = RgcPacket.DecodeString(pck.Strings[i]);

                    foreach (IRgcEventHandler handler in _handlers)
                    {
                        handler.HandleJoinedRoom(pck.Strings[0], username);
                    }

                    i += 3; // skip name, level, color
                    if (i >= pck.Strings.Count)
                    {
                        break;
                    }
                    if (pck.Strings[i] == "0")
                    {
                        i += 1; // skip clan, doesn't have any
                    }
                    else
                    {
                        i += 1; // skip clan

                        if (i >= pck.Strings.Count)
                        {
                            break;
                        }

                        if (pck.Strings[i] == "0")
                        {
                            i += 1; // skip prefix, doesn't have one
                        }
                        else
                        {
                            i += 3; // skip prefix
                        }

                        if (i >= pck.Strings.Count)
                        {
                            break;
                        }

                        if (pck.Strings[i] == "0")
                        {
                            i += 1; // skip suffix, doesn't have one
                        }
                        else
                        {
                            i += 3; // skip suffix
                        }
                    }
                }
            }
            else if (pck.Code == RGC.CLIENT_USER_REM)
            {
                foreach (IRgcEventHandler handler in _handlers)
                {
                    handler.HandleLeftRoom(pck.Strings[0], RgcPacket.DecodeString(pck.Strings[1]));
                }
            }
            else if (pck.Code == RGC.CLIENT_CHAT_MESSAGE)
            {
                string message = RgcPacket.DecodeString(pck.Strings[3]);

                foreach (IRgcEventHandler handler in _handlers)
                {
                    handler.HandleMessage(pck.Strings[0], RgcPacket.DecodeString(pck.Strings[2]), message);
                }
            }
            else if (pck.Code == RGC.CLIENT_CHAT_ERROR)
            {
                string message = RgcPacket.DecodeString(pck.Strings[0]);
                Globals.Debug(message, ConsoleColor.Red);

                _connected = false;
                _stream.Close();
                _client.Close();
            }
            else
            {
                if (Globals.GetSetting("//settings/general/showwarnings") == "true")
                {
                    Globals.Debug("! Unknown packet type, code=" + pck.Code + ", length=" + pck.Length);
                }
            }

            if (response1 != null)
            {
                _stream.Write(response1.ToByteArray(), 0, response1.BytesLength());
            }
            if (response2 != null)
            {
                _stream.Write(response2.ToByteArray(), 0, response2.BytesLength());
            }
        }