static Task LaneCaching() { return(Task.WhenAll(Enumerable.Range(0, Sockets).Select(async x => { var builder = new ClientBuilder().UseSockets().Build(); var connection = builder.ConnectAsync(new IPEndPoint(IPAddress.Parse(Destination), 11211)).GetAwaiter().GetResult(); await using (var client = new MemcachedProtocol(new LowLatencyMessageProtocol <MemcachedRequest, IMemcachedResponse>(connection, new MemcachedMessageWriter(), new MemcachedMessageReader()))) { client.Start(); var list = new List <Task>(); for (var i = 0; i < Threads; i++) { list.Add(Task.Run(async() => { await client.Set(Key, Value, null); foreach (var batch in Enumerable.Range(0, Iterations).Batch(200)) { await Task.WhenAll(batch.Select(x => client.Get <string, StringResponseDeserializer>(Key, new StringResponseDeserializer()))); } })); } await Task.WhenAll(list); } }))); }
private static async Task MemcachedProtocol(IServiceProvider serviceProvider) { var loggerFactory = LoggerFactory.Create(builder => { builder.SetMinimumLevel(LogLevel.Error); builder.AddConsole(); }); var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging(loggerFactory: loggerFactory) .Build(); var ipAddress = IPAddress.Parse("127.0.0.1"); var connection = await client.ConnectAsync(new IPEndPoint(ipAddress, 11211)); MemcachedProtocol memcachedProtocol = new MemcachedProtocol(connection); await memcachedProtocol.Set("Hello", Encoding.UTF8.GetBytes("World"), TimeSpan.FromMinutes(30)); var checkSet = await memcachedProtocol.Get("Hello"); Console.WriteLine($"checkSet result :{Encoding.UTF8.GetString(checkSet)}"); await memcachedProtocol.Replace("Hello", Encoding.UTF8.GetBytes("World replaced"), TimeSpan.FromMinutes(30)); var checkReplace = await memcachedProtocol.Get("Hello"); Console.WriteLine($"checkReplace result :{Encoding.UTF8.GetString(checkReplace)}"); }
private static async Task CustomProtocol(IServiceProvider serviceProvider) { var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging() .Build(); await using var connection = await client.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5005)); Console.WriteLine($"Connected to {connection.LocalEndPoint}"); var protocol = new LengthPrefixedProtocol(); var reader = connection.CreateReader(); var writer = connection.CreateWriter(); while (true) { var line = Console.ReadLine(); await writer.WriteAsync(protocol, new Message(Encoding.UTF8.GetBytes(line))); var result = await reader.ReadAsync(protocol); if (result.IsCompleted) { break; } reader.Advance(); } }
private static async Task EchoServerWithTls(ServiceProvider serviceProvider) { var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging() .UseClientTls(options => { options.OnAuthenticateAsClient = (connection, o) => { o.TargetHost = "foo"; }; options.LocalCertificate = new X509Certificate2("testcert.pfx", "testcert"); // NOTE: Do not do this in a production environment options.AllowAnyRemoteCertificate(); }) .Build(); var connection = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 5004)); Console.WriteLine($"Connected to {connection.LocalEndPoint}"); Console.WriteLine("Echo server running, type into the console"); var reads = Console.OpenStandardInput().CopyToAsync(connection.Transport.Output); var writes = connection.Transport.Input.CopyToAsync(Stream.Null); await reads; await writes; }
private static async Task InMemoryEchoTransport(IServiceProvider serviceProvider) { var memoryTransport = new MemoryTransport(); var client = new ClientBuilder(serviceProvider) .UseConnectionFactory(memoryTransport) .UseConnectionLogging("Client") .Build(); var server = new ServerBuilder(serviceProvider) .Listen(endPoint: null, memoryTransport, builder => { builder.UseConnectionLogging("Server").Run(connection => connection.Transport.Input.CopyToAsync(connection.Transport.Output)); }) .Build(); await server.StartAsync(); Console.WriteLine("Started Server"); var connection = await client.ConnectAsync(endpoint : null); Console.WriteLine($"Connected to {connection.LocalEndPoint}"); Console.WriteLine("Echo server running, type into the console"); var reads = Console.OpenStandardInput().CopyToAsync(connection.Transport.Output); var writes = connection.Transport.Input.CopyToAsync(Stream.Null); await reads; await writes; await server.StopAsync(); }
private static async Task HttpClient(IServiceProvider serviceProvider) { // TODO: Missing scenarios // - HTTP/2 needs to set the ALPN parameters (hard) // - Proxy support needs to know if the connection is secure // Build the client pipeline var client = new ClientBuilder(serviceProvider) .UseSockets() .UseDnsCaching(TimeSpan.FromHours(1)) .UseConnectionLogging() .Build(); await using var connection = await client.ConnectAsync(new DnsEndPoint ("localhost", 5001)); // Use the HTTP/1.1 protocol var httpProtocol = HttpClientProtocol.CreateFromConnection(connection); while (true) { Console.Write("http1.1> "); var path = Console.ReadLine(); // Send a request (we're ignoring the response for now since it will be dumped to the console) await httpProtocol.SendAsync(new HttpRequestMessage(HttpMethod.Get, path)); } }
public async Task SetAndGetValue() { var builder = new ClientBuilder().UseSockets().Build(); await using var connection = await builder.ConnectAsync(TestConstants.MemcachedServer); await using var client = new MemcachedProtocol(new LowLatencyMessageProtocol <MemcachedRequest, IMemcachedResponse>(connection, new MemcachedMessageWriter(), new MemcachedMessageReader())); client.Start(); await client.Set("key", "value", null); var value = await client.Get <string, StringResponseDeserializer>("key", new StringResponseDeserializer()); Assert.Equal("value", value); }
private static async Task RabbitMQProtocol(IServiceProvider serviceProvider) { var loggerFactory = LoggerFactory.Create(builder => { builder.SetMinimumLevel(LogLevel.Error); builder.AddConsole(); }); var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging(loggerFactory: loggerFactory) .Build(); var ipAddress = IPAddress.Parse("127.0.0.1"); var connection = await client.ConnectAsync(new IPEndPoint(ipAddress, 5672)); var rabbitMqClientProtocol = new RabbitMQClientProtocol(connection); await rabbitMqClientProtocol.SendAsync(new RabbitMQProtocolVersionHeader()); var connectionStart = await rabbitMqClientProtocol.ReceiveAsync <ConnectionStart>(); // byte[] credentials = Encoding.UTF8.GetBytes("\0guest" + "\0guest"); await rabbitMqClientProtocol.SendAsync(new ConnectionOk(connectionStart.SecurityMechanims, new ReadOnlyMemory <byte>(credentials), connectionStart.Locale)); var connectionTune = await rabbitMqClientProtocol.ReceiveAsync <ConnectionTune>(); await rabbitMqClientProtocol.SendAsync(new ConnectionTuneOk(connectionTune.MaxChannel, connectionTune.MaxFrame, connectionTune.HeartBeat)); await rabbitMqClientProtocol.SendAsync(new ConnectionOpen(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes("/")), new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(string.Empty)), 0)); var connectionOpenOk = await rabbitMqClientProtocol.ReceiveAsync <ConnectionOpenOk>(); ushort channelId = 1; await rabbitMqClientProtocol.SendAsync(new ChannelOpen(new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes(string.Empty)), channelId)); var channelOpenOk = await rabbitMqClientProtocol.ReceiveAsync <ChannelOpenOk>(); await rabbitMqClientProtocol.SendAsync(new QueueDeclare(channelId, 0, "queue_test")); var queueDeclareOk = await rabbitMqClientProtocol.ReceiveAsync <QueueDeclareOk>(); await rabbitMqClientProtocol.SendAsync(new QueueDelete(channelId, 0, "queue_test")); var queueDeleteOk = await rabbitMqClientProtocol.ReceiveAsync <QueueDeleteOk>(); }
public async Task ConnectAsync(EndPoint endpoint, IServiceProvider serviceProvider) { if (serviceProvider is null) { throw new ArgumentNullException(nameof(serviceProvider)); } var client = new ClientBuilder(serviceProvider) .UseSockets() //.UseConnectionLogging() .Build(); var conn = await client.ConnectAsync(endpoint); nats = new NatsClientProtocol(conn, this); }
public async Task StartAsync(CancellationToken cancellationToken) { await Task.Yield(); var client = new ClientBuilder(_serviceProvider) .UseSockets() .UseConnectionLogging("Client") .Build(); _connection = await client.ConnectAsync(new IPEndPoint(IPAddress.IPv6Loopback, 530), _hostApplicationLifetime.ApplicationStopping); if (_connection == null) { return; } var protocol = new MyClientProtocol(_connection); while (!_hostApplicationLifetime.ApplicationStopping.IsCancellationRequested) { await protocol.SendAsync(new InitMessage("1"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload0"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload1"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload2"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload3"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload4"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload5"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload6"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload7"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload8"), _hostApplicationLifetime.ApplicationStopping); await protocol.SendAsync(new PayloadMessage("1", "0", "Payload9"), _hostApplicationLifetime.ApplicationStopping); Thread.Sleep(100); } }
static async Task Main(string[] args) { Console.WriteLine("Hello World!"); // hit ctrl-C to close/exit var cts = new CancellationTokenSource(); Console.CancelKeyPress += (_, e) => { Console.WriteLine("cancelled..."); cts.Cancel(); e.Cancel = true; }; var sp = ConfigureServices(); var client = new ClientBuilder(sp) .UseSockets() //.UseConnectionLogging() .Build(); var conn = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 4222)); var nats = new NatsClient(); await nats.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 4222), sp); // test this with "pub test2 2\r\nhi" from telnet nats.Sub("test2", "1", msg => { var text = Encoding.UTF8.GetString(msg.Data.Span); Console.WriteLine($"OnMsg: subject:{msg.Subject} sid:{msg.Sid} replyto:{msg.ReplyTo} text:{text}"); }); // test this with "sub test1 1" from telnet while (!cts.Token.IsCancellationRequested) { Console.WriteLine("pub..."); nats.Pub("test1", Encoding.UTF8.GetBytes("hello")); await Task.Delay(2000); } Console.WriteLine("done..."); Console.ReadLine(); }
private static async Task EchoServer(IServiceProvider serviceProvider) { var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging() .Build(); var connection = await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 5000)); Console.WriteLine($"Connected to {connection.LocalEndPoint}"); Console.WriteLine("Echo server running, type into the console"); var reads = Console.OpenStandardInput().CopyToAsync(connection.Transport.Output); var writes = connection.Transport.Input.CopyToAsync(Stream.Null); await reads; await writes; }
private static async Task MemcachedProtocol(IServiceProvider serviceProvider) { var loggerFactory = LoggerFactory.Create(builder => { builder.SetMinimumLevel(LogLevel.Debug); builder.AddConsole(); }); var client = new ClientBuilder(serviceProvider) .UseSockets() /* .UseConnectionLogging(loggerFactory: loggerFactory)*/ .Build(); var ipAddress = IPAddress.Parse("127.0.0.1"); var connection = await client.ConnectAsync(new IPEndPoint(ipAddress, 11211)); MemcachedProtocol memcachedProtocol = new MemcachedProtocol(connection); var largeString = String.Empty; var tasks = new List <Task <byte[]> >(); for (int i = 0; i < 10; i++) { largeString += i + "_"; } //await memcachedProtocol.Set("Hello", Encoding.UTF8.GetBytes(largeString), TimeSpan.FromMinutes(30)); /*for (int i = 0; i < 10; i++) * { * tasks.Add(memcachedProtocol.Get("Hello")); * } * * await memcachedProtocol.Set("Hello", Encoding.UTF8.GetBytes("World!!"), TimeSpan.FromMinutes(30)); * await Task.WhenAll(tasks); * * foreach (var item in tasks) * { * Console.WriteLine(Encoding.UTF8.GetString(item.Result)); * Console.WriteLine("-------------------------------"); * } */ Console.WriteLine("Complete"); //var byteResult = await memcachedProtocol.Get("Hello"); //var result = Encoding.UTF8.GetString(byteResult); }
private static async Task HttpClient(IServiceProvider serviceProvider) { // TODO: Missing scenarios // - HTTP/2 needs to set the ALPN parameters (hard) // - Proxy support needs to know if the connection is secure // Build the client pipeline var client = new ClientBuilder(serviceProvider) .UseSockets() .UseDnsCaching(TimeSpan.FromHours(1)) .UseConnectionLogging() .Build(); await using var connection = await client.ConnectAsync(new IPEndPoint (IPAddress.Loopback, 5001)); // Use the HTTP/1.1 protocol var httpProtocol = new HttpClientProtocol(connection); while (true) { Console.Write("http1.1> "); var path = Console.ReadLine(); if (path == null) { break; } if (path == string.Empty) { path = "/"; } var request = new HttpRequestMessage(HttpMethod.Get, path); request.Headers.Host = "localhost"; var response = await httpProtocol.SendAsync(request); await response.Content.CopyToAsync(Console.OpenStandardOutput()); Console.WriteLine(); } }
public async Task DeleteAKey() { var builder = new ClientBuilder().UseSockets().Build(); await using var connection = await builder.ConnectAsync(TestConstants.MemcachedServer); await using var client = new MemcachedProtocol(new LowLatencyMessageProtocol <MemcachedRequest, IMemcachedResponse>(connection, new MemcachedMessageWriter(), new MemcachedMessageReader())); client.Start(); var key = "ToDelete"; await client.Set(key, "value", null); var value = await client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer()); Assert.Equal("value", value); await client.Delete(key); await Assert.ThrowsAsync <KeyNotFoundException>(() => client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer())); }
public async Task ConnectAsync() { var endpoint = new IPEndPoint(IPAddress.Loopback, 6379); _muxer = await ConnectionMultiplexer.ConnectAsync(new ConfigurationOptions { EndPoints = { endpoint } }); _server = _muxer.GetServer(endpoint); SERedis(); await SERedisAsync(); var serviceProvider = new ServiceCollection().BuildServiceProvider(); var client = new ClientBuilder(serviceProvider) .UseSockets() .Build(); _connection = await client.ConnectAsync(endpoint); _bedrock = new RespBedrockProtocol(_connection); Bedrock(); await BedrockAsync(); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketConnection.SetRecommendedClientOptions(socket); socket.Connect(endpoint); _socket = RespConnection.Create(socket); Socket(); await SocketAsync(); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketConnection.SetRecommendedClientOptions(socket); socket.Connect(endpoint); _stream = RespConnection.Create(new NetworkStream(socket)); Stream(); await StreamAsync(); }
private static async Task DockerDaemon(IServiceProvider serviceProvider) { var client = new ClientBuilder(serviceProvider) .UseConnectionFactory(new NamedPipeConnectionFactory()) .UseConnectionLogging() .Build(); await using var connection = await client.ConnectAsync(new NamedPipeEndPoint ("docker_engine")); // Use the HTTP/1.1 protocol var httpProtocol = new HttpClientProtocol(connection); while (true) { // Console.Write("http1.1> "); var path = Console.ReadLine(); if (path == null) { break; } // Console.WriteLine(); if (path == string.Empty) { path = "/"; } var request = new HttpRequestMessage(HttpMethod.Get, path); request.Headers.Host = "localhost"; var response = await httpProtocol.SendAsync(request); await response.Content.CopyToAsync(Console.OpenStandardOutput()); Console.WriteLine(); } }
public async Task ConnectAsync() { foreach (var host in _initialBrokers) { _connections.TryAdd(host, await makeConnection(host)); } _logger.LogInformation("Starting Kafka client."); async Task <ConnectionContext> makeConnection(DnsEndPoint h) { var client = new ClientBuilder(_serviceProvider) .UseSockets() .UseConnectionLogging() .Build(); var connection = await client.ConnectAsync(h); _logger.LogInformation($"Connected to {connection.LocalEndPoint}"); return(connection); }; }
private async Task RunLaneCache(Func <ConnectionContext, IMessageProtocol <MemcachedRequest, IMemcachedResponse> > protocol) { var builder = new ClientBuilder().UseSockets().Build(); var connection = await builder.ConnectAsync(new IPEndPoint(IPAddress.Parse("10.0.0.21"), 11211)); await using var client = new MemcachedProtocol(protocol(connection)); client.Start(); var list = new List <Task>(); for (var i = 0; i < Threads; i++) { list.Add(Task.Run(async() => { await client.Set(Key, Value, null); foreach (var batch in Enumerable.Range(0, Iterations).Batch(200)) { await Task.WhenAll(batch.Select(x => client.Get <string, StringResponseDeserializer>(Key, new StringResponseDeserializer()))); } })); } await Task.WhenAll(list); }
public async Task ReplaceValue() { var builder = new ClientBuilder().UseSockets().Build(); await using var connection = await builder.ConnectAsync(TestConstants.MemcachedServer); await using var client = new MemcachedProtocol(new LowLatencyMessageProtocol <MemcachedRequest, IMemcachedResponse>(connection, new MemcachedMessageWriter(), new MemcachedMessageReader())); client.Start(); var key = "ToReplace"; await client.Set(key, "firstValue", null); var value = await client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer()); Assert.Equal("firstValue", value); await client.Replace(new StringWritable(key), new StringWritable("newValue"), null); var replaced = await client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer()); Assert.Equal("newValue", replaced); }
public async Task ExpireQuickly() { var builder = new ClientBuilder().UseSockets().Build(); await using var connection = await builder.ConnectAsync(TestConstants.MemcachedServer); await using var client = new MemcachedProtocol(new LowLatencyMessageProtocol <MemcachedRequest, IMemcachedResponse>(connection, new MemcachedMessageWriter(), new MemcachedMessageReader())); client.Start(); var key = "ToExpire"; var expirationTime = TimeSpan.FromSeconds(2); await client.Set(key, "firstValue", expirationTime); var value = await client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer()); Assert.Equal("firstValue", value); // memcached expires entries on 1-second boundaries, so setting this to n+1 seconds expiration may cause the test to fail some of the time await Task.Delay(expirationTime.Add(TimeSpan.FromSeconds(2))); await Assert.ThrowsAsync <KeyNotFoundException>(() => client.Get <string, StringResponseDeserializer>(key, new StringResponseDeserializer())); }
static async Task Main(string[] args) { var serviceProvider = new ServiceCollection().AddLogging(builder => { builder.SetMinimumLevel(LogLevel.Debug); builder.AddConsole(); }) .BuildServiceProvider(); var loggerFactory = LoggerFactory.Create(builder => { builder.SetMinimumLevel(LogLevel.Error); builder.AddConsole(); }); var client = new ClientBuilder(serviceProvider) .UseSockets() .UseConnectionLogging(loggerFactory: loggerFactory) .Build(); var ipAddress = IPAddress.Parse("127.0.0.1"); var connection = await client.ConnectAsync(new IPEndPoint(ipAddress, 6379)); Console.WriteLine($"Connected to {connection.LocalEndPoint}"); var redis = new RedisProtocol(connection); //Console.WriteLine("Connected to Redis, type into the console"); //var reads = Console.OpenStandardInput().CopyToAsync(connection.Transport.Output); var writes = connection.Transport.Input.CopyToAsync(Console.OpenStandardOutput()); Console.WriteLine("----------- Connection Commands ---------------"); await redis.SendAsync(new AuthCommand("password")); await redis.SendAsync(new AuthCommand("Matthew", "password")); await redis.SendAsync(new ClientGetNameCommand()); await redis.SendAsync(new ClientIdCommand()); await redis.SendAsync(new ClientListCommand()); await redis.SendAsync(new EchoCommand("Hello o o o!")); await redis.SendAsync(new SelectCommand(1)); await redis.SendAsync(new ClientSetNameCommand("MyClient")); await redis.SendAsync(new ClientListCommand()); await redis.SendAsync(new PingCommand()); await redis.SendAsync(new PingCommand("Hello World!")); Console.WriteLine("----------- Key Commands ---------------"); await redis.SendAsync(new KeysCommand("*")); //await reads; await redis.SendAsync(new QuitCommand()); await writes; }