Exemple #1
0
        public Server()
        {
            _server = ServerBuilder.CreateAsync(new IPEndPoint(IPAddress.Parse("11.11.11.10"), 12345))
                      .WithCommand("ECHO")
                      .ExpectsNoData()
                      .HandledBy(r => Response.Create("OK").WithArguments(r.Arguments))
                      .Register()
                      .WithCommand("REPEAT")
                      .ExpectsNoData()
                      .HandledBy(r => {
                var n          = int.Parse(r.Arguments[0]);
                var wat        = r.Arguments[1];
                var enumerable = Enumerable.Repeat(wat, n)
                                 .Select((x, i) => Response.Create("OK").WithArgument(i).WithArgument(x))
                                 .Concat(new[] { Response.Create("DONE") });
                return(enumerable);
            })
                      .Register()

                      // Async Handler version of ECHO
                      .WithCommand("ECHO2")
                      .HandledBy((r, c) => {
                Console.WriteLine("got ECHO");
                c(Response.Create("ECHO").WithArguments(r.Arguments));
            })
                      .Register()

                      // Async Handler version of ECHO
                      .WithCommand("REPEAT2")
                      .ExpectsData()
                      .HandledBy((r, c) => {
                var n          = int.Parse(r.Arguments[0]);
                var wat        = r.Arguments[1];
                var enumerator = Enumerable.Repeat(wat, n).Select((X, I) => new { X, I }).GetEnumerator();
                Action f       = null;
                f = () => {
                    if (!enumerator.MoveNext())
                    {
                        c(Response.Create("DONE"), null);
                        return;
                    }
                    var v = enumerator.Current;
                    c(Response.Create("OK").WithArgument(v.I).WithArgument(v.X), f);
                };
                f();
            })
                      .Register()
                      .WithDefaultHandler(r => {
                Console.WriteLine("Got '{0}'", r.Command);
                return(Response.Create("WAT"));
            })
                      .Build();
        }
 public DungeonServer(IPEndPoint endpoint, ILog log)
 {
     _log    = log;
     _house  = new TheHouse();
     _server = ServerBuilder.CreateAsync(endpoint)
               .WithCommand("JOIN").ExpectsData().HandledBy(r => {
         var name   = Encoding.ASCII.GetString(r.Data);
         var player = _house.Join(name);
         _playerConnectionLookup[r.Client] = player;
         _log.Debug("server: '{0}' joined as '{1}'", name, player.Name);
         return(Response.Create("OK").WithData(Encode(name)));
     }).Register()
               .WithCommand("LEAVE").ExpectsNoData().HandledBy(r => {
         var player = _playerConnectionLookup[r.Client];
         _house.Leave(player);
         _log.Debug("server: '{0}' left", player.Name);
         return(Response.Create("OK"));
     }).Register()
               .WithCommand("LISTEN").ExpectsNoData().HandledBy(r => {
         var player = _playerConnectionLookup[r.Client];
         return(Response.Create("OK").WithData(Encode(string.Join("\r\n", player.Listen()))));
     }).Register()
               .WithCommand("LOOK").ExpectsNoData().HandledBy(r => {
         var player = _playerConnectionLookup[r.Client];
         return(Response.Create("OK").WithData(Encode(player.Look())));
     }).Register()
               .WithCommand("LOCATION").ExpectsNoData().HandledBy(r => {
         var player = _playerConnectionLookup[r.Client];
         return(Response.Create("OK").WithData(Encode(player.Location.Name)));
     }).Register()
               .WithCommand("GO").ExpectsNoData().HandledBy(r => {
         var player    = _playerConnectionLookup[r.Client];
         var direction = (Direction)Enum.Parse(typeof(Direction), r.Arguments[0], true);
         return(Response.Create("OK").WithData(Encode(player.Go(direction))));
     }).Register()
               .WithCommand("SAY").ExpectsData().HandledBy(r => {
         var player = _playerConnectionLookup[r.Client];
         player.Say(Encoding.ASCII.GetString(r.Data));
         return(Response.Create("OK"));
     }).Register()
               .OnClientDisconnected((id, ip) => {
         var player = _playerConnectionLookup[ip];
         player.Dispose();
         _playerConnectionLookup.Remove(ip);
     })
               .Build();
     log.Debug("started server on {0}", endpoint);
 }