Example #1
0
        /// <summary>
        /// Callback for Accept Implementation
        /// </summary>
        /// <returns>
        /// TTransport-object.
        /// </returns>
        protected override TTransport AcceptImpl()
        {
            if (this.server == null)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
            }

            try
            {
                TcpClient client = this.server.AcceptTcpClient();
                client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;

                //wrap the client in an SSL Socket passing in the SSL cert
                TTLSSocket socket = new TTLSSocket(client, this.serverCertificate, true, this.clientCertValidator);

                socket.setupTLS();

                if (useBufferedSockets)
                {
                    TBufferedTransport trans = new TBufferedTransport(socket);
                    return(trans);
                }
                else
                {
                    return(socket);
                }
            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString());
            }
        }
Example #2
0
        /// <summary>
        /// Callback for Accept Implementation
        /// </summary>
        /// <returns>
        /// TTransport-object.
        /// </returns>
        protected override TTransport AcceptImpl()
        {
            if (this.server == null)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
            }

            try
            {
#if NETSTANDARD1_4 || NETSTANDARD1_5
                TcpClient client = this.server.AcceptTcpClientAsync().Result;
#else
                TcpClient client = this.server.AcceptTcpClient();
#endif
                client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;

                //wrap the client in an SSL Socket passing in the SSL cert
                TTLSSocket socket = new TTLSSocket(
                    client,
                    this.serverCertificate,
                    true,
                    this.clientCertValidator,
                    this.localCertificateSelectionCallback
                    );

                socket.setupTLS();

                if (useBufferedSockets)
                {
#if NETSTANDARD1_4
                    throw new NotImplementedException("Not supported with NetStandard 1.4, use NetStandard 1.5 instead.");
#else
                    TBufferedTransport trans = new TBufferedTransport(socket);
                    return(trans);
#endif
                }
                else
                {
                    return(socket);
                }
            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString());
            }
        }
Example #3
0
            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));
                }
            }
Example #4
0
        /// <summary>
        /// Callback for Accept Implementation
        /// </summary>
        /// <returns>
        /// TTransport-object.
        /// </returns>
        protected override TTransport AcceptImpl()
        {
            if (server == null)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
            }

            try
            {
                var client = server.AcceptTcpClient();
                client.SendTimeout = client.ReceiveTimeout = clientTimeout;

                //wrap the client in an SSL Socket passing in the SSL cert
                var socket = new TTLSSocket(
                    client,
                    serverCertificate,
                    true,
                    clientCertValidator,
                    localCertificateSelectionCallback,
                    sslProtocols);

                socket.setupTLS();

                if (useBufferedSockets)
                {
                    var trans = new TBufferedTransport(socket);
                    return(trans);
                }
                else
                {
                    return(socket);
                }
            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString(), ex);
            }
        }
Example #5
0
        /// <summary>
        /// Callback for Accept Implementation
        /// </summary>
        /// <returns>
        /// TTransport-object.
        /// </returns>
        protected override TTransport AcceptImpl()
        {
            if (this.server == null)
            {
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
            }

            try
            {
                TcpClient client = this.server.AcceptTcpClient();
                client.SendTimeout = client.ReceiveTimeout = this.clientTimeout;

                //wrap the client in an SSL Socket passing in the SSL cert
                TTLSSocket socket = new TTLSSocket(
                    client,
                    this.serverCertificate,
                    true,
                    this.clientCertValidator,
                    this.localCertificateSelectionCallback,
                    this.sslProtocols);

                socket.setupTLS();

                if (useBufferedSockets)
                {
                    TBufferedTransport trans = new TBufferedTransport(socket);
                    return trans;
                }
                else
                {
                    return socket;
                }

            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString());
            }
        }
Example #6
0
        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();
                        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);
                return false;
            }

            Console.WriteLine();
            Console.WriteLine();
            return true;
        }