static NotificationService.Client OpenConnectionToServer() { var transport = new TSocketClientTransport(IPAddress.Loopback, 9091); var protocol = new TBinaryProtocol(transport); var client = new NotificationService.Client(protocol); return(client); }
public ActorDbClient(string host = "localhost", int port = 33306, ILogger <ActorDbClient> logger = null) { _logger = logger; var tcp = new TcpClient(host, port); var transport = new TSocketClientTransport(tcp); var protocol = new TBinaryProtocol(transport); _thrift = new Actordb.Client(protocol); _cancel = new CancellationTokenSource(); }
protected override async Task <TClientTransport> AcceptImplementationAsync(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(await Task.FromCanceled <TClientTransport>(cancellationToken)); } if (_server == null) { throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket."); } try { TClientTransport tSocketTransport = null; var tcpClient = await _server.AcceptTcpClientAsync(); try { tSocketTransport = new TSocketClientTransport(tcpClient) { Timeout = _clientTimeout }; if (_useBufferedSockets) { tSocketTransport = new TBufferedClientTransport(tSocketTransport); } if (_useFramedTransport) { tSocketTransport = new TFramedClientTransport(tSocketTransport); } return(tSocketTransport); } catch (Exception) { if (tSocketTransport != null) { tSocketTransport.Dispose(); } else // Otherwise, clean it up ourselves. { ((IDisposable)tcpClient).Dispose(); } throw; } } catch (Exception ex) { throw new TTransportException(ex.ToString()); } }
private async Task <T> InvokeAsync <T>(Func <Client, Task <T> > action, CancellationToken cancellationToken) { using (var transport = new TSocketClientTransport(_host, _port)) { using (var protocol = new TBinaryProtocol(transport)) { using (var client = new Client(protocol)) { await client.OpenTransportAsync(cancellationToken); return(await action(client)); } } } }
private async Task InvokeAsync(Action <Client> action, CancellationToken cancellationToken) { using (var transport = new TSocketClientTransport(_host, _port)) { using (var protocol = new TBinaryProtocol(transport)) { using (var client = new Client(protocol)) { await client.OpenTransportAsync(cancellationToken); action(client); } } } }
public TClientTransport CreateTransport() { if (url == null) { // endpoint transport TClientTransport trans = null; if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { var cert = GetClientCert(); if (cert == null || !cert.HasPrivateKey) { throw new InvalidOperationException("Certificate doesn't contain private key"); } trans = new TTlsSocketClientTransport(host, port, 0, cert, (sender, certificate, chain, errors) => true, null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12); } else { trans = new TSocketClientTransport(host, port); } } // layered transport if (buffered) { trans = new TBufferedClientTransport(trans); } if (framed) { trans = new TFramedClientTransport(trans); } return(trans); } return(new THttpClientTransport(new Uri(url), null)); }
static async Task RunClient() { TClientTransport clientTransport = null; try { CancellationToken token = new CancellationToken(); clientTransport = new TSocketClientTransport(IPAddress.Loopback, 9090); TProtocol protocol = new TBinaryProtocol(clientTransport); var client = new THBaseService.Client(protocol); await client.OpenTransportAsync(token); byte[] table = Encoding.UTF8.GetBytes("test"); byte[] row1 = Encoding.UTF8.GetBytes("row1"); byte[] cos = Encoding.UTF8.GetBytes("a"); TGet get = new TGet(); get.Row = row1; var regs = await client.getAllRegionLocationsAsync(table, new CancellationToken()); var rlist = await client.getMultipleAsync(table, new List <TGet> { new TGet(row1) }, new CancellationToken()); TResult reslut = await client.getAsync(table, get, new CancellationToken()); //print results Console.WriteLine("RowKey:\n{0}", Encoding.UTF8.GetString(reslut.Row)); foreach (var k in reslut.ColumnValues) { Console.WriteLine("Family:Qualifier:" + "\n" + Encoding.UTF8.GetString(k.Family) + ":" + Encoding.UTF8.GetString(k.Qualifier)); Console.WriteLine("Value:" + Encoding.UTF8.GetString(k.Value)); } } catch (Exception e) { Console.WriteLine(e); } finally { clientTransport?.Close(); } }
public static void Run(int times, CancellationToken cancellationToken) { Parallel.For(0, times, i => { using (var socket = new TcpClient("localhost", 8005)) { using (var transport = new TSocketClientTransport(socket)) { using (TProtocol protocol = new TCompactProtocol(transport)) { using (var client = new IHelloWorldService.Client(protocol)) { transport.OpenAsync().Wait(); client.HelloWorldAsync(cancellationToken).Wait(); } } } } }); }
public TClientTransport CreateTransport() { if (url == null) { // endpoint transport TClientTransport trans = null; if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { var certPath = "../../keys/client.p12"; var cert = new X509Certificate2(certPath, "thrift"); trans = new TTlsSocketClientTransport(host, port, 0, cert, (o, c, chain, errors) => true, null, SslProtocols.Tls); } else { trans = new TSocketClientTransport(host, port); } } // layered transport if (buffered) { trans = new TBufferedClientTransport(trans); } if (framed) { trans = new TFramedClientTransport(trans); } return(trans); } return(new THttpClientTransport(new Uri(url), null)); }
protected override IClient CreateClient(URL url) { //实例化TheTransport //获得transport参数,用于反射实例化 var timeout = url.GetParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT); TClientTransport transport = new TSocketClientTransport(IPAddress.Parse(url.Host), url.Port, timeout); var transportKey = url.GetParameter(TRANSPORT_KEY, DEFAULT_TRANSPORT); if (TheTransportTypes != null && TheTransportTypes.ContainsKey(transportKey) && transportKey != DEFAULT_TRANSPORT) { transport = (TClientTransport)Activator.CreateInstance(TheTransportTypes[transportKey], IPAddress.Parse(url.Host), url.Port, timeout); } //获取协议 TProtocol protocol = new TBinaryProtocol(transport); //var protocolKey = url.GetParameter(PROTOCOL_KEY, DEFAULT_PROTOCOL); if (TheProtocolTypes != null && TheProtocolTypes.ContainsKey(url.Protocol) && url.Protocol != DEFAULT_PROTOCOL) { protocol = (TProtocol)Activator.CreateInstance(TheProtocolTypes[url.Protocol], transport); } var proxyKey = url.GetParameter(PROXY_KEY); if (string.IsNullOrEmpty(proxyKey) || !TheThriftClientTypes.ContainsKey(proxyKey)) { throw new RpcException($"not find the proxy thrift client{url.ToFullString()}"); } //实例化TheThriftClient var client = (IDisposable)Activator.CreateInstance(TheThriftClientTypes[proxyKey], protocol); return(new ThriftClient(transport, client, url)); }
public void Dispose() { _socket.Close(); _socket = null; _sasl.Dispose(); }
public TSaslClientTransport(TSocketClientTransport socket, string userName, string password) { _sasl = new SaslClient(socket.Host.ToString(), new PlainMechanism(userName, password)); _socket = socket; }