Beispiel #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);
 }
 private void DisconnectTest(ClacksServer server) {
     _log.Debug("creating server");
     using(server) {
         _log.Debug("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Assert.IsFalse(client.Disposed);
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("BYE"));
             _log.Debug("got response");
             Assert.AreEqual("BYE", response.Status);
             _log.Debug("checking disposed");
             var c = 0;
             while(!client.Disposed) {
                 c++;
                 if(c > 100) {
                     Assert.Fail("client was not disposed");
                 }
                 Thread.Sleep(100);
             }
         }
     }
 }
 private void Receive_large_binary_payload(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("BIN").ExpectData("OK"));
             Console.WriteLine("got response");
             Assert.AreEqual("OK", response.Status);
             Assert.AreEqual(LargeBin.Length, response.Data.Length);
             Assert.AreEqual(LargeBin, response.Data);
         }
     }
 }
 private void Send_large_binary_payload(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("BIN").WithData(LargeBin));
             Console.WriteLine("got response");
             Assert.AreEqual("OK", response.Status);
         }
     }
 }
 private void Expect_error_handler(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("FAIL"));
             Console.WriteLine("got response");
             Assert.AreEqual("ERROR", response.Status);
         }
     }
 }
 private void Receive_multi_response(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var responses = client.Exec(new MultiRequest("MULTI")
                 .WithArgument("foo")
                 .WithArgument("bar")
                 .ExpectMultiple("VALUE", false)
                 .TerminatedBy("END")
             );
             Console.WriteLine("got responses");
             Assert.AreEqual(3, responses.Count());
             var r = responses.ToArray();
             Assert.AreEqual("VALUE", r[0].Status);
             Assert.AreEqual(1, r[0].Arguments.Length);
             Assert.AreEqual("foo", r[0].Arguments[0]);
             Assert.AreEqual("VALUE", r[1].Status);
             Assert.AreEqual(1, r[1].Arguments.Length, string.Join(",", r[1].Arguments));
             Assert.AreEqual("bar", r[1].Arguments[0]);
             Assert.AreEqual("END", r[2].Status);
             Assert.AreEqual(0, r[2].Arguments.Length, string.Join(",", r[2].Arguments));
         }
     }
 }
 private void Receive_multi_binary_payload(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var responses = client.Exec(new MultiRequest("MULTI")
                 .WithArgument("foo")
                 .WithArgument("bar")
                 .ExpectMultiple("VALUE", true)
                 .TerminatedBy("END")
             );
             Console.WriteLine("got responses");
             Assert.AreEqual(3, responses.Count());
             Assert.AreEqual("VALUE", responses.ElementAt(0).Status);
             Assert.AreEqual("foo", Encoding.ASCII.GetString(responses.ElementAt(0).Data));
             Assert.AreEqual("VALUE", responses.ElementAt(1).Status);
             Assert.AreEqual("bar", Encoding.ASCII.GetString(responses.ElementAt(1).Data));
             Assert.AreEqual("END", responses.ElementAt(2).Status);
         }
     }
 }
 private void Receive_binary_payload(ClacksServer server, string payloadstring, byte[] payload) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("foo").ExpectData("OK"));
             Console.WriteLine("got response");
             Assert.AreEqual("OK", response.Status);
             Assert.AreEqual(1, response.Arguments.Length);
             Assert.AreEqual(payload.Length, response.Data.Length);
             Assert.AreEqual(payload, response.Data);
         }
     }
 }
 private void Can_send_binary_payload_with_auto_data_detection(ClacksServer server) {
     var payloadString = "blahblahblah";
     var payload = Encoding.ASCII.GetBytes(payloadString);
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("foo").WithData(payload));
             Console.WriteLine("got response");
             Assert.AreEqual("OK", response.Status);
             Assert.AreEqual(2, response.Arguments.Length);
             Assert.AreEqual(payload.Length.ToString(), response.Arguments[0]);
             Assert.AreEqual(payloadString, response.Arguments[1]);
         }
     }
 }
 private void EchoData(ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient("127.0.0.1", _port)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("ECHO").WithArgument("foo").WithArgument("bar"));
             Console.WriteLine("got response");
             Assert.AreEqual("ECHO", response.Status);
             Assert.AreEqual(new[] { "foo", "bar" }, response.Arguments);
         }
     }
 }
 private void Expect_error_handler(string errorStatus, ClacksServer server) {
     using(server) {
         Console.WriteLine("created server");
         using(var client = new ClacksClient(_endpoint)) {
             Console.WriteLine("created client");
             var response = client.Exec(new Client.Request("FAIL"));
             Console.WriteLine("got response");
             Assert.AreEqual(errorStatus, response.Status);
         }
     }
 }