public override void Test(UpdateDelegate action) { string appSetting = ConfigurationManager.AppSettings["CertPath"]; if (string.IsNullOrEmpty(appSetting)) { throw new Exception("授权文件路径为空,需要进行设置!设置位置: Web.Config->appSettings->CertPath"); } string certPath = appSetting + "\\client.p12"; if (!File.Exists(certPath)) { throw new Exception("授权文件不存在, 请确认文件夹路径是否正确!" + certPath); } string password = "******"; string currentPath = Directory.GetCurrentDirectory(); TTransport transport = new TTLSSocket(m_Ip, 7912, new X509Certificate2(certPath, password), RemoteCertificateCallback); TProtocol protocol = new TBinaryProtocol(transport); PrintServices.Client client = new PrintServices.Client(protocol); m_StrTransport = "TTLSSocket"; m_StrProtocol = "TBinaryProtocol"; HandleAdd(transport, client, action); }
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))); } }
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(); }