Esempio n. 1
0
        public override void handleGETRequest(HttpProcessor p)
        {
            string content            = "";
            bool   ShutdownAfterQuery = false;
            string home = HomePage.Replace("{$.spec.$}", Clients.Count.ToString());

            if (p.http_url == "/")
            {
                p.writeSuccess();
                p.outputStream.WriteLine(home);
                //p.outputStream.WriteLine("<html><head><title>Ghostblade Replay Server</title></head><body><h1>ACCESS DENIED - Spectator Server</h1><p>This is the Ghostblade League of Legends Replay Server Version 1.0 .</p></body></html>");
            }
            else if (p.http_url.StartsWith("/observer-mode/rest/consumer/"))
            {
                // Replay handler

                string RequestedURL = p.http_url.Replace("/observer-mode/rest/consumer/", "");
                //  [DISABLED] Console.Clear();
                //  [DISABLED] Console.WriteLine("Requested  " + RequestedURL + "   "+DateTime.Now.ToLongTimeString());
                string client = p.socket.Client.RemoteEndPoint.ToString().Split(':')[0];
                if (!Clients.ContainsKey(client))
                {
                    Clients.Add(client, new HeroSpawn());
                }

                HeroSpawn hr = Clients[client];
                //     if (hr.IsHeroSpawned)
                //  [DISABLED] Console.Title = "Champions Spawned";

                byte[] buffer = new byte[0];

                if (RequestedURL == "version")
                {
                    buffer  = Encoding.UTF8.GetBytes(rep.Version);
                    content = "text/plain";
                }
                else if (RequestedURL == "end")
                {
                    buffer  = Encoding.UTF8.GetBytes("done");
                    content = "text/plain";
                    //  ShutdownAfterQuery = true;
                }
                string[] Params = RequestedURL.Split('/');
                if (Params.Length > 0)
                {
                    if (Params[0] == "getGameMetaData")
                    {
                        content = "application/json";
                        buffer  = rep.GetMetaData();
                    }
                    else if (Params[0] == "getLastChunkInfo")
                    {
                        ChunkInfo ci = rep.LastChunkInfo;

                        if (Params[3] != "30000" && !hr.IsHeroSpawned)
                        {
                            hr.IsHeroSpawned  = true;
                            hr.FirstChunkInfo = DateTime.Now;
                        }
                        else if (!hr.IsHeroSpawned)
                        {
                            hr.IsHeroSpawned = false;
                        }
                        content = "application/json";

                        //if (!IsHeroSpawned)
                        //    CheckHero();
                        //    buffer = Encoding.UTF8.GetBytes(ChunkInfo);
                        if (!hr.IsHeroSpawned)
                        {
                            buffer = rep.GetChunkInfo(FChunk);
                        }


                        else if (hr.IsHeroSpawned && (DateTime.Now - hr.FirstChunkInfo).TotalMilliseconds < 9000)
                        {
                            buffer = rep.GetChunkInfo(FChunk);
                        }

                        else
                        {
                            buffer = rep.GetChunkInfo(rep.LastChunkInfo);
                        }
                    }
                    else if (Params[0] == "getGameDataChunk")
                    {
                        //if (Convert.ToInt32(Params[3]) == (LastChunk - 1))
                        //    Params[3] = LastChunk.ToString();

                        buffer = rep.GetChunk(short.Parse(Params[3]));

                        if (Convert.ToInt32(Params[3]) >= LatestChunk)
                        {
                            LatestChunk += 1;
                            NextChunk   += 1;

                            if (LatestChunk >= 7)
                            {
                                if (LatestChunk % 2 == 0)
                                {
                                    LatestKeyframe += 1;
                                }
                            }
                        }
                        content = "application/octet-stream";
                    }
                    else if (Params[0] == "getKeyFrame")
                    {
                        content = "application/octet-stream";
                        buffer  = rep.GetKey(short.Parse(Params[3]));
                    }
                }

                CultureInfo provider = new CultureInfo("en-US");
                p.outputStream.WriteLine("HTTP/1.1 200 OK");
                p.outputStream.WriteLine("Content-Type: " + content);

                p.outputStream.WriteLine("Accept-Ranges: bytes");
                p.outputStream.WriteLine("Server: GhostReplay");
                p.outputStream.WriteLine("Pragma: no-cache");
                p.outputStream.WriteLine("Cash-Control: no-cache");
                p.outputStream.WriteLine("Expires: Thu, 01 Jan 1970 08:00:00 CST");
                p.outputStream.WriteLine("Content-Length: " + buffer.Length.ToString());
                p.outputStream.WriteLine("Date: " + DateTime.UtcNow.ToString("ddd, dd MM yyyy HH:mm:ss GMT", provider));
                p.outputStream.WriteLine("Connection: close");
                p.outputStream.WriteLine("");
                p.outputStream.WriteContent(buffer);
            }
            if (ShutdownAfterQuery)
            {
                Environment.Exit(0);
            }
        }
        public override void handleGETRequest(HttpProcessor p)
        {
            try
            {
                string content = "";
                // bool ShutdownAfterQuery = false;
                bool SERVED = true;
                if (Version == null)
                {
                    p.writeFailure();
                    p.outputStream.WriteLine("No Stream available");
                    return;
                }

                //string home = "Hello World";
                if (p.http_url == "/")
                {
                    p.writeSuccess();
                    StringBuilder sb = new StringBuilder();
                    foreach (KeyValuePair <string, GhostbladeStream> kp in Streams)
                    {
                        string dur = string.Format("{0:0}:{1:00}", kp.Value.CurrentReplay.GameLength.Minutes, kp.Value.CurrentReplay.GameLength.Seconds);
                        sb.AppendLine("<p>" + kp.Key + " [" + dur + "]   </p>" + "<br>Spectator Command : <pre>" + GetCmd(kp.Value.CurrentReplay) + " </pre>");
                    }
                    sb.AppendLine("<hr />");
                    string home = HomePage.Replace("{$.games.$}", sb.ToString());
                    p.outputStream.WriteLine(home);
                    //p.outputStream.WriteLine("<html><head><title>Ghostblade Replay Server</title></head><body><h1>ACCESS DENIED - Spectator Server</h1><p>This is the Ghostblade League of Legends Replay Server Version 1.0 .</p></body></html>");
                }
                else if (p.http_url.StartsWith("/observer-mode/rest/featured"))
                {
                    RiotSharp.Featured.FeaturedGames fg = GetCurrentGames();
                    byte[] buffer = new byte[0];
                    string dat    = JsonConvert.SerializeObject(fg);
                    buffer = Encoding.UTF8.GetBytes(dat);
                    p.outputStream.WriteLine("HTTP/1.1 200 OK");
                    p.outputStream.WriteLine("Content-Type: application/json");
                    p.outputStream.WriteLine("Accept-Ranges: bytes");
                    p.outputStream.WriteLine("Server: GhostReplay");
                    p.outputStream.WriteLine("Pragma: no-cache");
                    p.outputStream.WriteLine("Cash-Control: no-cache");
                    p.outputStream.WriteLine("Expires: Thu, 01 Jan 1970 08:00:00 CST");
                    p.outputStream.WriteLine("Content-Length: " + buffer.Length.ToString());
                    p.outputStream.WriteLine("Connection: close");
                    p.outputStream.WriteLine("");
                    p.outputStream.WriteContent(buffer);
                }
                else if (p.http_url.StartsWith("/observer-mode/rest/consumer/"))
                {
                    // Replay handler

                    string RequestedURL = p.http_url.Replace("/observer-mode/rest/consumer/", "");
                    //  [DISABLED] Console.Clear();
                    //  [DISABLED] Console.WriteLine("Requested  " + RequestedURL + "   "+DateTime.Now.ToLongTimeString());
                    string client = p.socket.Client.RemoteEndPoint.ToString().Split(':')[0];



                    //     if (hr.IsHeroSpawned)
                    //  [DISABLED] Console.Title = "Champions Spawned";

                    byte[] buffer = new byte[0];

                    if (RequestedURL == "version")
                    {
                        buffer  = Encoding.UTF8.GetBytes(Version);
                        content = "text/plain";
                    }
                    else if (RequestedURL == "end")
                    {
                        buffer  = Encoding.UTF8.GetBytes("done");
                        content = "text/plain";
                        //  ShutdownAfterQuery = true;
                    }
                    else if (RequestedURL == "reset")
                    {
                        ResetForClient(client);
                        buffer  = Encoding.UTF8.GetBytes("done");
                        content = "text/plain";
                        //  ShutdownAfterQuery = true;
                    }
                    string[] Params = RequestedURL.Split('/');
                    if (Params.Length > 2)
                    {
                        string platform = Params[1];
                        string gid      = Params[2];


                        if (IsStreaming(gid + "-" + platform))
                        {
                            GhostbladeStream stream = GetStream(gid + "-" + platform);
                            if (!stream.Clients.ContainsKey(client))
                            {
                                stream.Clients.Add(client, new HeroSpawn());
                            }

                            HeroSpawn hr = stream.Clients[client];
                            if (Params[0] == "getGameMetaData")
                            {
                                content = "application/json";

                                buffer = stream.CurrentReplay.GetMetaData();
                            }
                            else if (Params[0] == "getLastChunkInfo")
                            {
                                ChunkInfo ci = stream.CurrentReplay.LastChunkInfo;

                                if (Params[3] != "30000" && !hr.IsHeroSpawned)
                                {
                                    hr.IsHeroSpawned  = true;
                                    hr.FirstChunkInfo = DateTime.Now;
                                }
                                else if (!hr.IsHeroSpawned)
                                {
                                    hr.IsHeroSpawned = false;
                                }

                                content = "application/json";

                                //if (!IsHeroSpawned)
                                //    CheckHero();
                                //    buffer = Encoding.UTF8.GetBytes(ChunkInfo);
                                if (!hr.IsHeroSpawned)
                                {
                                    buffer = stream.CurrentReplay.GetChunkInfo(stream.FChunk);
                                }


                                else if (hr.IsHeroSpawned && (DateTime.Now - hr.FirstChunkInfo).TotalMilliseconds < 9000)
                                {
                                    buffer = stream.CurrentReplay.GetChunkInfo(stream.FChunk);
                                }

                                else
                                {
                                    buffer = stream.CurrentReplay.GetChunkInfo(stream.CurrentReplay.LastChunkInfo);
                                }
                            }
                            else if (Params[0] == "getGameDataChunk")
                            {
                                //if (Convert.ToInt32(Params[3]) == (LastChunk - 1))
                                //    Params[3] = LastChunk.ToString();

                                buffer = stream.CurrentReplay.GetChunk(short.Parse(Params[3]));

                                if (Convert.ToInt32(Params[3]) >= stream.LatestChunk)
                                {
                                    stream.LatestChunk += 1;
                                    stream.NextChunk   += 1;

                                    if (stream.LatestChunk >= 7)
                                    {
                                        if (stream.LatestChunk % 2 == 0)
                                        {
                                            stream.LatestKeyframe += 1;
                                        }
                                    }
                                }
                                content = "application/octet-stream";
                            }
                            else if (Params[0] == "getKeyFrame")
                            {
                                content = "application/octet-stream";
                                buffer  = stream.CurrentReplay.GetKey(short.Parse(Params[3]));
                            }
                        }
                        else
                        {
                            // no stream
                            SERVED = false;
                            buffer = Encoding.UTF8.GetBytes(Ghostblade.Properties.Resources.NOTFOUND.Replace("{$msg$}", "Stream unavailable : " + gid + "-" + platform));
                        }
                    }



                    CultureInfo provider = new CultureInfo("en-US");
                    if (SERVED && buffer != null)
                    {
                        p.outputStream.WriteLine("HTTP/1.1 200 OK");
                    }
                    else
                    {
                        content = "text/html";
                        p.outputStream.WriteLine("HTTP/1.0 404 File not found");

                        if (buffer == null)
                        {
                            buffer = Encoding.UTF8.GetBytes(Ghostblade.Properties.Resources.NOTFOUND.Replace("{$msg$}", "Not found"));
                        }
                    }
                    p.outputStream.WriteLine("Content-Type: " + content);

                    p.outputStream.WriteLine("Accept-Ranges: bytes");
                    p.outputStream.WriteLine("Server: GhostReplay");
                    p.outputStream.WriteLine("Pragma: no-cache");
                    p.outputStream.WriteLine("Cash-Control: no-cache");
                    p.outputStream.WriteLine("Expires: Thu, 01 Jan 1970 08:00:00 CST");
                    p.outputStream.WriteLine("Content-Length: " + buffer.Length.ToString());
                    p.outputStream.WriteLine("Date: " + DateTime.UtcNow.ToString("ddd, dd MM yyyy HH:mm:ss GMT", provider));
                    p.outputStream.WriteLine("Connection: close");
                    p.outputStream.WriteLine("");
                    p.outputStream.WriteContent(buffer);
                }
                //if (ShutdownAfterQuery)
                //    Environment.Exit(0);
            }
            catch (Exception ex)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(Ghostblade.Properties.Resources.NOTFOUND.Replace("{$msg$}", "Internal Error " + ex.Message));
                p.outputStream.WriteLine("HTTP/1.0 404 File not found");

                p.outputStream.WriteLine("Content-Type: " + "text/html");

                p.outputStream.WriteLine("Accept-Ranges: bytes");
                p.outputStream.WriteLine("Server: GhostReplay");
                p.outputStream.WriteLine("Pragma: no-cache");
                p.outputStream.WriteLine("Cash-Control: no-cache");
                p.outputStream.WriteLine("Expires: Thu, 01 Jan 1970 08:00:00 CST");
                p.outputStream.WriteLine("Content-Length: " + buffer.Length.ToString());
                //        p.outputStream.WriteLine("Date: " + DateTime.UtcNow.ToString("ddd, dd MM yyyy HH:mm:ss GMT", provider));
                p.outputStream.WriteLine("Connection: close");
                p.outputStream.WriteLine("");
                p.outputStream.WriteContent(buffer);
            }
        }