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


C# Connector.RunOrDefaultAsync方法代码示例

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


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

示例1: Init

        static void Init(Settings settings, Connector conn, Action<Connector, ChanStore> initExtra)
        {
            var store = new ChanStore();
              var client = new ChatClient(settings, store, conn);

              //simple default config for now
              var dfltCfg = NetChanConfig.MakeDefault<Message>();

              //client
              var rFailT = store.PrepareClientReceiverForType(dfltCfg);
              var sFailT = store.PrepareClientSenderForType(dfltCfg);
              rFailT.PipeEx(conn, "store: receiver cache");
              sFailT.PipeEx(conn, "store: sender cache");
              conn.Register(Cmd.Send, client.BroadcastMessage);
              conn.Register(Cmd.Connect, client.Connect);
              conn.Register(Cmd.Disconnect, _ => client.Disconnect());
              conn.Register(Cmd.Join, a => {
            var args = (a.Text ?? "").Split(new []{ ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (args.Length > 2)
              conn.RunError("requires 1 or 2 arguments <?host:port> <name>".ArgSrc("join"));
            else ((Func<Func<string, string, bool>, bool>) (cont => { //I could use ifs; this is more obvious to me...
            switch (args.Length) { //host, name
              case 1:
                return cont(null, args[0]);
              case 2:
                return cont(args[0], args[1]);
              default:
                return cont(null, null);
            } // apply args^ to connect and (name if != null)
              }))((host, name) => conn.RunOrDefault(Cmd.Connect, host) && (name != null) && conn.RunOrDefault(Cmd.Name, name));
              });

              //server
              //it reregisters it's commands if started (to ones created in start fn)
              Action<CmdArg> serverStartOff = null; //~ referenced from itself
              Action<CmdArg> serverStopOff = _ => conn.RunError("not running".ArgSrc("server"));
              serverStartOff = a => {
            var portS = a.Text;
            int port;
            if (!int.TryParse(portS, out port)) {
              //couldn't parse: try default
              if (settings.DefaultServerPort == -1) {
            conn.RunError("requires port (default not allowed)".ArgSrc(Cmd.ServerStart + " " + portS));
            return;
              }
              port = settings.DefaultServerPort;
            }
            //port OK: try creating and starting server:
            try {
              var serverStore = new ChanStore();
              InitServer(settings, serverStore, conn);
              serverStore.StartServer(port);

              //created fine: reregister
              conn.Reregister(Cmd.ServerStart, _ => conn.RunError(("already running (port: " + port + ")").ArgSrc("server")));
              conn.Reregister(Cmd.ServerStop, _ => {
            try {
              serverStore.StopServer();//stop listening for new
              //kill stuff in ChanStore: this will force shut the open channels and close clients
              serverStore.CloseAll().PipeEx(conn, "server.stop (in clear)");

              conn.RunNotifySystem("stopped".ArgSrc("server"));
            } catch (Exception ex) {
              ex.PipeEx(conn, "server.stop");
            } finally {
              conn.Reregister(Cmd.ServerStop, serverStopOff); //use original handlers again
              conn.Reregister(Cmd.ServerStart, serverStartOff);
            }
              });

              conn.RunNotifySystem("started".ArgSrc("server"));
            } catch (Exception ex) {
              ex.PipeEx(conn, Cmd.ServerStart + " " + port);
            }
              };

              conn.Register(Cmd.ServerStart, serverStartOff);
              conn.Register(Cmd.ServerStop, serverStopOff);
              conn.Register(Cmd.Host, async port => {
            try {
              if (string.IsNullOrWhiteSpace(port)) {
            if (settings.DefaultServerPort == -1)
              throw new ArgumentException("requires port (default not allowed)");
            port = settings.DefaultServerPort.ToString();
              }

              if (await conn.RunOrDefaultAsync(Cmd.ServerStart, port)) {
            //~ok: problem: this is run after known, whether Cmd.Server exists, not after server started
            //: connect waits until started; ok
            // - no it doesn't: it cannot: it's elsewhere...
            await Task.Delay(50); //wait a little instead
            await conn.RunOrDefaultAsync(Cmd.Connect, "localhost:" + port);
              }
            } catch (Exception ex) {
              ex.PipeEx(conn, Cmd.Host + " " + port);
            }
              });

              //translate text to send
              conn.Register(Cmd.Text, a => {
//.........这里部分代码省略.........
开发者ID:Maartyl,项目名称:cs-chan,代码行数:101,代码来源:GuiChat.cs


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