示例#1
0
        private static void RemoteCompileService()
        {
            var udpClient = new UdpClient(4321);
            var thread    = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        IPEndPoint endpoint = null;
                        var bytes           = udpClient.Receive(ref endpoint);
                        var message         = Encoding.ASCII.GetString(bytes);

                        if (message == "request compiler")
                        {
                            var result = Encoding.ASCII.GetBytes("provide compiler");
                            udpClient.Send(result, result.Length, new IPEndPoint(endpoint.Address, 4321));
                            Console.WriteLine("Providing compiler services for " + endpoint.Address);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            });

            thread.IsBackground = true;
            thread.Start();

            var server = new HttpListener();

            server.Prefixes.Add("http://+:8080/");
            try
            {
                server.Start();
            }
            catch (HttpListenerException ex)
            {
                var args = "http add urlacl http://+:8080/ user=Everyone listen=yes";

                var psi = new ProcessStartInfo("netsh", args);
                psi.Verb            = "runas";
                psi.CreateNoWindow  = true;
                psi.WindowStyle     = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = true;

                Process.Start(psi).WaitForExit();

                args = "advfirewall firewall add rule name=\"Port 8080 for Protogame Remote Compiler\" dir=in action=allow protocol=TCP localport=8080";

                psi                 = new ProcessStartInfo("netsh", args);
                psi.Verb            = "runas";
                psi.CreateNoWindow  = true;
                psi.WindowStyle     = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = true;

                Process.Start(psi).WaitForExit();

                server = new HttpListener();
                server.Prefixes.Add("http://+:8080/");
                server.Start();
            }

            Console.WriteLine("Remote compiler for Protogame started on port 4321 (UDP) and port 8080 (TCP)");

            while (true)
            {
                var context  = server.GetContext();
                var request  = context.Request;
                var response = context.Response;

                Console.WriteLine("Request: " + request.RawUrl);

                string input = null;
                using (var reader = new StreamReader(request.InputStream))
                {
                    input = reader.ReadToEnd();
                }

                switch (request.Url.AbsolutePath)
                {
                case "/compileeffect":
                {
                    var platform = (TargetPlatform)Convert.ToInt32(request.QueryString["platform"]);

                    var effect = new EffectAsset(
                        null,
                        "effect",
                        input,
                        null,
                        true);

                    var compiler = new EffectAssetCompiler();
                    try
                    {
                        compiler.Compile(effect, platform);
                    }
                    catch (Exception ex)
                    {
                        response.StatusCode = 500;
                        var result = Encoding.ASCII.GetBytes(ex.Message);
                        response.ContentLength64 = result.Length;
                        response.OutputStream.Write(result, 0, result.Length);
                        response.Close();
                        break;
                    }

                    try
                    {
                        response.ContentLength64 = effect.PlatformData.Data.Length;
                        response.OutputStream.Write(effect.PlatformData.Data, 0, effect.PlatformData.Data.Length);
                        response.Close();
                    }
                    catch (HttpListenerException)
                    {
                    }

                    break;
                }

                case "/compilefont":
                {
                    var platform = (TargetPlatform)Convert.ToInt32(request.QueryString["platform"]);

                    var content    = input.Split('\0');
                    var fontName   = content[0];
                    var fontSize   = int.Parse(content[1]);
                    var spacing    = int.Parse(content[2]);
                    var useKerning = bool.Parse(content[3]);

                    var font = new FontAsset(
                        null,
                        "font",
                        fontName,
                        fontSize,
                        useKerning,
                        spacing,
                        null);

                    var compiler = new FontAssetCompiler();
                    try
                    {
                        compiler.Compile(font, platform);
                    }
                    catch (Exception ex)
                    {
                        response.StatusCode = 500;
                        var result = Encoding.ASCII.GetBytes(ex.Message);
                        response.ContentLength64 = result.Length;
                        response.OutputStream.Write(result, 0, result.Length);
                        response.Close();
                        break;
                    }

                    try
                    {
                        response.ContentLength64 = font.PlatformData.Data.Length;
                        response.OutputStream.Write(font.PlatformData.Data, 0, font.PlatformData.Data.Length);
                        response.Close();
                    }
                    catch (HttpListenerException)
                    {
                    }

                    break;
                }

                default:
                {
                    response.StatusCode = 404;
                    var result = Encoding.ASCII.GetBytes("not found");
                    response.ContentLength64 = result.Length;
                    response.OutputStream.Write(result, 0, result.Length);
                    response.Close();
                    break;
                }
                }
            }
        }
