internal void AddPacket(ushort playerid, GDP packet)
 {
     if (PacketStore.ContainsKey(playerid))
     {
         PacketStore[playerid].Push(packet);
     }
 }
        public static GDP Parse(byte[] data)
        {
            GDP p = new GDP();

            p.Playerid    = BitConverter.ToUInt16(data, 0);
            p.AccessToken = Encoding.UTF8.GetString(data, 2, 30);
            p.Command     = data[32];
            p.Length      = BitConverter.ToUInt16(data, 33);
            p.Data        = Utils.Extract(data, 35, p.Length);
            return(p);
        }
        internal GDP MakePacket(GDPCommand cmd, ushort playerid, string data)
        {
            GDP gdp = new GDP();

            gdp.Command     = (byte)cmd;
            gdp.Playerid    = playerid;
            gdp.AccessToken = PlayerToken(playerid);

            gdp.Data   = Encoding.UTF8.GetBytes(data);
            gdp.Length = (ushort)gdp.Data.Length;
            return(gdp);
        }
        internal GDP WaitForResponse(ushort playerid)
        {
            while (!Response.ContainsKey(playerid))
            {
                Thread.Sleep(50);
            }

            GDP gd = Response[playerid];

            Response.Remove(playerid);

            return(gd);
        }
        internal bool ProcessGameData(string data, out string resp)
        {
            resp = "OK";
            string[] chunk = GameDataSplitter.Split(data);
            if (chunk.Length > 1)
            {
                string command = chunk[chunk.Length - 1];
                if (command == "READ_KEY")
                {
                    ushort playerid = ushort.Parse(chunk[0]);
                    GDP    gdp      = MakePacket(GDPCommand.READ_KEY, playerid, "");
                    AddPacket(playerid, gdp);
                    gdp = WaitForResponse(playerid);
                    if (gdp.Command == 14)
                    {
                        resp = Encoding.UTF8.GetString(gdp.Data);
                    }
                    else
                    {
                        resp = "000";
                    }
                    return(true);
                }


                else if (command == "STREAM_P")
                {
                    ushort playerid = ushort.Parse(chunk[0]);

                    GDP gdp = MakePacket(GDPCommand.PLAY_STREAM, playerid, chunk[1] + ";" + Streamer.GetVidTime(ushort.Parse(chunk[1])).ToString());
                    AddPacket(playerid, gdp);

                    return(true);
                }
                else if (command == "STREAM_B")
                {
                    ushort playerid = ushort.Parse(chunk[0]);
                    // Start the stream from server
                    Streamer.StreamVID(ushort.Parse(chunk[0]));
                    return(true);
                }
                else if (command == "LOGIN")
                {
                    ushort playerid = ushort.Parse(chunk[0]);
                    string username = chunk[1];
                    string AT       = GenerateAccessToken();
                    AddToken(playerid, AT);
                    AddPlayer(playerid, username);
                    CreatePacketStore(playerid);
                    return(true);
                }
                else if (command == "LOGOUT")
                {
                    ushort playerid = ushort.Parse(chunk[0]);
                    if (PlayerTable.ContainsKey(playerid))
                    {
                        PlayerTable.Remove(playerid);
                    }

                    if (AccessTok.ContainsKey(playerid))
                    {
                        AccessTok.Remove(playerid);
                    }

                    if (PacketStore.ContainsKey(playerid))
                    {
                        PacketStore.Remove(playerid);
                    }
                    return(true);
                }
                else if (command == "SAY_TTS")
                {
                    ushort playerid = ushort.Parse(chunk[0]);

                    GDP gdp = MakePacket(GDPCommand.PLAY_VOICE, playerid, chunk[1]);
                    AddPacket(playerid, gdp);
                    return(true);
                }
                else if (command == "STOP_TTS")
                {
                    ushort playerid = ushort.Parse(chunk[0]);

                    GDP gdp = MakePacket(GDPCommand.STOP_VOICE, playerid, "");
                    AddPacket(playerid, gdp);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        byte[] DataReceived(byte[] data, int read)
        {
            try
            {
                GDP gdp = GDP.Parse(Utils.Extract(data, 0, read));
                switch ((GDPCommand)gdp.Command)
                {
                case GDPCommand.LOGIN:
                    string dat = Encoding.UTF8.GetString(gdp.Data);
                    // GTA UN;mac
                    string[] ld = dat.Split(';');
                    if (Login(ld[0], ld[1]) == true)
                    {
                        gdp.Command = (byte)GDPCommand.LOGGED;
                        ushort playerid = Program.GameInterface.GetPlayerID(ld[0]);
                        gdp.AccessToken = Program.GameInterface.AccessTok[playerid];
                        gdp.Length      = 30;
                        gdp.Playerid    = playerid;
                        gdp.Data        = Encoding.UTF8.GetBytes(gdp.AccessToken);

                        return(gdp.ToPacket());
                    }
                    else
                    {
                        gdp.Command  = (byte)GDPCommand.LOGIN_ERROR;
                        gdp.Length   = 3;
                        gdp.Playerid = 0;
                        gdp.Data     = Encoding.UTF8.GetBytes("DEN");

                        return(gdp.ToPacket());
                    }
                    break;

                case GDPCommand.KEYDATA:
                    // KEY_CODE
                    if (Program.GameInterface.ValidToken(gdp.Playerid, gdp.AccessToken))
                    {
                        OnGameDataArrived(gdp.Playerid, "KEY", Encoding.UTF8.GetString(gdp.Data));
                        gdp.Command  = (byte)GDPCommand.OK;
                        gdp.Length   = 2;
                        gdp.Playerid = 0;
                        gdp.Data     = Encoding.UTF8.GetBytes("OK");

                        return(gdp.ToPacket());
                    }
                    else
                    {
                        gdp.Command  = (byte)GDPCommand.ACCESS_TOKEN_ERROR;
                        gdp.Length   = 3;
                        gdp.Playerid = 0;
                        gdp.Data     = Encoding.UTF8.GetBytes("DEN");

                        return(gdp.ToPacket());
                    }
                    break;

                case GDPCommand.STATUS:
                    // STATUS
                    if (Program.GameInterface.ValidToken(gdp.Playerid, gdp.AccessToken))
                    {
                        string at = gdp.AccessToken;
                        gdp = Program.GameInterface.GetPacket(gdp.Playerid);
                        if (gdp != null)
                        {
                            return(gdp.ToPacket());
                        }
                        else
                        {
                            gdp             = new GDP();
                            gdp.AccessToken = at;
                            gdp.Command     = (byte)GDPCommand.ERROR;
                            gdp.Length      = 3;
                            gdp.Playerid    = 0;
                            gdp.Data        = Encoding.UTF8.GetBytes("ERR");

                            return(gdp.ToPacket());
                        }
                    }
                    else
                    {
                        gdp.Command  = (byte)GDPCommand.ACCESS_TOKEN_ERROR;
                        gdp.Length   = 3;
                        gdp.Playerid = 0;
                        gdp.Data     = Encoding.UTF8.GetBytes("DEN");

                        return(gdp.ToPacket());
                    }
                    break;

                case GDPCommand.RESPONSE:
                    // RESPONSE
                    if (Program.GameInterface.ValidToken(gdp.Playerid, gdp.AccessToken))
                    {
                        string at = gdp.AccessToken;
                        if (!Program.GameInterface.Response.ContainsKey(gdp.Playerid))
                        {
                            Program.GameInterface.Response.Add(gdp.Playerid, gdp);
                        }

                        gdp             = new GDP();
                        gdp.AccessToken = at;
                        gdp.Command     = (byte)GDPCommand.OK;
                        gdp.Length      = 2;
                        gdp.Playerid    = 0;
                        gdp.Data        = Encoding.UTF8.GetBytes("OK");

                        return(gdp.ToPacket());
                    }
                    else
                    {
                        gdp.Command  = (byte)GDPCommand.ACCESS_TOKEN_ERROR;
                        gdp.Length   = 3;
                        gdp.Playerid = 0;
                        gdp.Data     = Encoding.UTF8.GetBytes("DEN");

                        return(gdp.ToPacket());
                    }
                    break;

                case GDPCommand.PLAYER_CONNECTED:
                    // STATUS
                    string player = Encoding.UTF8.GetString(gdp.Data);
                    if (Program.GameInterface.GetPlayerID(player) != 999)
                    {
                        gdp.Data   = Encoding.UTF8.GetBytes("CON");
                        gdp.Length = 3;

                        return(gdp.ToPacket());
                    }
                    else
                    {
                        gdp.Data   = Encoding.UTF8.GetBytes("DIS");
                        gdp.Length = 3;

                        return(gdp.ToPacket());
                    }
                    break;
                }
            }
            catch
            {
            }
            return(Encoding.UTF8.GetBytes("ERROR"));
        }