当前位置: 首页>>代码示例>>C#>>正文


C# Executor.Cycle方法代码示例

本文整理汇总了C#中Executor.Cycle方法的典型用法代码示例。如果您正苦于以下问题:C# Executor.Cycle方法的具体用法?C# Executor.Cycle怎么用?C# Executor.Cycle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Executor的用法示例。


在下文中一共展示了Executor.Cycle方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            Config config = null;
            try {
                config = JsonConvert.DeserializeObject<Config>(File.ReadAllText("config.json"));
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Could not find config! Rename config.example.json to config.json and edit it.");
                return;
            }

            TcpClient client = new TcpClient(config.host, config.port);
            var stream = client.GetStream();
            var writer = new StreamWriter(stream);
            var reader = new StreamReader(stream);
            writer.AutoFlush = true;
            foreach (var line in config.connect)
            {
                writer.WriteLine(line);
            }

            Executor e = new Executor();
            var stdio = new IRCStdIo();
            e.Methods["stream-stdio"] = new Executor.NativeFunction(delegate (Executor exec)
            {
                exec.DataStack.Push(stdio);
            });

            while (true)
            {
                var msg = reader.ReadLine().Split(' ');
                if (msg[0] == "PING")
                {
                    msg[0] = "PONG";
                    writer.WriteLine(string.Join(" ", msg));
                }
                else if (msg[1] == "PRIVMSG")
                {
                    string channel = msg[2];
                    string message = string.Join(" ", msg.Skip(3)).Substring(1);
                    if (message.StartsWith("~{"))
                    {
                        stdio.Builder.SetLength(0);

                        e.CodeStack.Clear();
                        e.DataStack.Clear();

                        e.Methods["reset"] = new Executor.NativeFunction(delegate (Executor exec)
                        {
                            e = new Executor();
                            e.Methods["stream-stdio"] = new Executor.NativeFunction(delegate (Executor execu)
                            {
                                execu.DataStack.Push(stdio);
                            });
                        });

                        e.CodeStack.PushRange(Lexer.Parse(message.Substring(2)).Value);
                        string result;
                        int cyclecount = 0;
                        try {
                            for (cyclecount = 0; cyclecount < 12000 && e.CodeStack.Count > 0; cyclecount++)
                                e.Cycle();
                            if (e.CodeStack.Count > 0)
                                result = "[TIMED OUT]";
                            else
                                try {
                                    result = Encoding.UTF8.GetString(stdio.Builder.ToArray());
                                }
                                catch (ArgumentException)
                                {
                                    result = string.Join("", stdio.Builder.ToArray().Select(a => new string((char) a, 1)));
                                }
                        }
                        catch (Exception ex)
                        {
                            result = ex.Message;
                        }

                        result = result.Split('\r', '\n')[0];
                        if (result.Length > 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({2}) {1}", msg[2], result, cyclecount);
                        if (e.DataStack.Count > 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({2}) Stack: {1}", msg[2], e.DataStack.ToString(), cyclecount);
                        if (result.Length == 0 && e.DataStack.Count == 0)
                            writer.WriteLine("PRIVMSG {0} :\u200B({1}) [NO RESULT]", msg[2], cyclecount);
                    }
                }
            }
        }
开发者ID:puckipedia,项目名称:StackStream,代码行数:90,代码来源:Program.cs


注:本文中的Executor.Cycle方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。