Example #1
0
    void OnPostStreamInfo(HttpListenerRequest req, HttpListenerResponse res, string channel, string streamName)
    {
        if (!channels.Contains(channel))
        {
            OnError(req, res, "404");
            return;
        }

        string inString = GetText(req);

        if (inString == null)
        {
            OnError(req, res, "400");
            return;
        }
        StreamingMesh.StreamInfo streamInfo = JsonUtility.FromJson <StreamingMesh.StreamInfo>(inString);
        streamInfo.name = url + "channels/" + channel + "/" + streamInfo.name;

        Debug.Log(streamInfo.name);

        if (Directory.Exists(Path.Combine("channels", channel)))
        {
            string stmjPath = Path.Combine(Path.Combine("channels", channel), "streaminfo.stmj");
            File.AppendAllText(stmjPath, JsonUtility.ToJson(streamInfo) + Environment.NewLine);
            if (lastMinOnly)
            {
                List <string> lines = new List <string>(File.ReadAllLines(stmjPath));
                while (lines.Count > 6)
                {
                    lines.RemoveAt(0);
                }
                File.WriteAllLines(stmjPath, lines.ToArray());
            }
        }
        else
        {
            OnError(req, res, "400");
            return;
        }

        string outString = "{\"stat\":\"ok\"}";

        res.StatusCode  = (int)HttpStatusCode.OK;
        res.ContentType = MediaTypeNames.Text.Plain;

        SendText(outString, res);
    }
Example #2
0
    void OnPost(HttpListenerRequest req, HttpListenerResponse res)
    {
        string[] path = ParseURLToPath(req.RawUrl);
        if (path[0] != "channels" || path[1] == null)
        {
            OnError(req, res, "400");
            return;
        }

        NameValueCollection queries = req.QueryString;

        if (queries.Count == 0)
        {
            OnError(req, res, "400");
            return;
        }
        if (queries["auth"] != null)
        {
            if (channels.Contains(path[1]))
            {
                int    index    = 0;
                string channel  = path[1];
                string authCode = auths[channels.IndexOf(path[1])];

                if (queries["auth"] == authCode && queries["streaminfo"] != null)
                {
                    //OnPostStreamInfo(req, res, channel, queries["streaminfo"]);
                    OnPostData <string>(req, res, path[1], new Func <string, bool>((data) => {
                        if (Directory.Exists(Path.Combine("channels", channel)))
                        {
                            StreamingMesh.StreamInfo streamInfo = JsonUtility.FromJson <StreamingMesh.StreamInfo>(data);
                            streamInfo.name = url + "channels/" + channel + "/" + streamInfo.name;

                            string stmjPath = Path.Combine(Path.Combine("channels", channel), "streaminfo.stmj");
                            if (initStream == false)
                            {
                                if (File.Exists(stmjPath))
                                {
                                    File.Delete(stmjPath);
                                }
                                initStream    = true;
                                FileStream fs = File.Create(stmjPath);
                                fs.Close();
                            }
                            File.AppendAllText(stmjPath, JsonUtility.ToJson(streamInfo).Replace(Environment.NewLine, "") + "\n");
                            if (lastMinOnly)
                            {
                                List <string> lines = new List <string>(File.ReadAllLines(stmjPath));
                                while (lines.Count > 6)
                                {
                                    lines.RemoveAt(0);
                                }
                                File.WriteAllLines(stmjPath, lines.ToArray());
                            }
                            return(true);
                        }
                        return(false);
                    }));
                    return;
                }

                if (queries["auth"] == authCode && queries["stream"] != null)
                {
                    //OnPostStream(req, res, channel, queries["stream"]);
                    OnPostData <byte[]>(req, res, channel, new Func <byte[], bool>((data) => {
                        if (Directory.Exists(Path.Combine("channels", path[1])))
                        {
                            FileWrite <byte[]>("channels", path[1], queries["stream"], data);
                            return(true);
                        }
                        return(false);
                    }));
                    return;
                }

                if (queries["auth"] == authCode && int.TryParse(queries["mesh"], out index))
                {
                    OnPostMeshInfo(req, res, path[1], index);
                    return;
                }

                if (queries["auth"] == authCode && int.TryParse(queries["material"], out index))
                {
                    OnPostMaterialInfo(req, res, path[1], index);
                    return;
                }

                if (queries["auth"] == authCode && queries["texture"] != null)
                {
                    OnPostTexture(req, res, path[1], queries["texture"]);
                    return;
                }
            }
            OnError(req, res, "authfail");
            return;
        }

        if (queries["channel"] != null)
        {
            OnPostChannelInfo(req, res, path[1]);
            return;
        }

        OnError(req, res, "400");
    }