/// <summary> /// Retrives the correct Thrift transport type based on the value in <see cref="Transport"/>. /// </summary> /// <returns>If <see cref="Transport"/> is <code>namedpipe</code> then a <see cref="TNamedPipeClientTransport"/> is used. For all other values /// a <see cref="TSocket"/> is returned. Depending on the value of <see cref="Framed"/> and <see cref="Buffered"/>, appropriate wrappers will be placed /// around the transport. /// </returns> public TTransport GetThriftTransport() { TTransport transport; switch (Transport) { case "namedpipe": transport = new TNamedPipeClientTransport(ServiceHost, NamedPipeName); break; // case "socket": default: transport = new TSocket(ServiceHost, ServicePort); break; } if (Framed) { transport = new TFramedTransport(transport); } if (Buffered) { transport = new TBufferedTransport((TStreamTransport)transport, BufferSize); } return(transport); }
static void Main(string[] args) { TTransport trans = new TNamedPipeClientTransport("TradeStream"); trans.Open(); TProtocol proto = new TCompactProtocol(trans); PNWF.TradeStream.Client client = new PNWF.TradeStream.Client(proto); try { for (int i = 0; i < 100; i++) { List <PNWF.Trade> trades = client.GetNextTrade(""); foreach (PNWF.Trade trade in trades) { Console.Out.WriteLine(trade.Date_time.Hour.ToString("D2") + ":" + trade.Date_time.Minute.ToString("D2") + ":" + trade.Date_time.Second.ToString("D2") + " " + trade.Fish + "-" + trade.Market.ToString() + " " + trade.Price); } } } catch (TTransportException ex) { Console.WriteLine("Exception: " + ex.Message); } }
public TTransport CreateTransport() { if (url == null) { // endpoint transport TTransport trans = null; if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { string certPath = "../keys/client.p12"; X509Certificate cert = new X509Certificate2(certPath, "thrift"); trans = new TTLSSocket(host, port, 0, cert, (o, c, chain, errors) => true, null, SslProtocols.Tls); } else { trans = new TSocket(host, port); } } // layered transport if (buffered) { trans = new TBufferedTransport(trans); } if (framed) { trans = new TFramedTransport(trans); } if (_isFirstTransport) { //ensure proper open/close of transport trans.Open(); trans.Close(); _isFirstTransport = false; } return(trans); } else { return(new THttpClient(new Uri(url))); } }
static void Main(string[] args) { #if SOCKET TTransport transport = new TSocket("localhost", 8080); #else TTransport transport = new TNamedPipeClientTransport("MyPipeName"); #endif TProtocol protocol = new TBinaryProtocol(transport); HelloWorld.Client client = new HelloWorld.Client(protocol); transport.Open(); String res = client.hoge("Send to Server"); Console.WriteLine(res); }
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)); }
public override void Test(UpdateDelegate action) { string pipeName = "pipe123"; TTransport transport = new TNamedPipeClientTransport(pipeName); TProtocol protocol = new TBinaryProtocol(transport); PrintServices.Client client = new PrintServices.Client(protocol); m_StrTransport = "TNamedPipe"; m_StrProtocol = "TBinaryProtocol"; if (!transport.IsOpen) { transport.Open(); } testAdd(client, action); transport.Close(); }
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)); }
public static void Execute(string[] args) { try { string host = "localhost"; int port = 9090; string url = null, pipe = null; int numThreads = 1; bool buffered = false, framed = false, encrypted = false; try { for (int i = 0; i < args.Length; i++) { if (args[i] == "-h") { string[] hostport = args[++i].Split(':'); host = hostport[0]; if (hostport.Length > 1) { port = Convert.ToInt32(hostport[1]); } } else if (args[i] == "-u") { url = args[++i]; } else if (args[i] == "-n") { numIterations = Convert.ToInt32(args[++i]); } else if (args[i] == "-b" || args[i] == "-buffered") { buffered = true; Console.WriteLine("Using buffered sockets"); } else if (args[i] == "-f" || args[i] == "-framed") { framed = true; Console.WriteLine("Using framed transport"); } else if (args[i] == "-pipe") // -pipe <name> { pipe = args[++i]; Console.WriteLine("Using named pipes transport"); } else if (args[i] == "-t") { numThreads = Convert.ToInt32(args[++i]); } else if (args[i] == "-ssl") { encrypted = true; Console.WriteLine("Using encrypted transport"); } else if (args[i] == "-compact") { protocol = "compact"; Console.WriteLine("Using compact protocol"); } else if (args[i] == "-json") { protocol = "json"; Console.WriteLine("Using JSON protocol"); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); } //issue tests on separate threads simultaneously Thread[] threads = new Thread[numThreads]; DateTime start = DateTime.Now; for (int test = 0; test < numThreads; test++) { Thread t = new Thread(new ParameterizedThreadStart(ClientThread)); threads[test] = t; if (url == null) { // endpoint transport TTransport trans = null; if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { trans = new TTLSSocket(host, port, "../../../../../keys/client.pem"); } else { trans = new TSocket(host, port); } } // layered transport if (buffered) { trans = new TBufferedTransport(trans as TStreamTransport); } if (framed) { trans = new TFramedTransport(trans); } //ensure proper open/close of transport trans.Open(); trans.Close(); t.Start(trans); } else { THttpClient http = new THttpClient(new Uri(url)); t.Start(http); } } for (int test = 0; test < numThreads; test++) { threads[test].Join(); } Console.Write("Total time: " + (DateTime.Now - start)); } catch (Exception outerEx) { Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace); } Console.WriteLine(); Console.WriteLine(); }
public static void ClientTest(string host, int port, string certPath, string url, string pipe, bool encrypted, bool buffered, bool framed) { TTransport trans = null; if (url == null) { // endpoint transport if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { trans = new TTLSSocket(host, port, certPath); } else { trans = new TSocket(host, port); } } // layered transport if (buffered) { trans = new TBufferedTransport(trans as TStreamTransport); } if (framed) { trans = new TFramedTransport(trans); } //ensure proper open/close of transport trans.Open(); } else { trans = new THttpClient(new Uri(url)); } TProtocol proto; if (protocol == "compact") { proto = new TCompactProtocol(trans); } else if (protocol == "json") { proto = new TJSONProtocol(trans); } else { proto = new TBinaryProtocol(trans); } RPCDemoService.Client client = new RPCDemoService.Client(proto); try { if (!trans.IsOpen) { trans.Open(); } } catch (TTransportException ttx) { Console.WriteLine("Connect failed: " + ttx.Message); return; } var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < length; i++) { var reply = client.GetById(i); Console.WriteLine("receive" + JsonConvert.SerializeObject(reply)); } stopwatch.Stop(); Console.WriteLine(string.Format("repeat={0}, time={1} Milliseconds, time/repeat={2}", length, stopwatch.ElapsedMilliseconds, stopwatch.ElapsedMilliseconds / (float)length)); Console.ReadKey(); }
public static bool Execute(string[] args) { try { string host = "localhost"; int port = 9090; string url = null, pipe = null; int numThreads = 1; bool buffered = false, framed = false, encrypted = false; string certPath = "../../../../../keys/server.pem"; try { for (int i = 0; i < args.Length; i++) { if (args[i] == "-u") { url = args[++i]; } else if (args[i] == "-n") { numIterations = Convert.ToInt32(args[++i]); } else if (args[i] == "-pipe") // -pipe <name> { pipe = args[++i]; Console.WriteLine("Using named pipes transport"); } else if (args[i].Contains("--host=")) { host = args[i].Substring(args[i].IndexOf("=") + 1); } else if (args[i].Contains("--port=")) { port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1)); } else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") { buffered = true; Console.WriteLine("Using buffered sockets"); } else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") { framed = true; Console.WriteLine("Using framed transport"); } else if (args[i] == "-t") { numThreads = Convert.ToInt32(args[++i]); } else if (args[i] == "--compact" || args[i] == "--protocol=compact") { protocol = "compact"; Console.WriteLine("Using compact protocol"); } else if (args[i] == "--json" || args[i] == "--protocol=json") { protocol = "json"; Console.WriteLine("Using JSON protocol"); } else if (args[i] == "--ssl") { encrypted = true; Console.WriteLine("Using encrypted transport"); } else if (args[i].StartsWith("--cert=")) { certPath = args[i].Substring("--cert=".Length); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); } //issue tests on separate threads simultaneously Thread[] threads = new Thread[numThreads]; DateTime start = DateTime.Now; for (int test = 0; test < numThreads; test++) { Thread t = new Thread(new ParameterizedThreadStart(ClientThread)); threads[test] = t; if (url == null) { // endpoint transport TTransport trans = null; if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { trans = new TTLSSocket(host, port, certPath); } else { trans = new TSocket(host, port); } } // layered transport if (buffered) { trans = new TBufferedTransport(trans as TStreamTransport); } if (framed) { trans = new TFramedTransport(trans); } //ensure proper open/close of transport trans.Open(); #if !NETCOREAPP1_1 trans.Close(); #endif t.Start(trans); } else { THttpClient http = new THttpClient(new Uri(url)); t.Start(http); } } for (int test = 0; test < numThreads; test++) { threads[test].Join(); } Console.Write("Total time: " + (DateTime.Now - start)); } catch (Exception outerEx) { Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace); return(false); } Console.WriteLine(); Console.WriteLine(); return(true); }
public static void ClientTest(string host, int port, string certPath, string url, string pipe, bool encrypted, bool buffered, bool framed, string protocol, int i) { TTransport trans = null; if (url == null) { if (pipe != null) { trans = new TNamedPipeClientTransport(pipe); } else { if (encrypted) { trans = new TTLSSocket(host, port, certPath); } else { trans = new TSocket(host, port); } } // layered transport if (buffered) { trans = new TBufferedTransport(trans as TStreamTransport); } if (framed) { trans = new TFramedTransport(trans); } //ensure proper open/close of transport trans.Open(); } else { trans = new THttpClient(new Uri(url)); } TProtocol proto; if (protocol == "compact") { proto = new TCompactProtocol(trans); } else if (protocol == "json") { proto = new TJSONProtocol(trans); } else { proto = new TBinaryProtocol(trans); } RPCDemoService.Client client = new RPCDemoService.Client(proto); try { if (!trans.IsOpen) { trans.Open(); } } catch (TTransportException ttx) { Console.WriteLine("Connect failed: " + ttx.Message); return; } var reply = client.GetById(i); Console.WriteLine("receive" + JsonConvert.SerializeObject(reply)); trans.Close(); }