示例#2
0
        private static void RemoteCompileService()
        {
            var udpClient = new UdpClient(4321);
            var thread = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        IPEndPoint endpoint = null;
                        var bytes = udpClient.Receive(ref endpoint);
                        var message = Encoding.ASCII.GetString(bytes);

                        if (message == "request compiler")
                        {
                            var result = Encoding.ASCII.GetBytes("provide compiler");
                            udpClient.Send(result, result.Length, new IPEndPoint(endpoint.Address, 4321));
                            Console.WriteLine("Providing compiler services for " + endpoint.Address);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            });
            thread.IsBackground = true;
            thread.Start();

            var server = new HttpListener();
            server.Prefixes.Add("http://+:8080/");
            try
            {
                server.Start();
            }
            catch (HttpListenerException ex)
            {
                var args = "http add urlacl http://+:8080/ user=Everyone listen=yes";

                var psi = new ProcessStartInfo("netsh", args);
                psi.Verb = "runas";
                psi.CreateNoWindow = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = true;

                Process.Start(psi).WaitForExit();

                args = "advfirewall firewall add rule name=\"Port 8080 for Protogame Remote Compiler\" dir=in action=allow protocol=TCP localport=8080";

                psi = new ProcessStartInfo("netsh", args);
                psi.Verb = "runas";
                psi.CreateNoWindow = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = true;

                Process.Start(psi).WaitForExit();

                server = new HttpListener();
                server.Prefixes.Add("http://+:8080/");
                server.Start();
            }

            Console.WriteLine("Remote compiler for Protogame started on port 4321 (UDP) and port 8080 (TCP)");

            while (true)
            {
                var context = server.GetContext();
                var request = context.Request;
                var response = context.Response;

                Console.WriteLine("Request: " + request.RawUrl);

                string input = null;
                using (var reader = new StreamReader(request.InputStream))
                {
                    input = reader.ReadToEnd();
                }

                switch (request.Url.AbsolutePath)
                {
                    case "/compileeffect":
                    {
                        var platform = (TargetPlatform) Convert.ToInt32(request.QueryString["platform"]);

                        var effect = new EffectAsset(
                            null,
                            null,
                            null,
                            "effect",
                            input,
                            null,
                            true);

                        var compiler = new EffectAssetCompiler();
                        try
                        {
                            compiler.Compile(effect, platform);
                        }
                        catch (Exception ex)
                        {
                            response.StatusCode = 500;
                            var result = Encoding.ASCII.GetBytes(ex.Message);
                            response.ContentLength64 = result.Length;
                            response.OutputStream.Write(result, 0, result.Length);
                            response.Close();
                            break;
                        }

                        try
                        {
                            response.ContentLength64 = effect.PlatformData.Data.Length;
                            response.OutputStream.Write(effect.PlatformData.Data, 0, effect.PlatformData.Data.Length);
                            response.Close();
                        }
                        catch (HttpListenerException)
                        {
                        }

                        break;
                    }
                    case "/compilefont":
                    {
                        var platform = (TargetPlatform) Convert.ToInt32(request.QueryString["platform"]);

                        var content = input.Split('\0');
                        var fontName = content[0];
                        var fontSize = int.Parse(content[1]);
                        var spacing = int.Parse(content[2]);
                        var useKerning = bool.Parse(content[3]);

                        var font = new FontAsset(
                            null,
                            "font",
                            fontName,
                            fontSize,
                            useKerning,
                            spacing,
                            null);

                        var compiler = new FontAssetCompiler();
                        try
                        {
                            compiler.Compile(font, platform);
                        }
                        catch (Exception ex)
                        {
                            response.StatusCode = 500;
                            var result = Encoding.ASCII.GetBytes(ex.Message);
                            response.ContentLength64 = result.Length;
                            response.OutputStream.Write(result, 0, result.Length);
                            response.Close();
                            break;
                        }

                        try
                        {
                            response.ContentLength64 = font.PlatformData.Data.Length;
                            response.OutputStream.Write(font.PlatformData.Data, 0, font.PlatformData.Data.Length);
                            response.Close();
                        }
                        catch (HttpListenerException)
                        {
                        }

                        break;
                    }
                    default:
                    {
                        response.StatusCode = 404;
                        var result = Encoding.ASCII.GetBytes("not found");
                        response.ContentLength64 = result.Length;
                        response.OutputStream.Write(result, 0, result.Length);
                        response.Close();
                        break;
                    }
                }
            }
        }