Beispiel #1
0
        private static Tuple <Protocol, TProtocol> GetProtocol(string[] args, TClientTransport transport)
        {
            var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':')?[1];

            Protocol selectedProtocol;

            if (Enum.TryParse(protocol, true, out selectedProtocol))
            {
                switch (selectedProtocol)
                {
                case Protocol.Binary:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));

                case Protocol.Compact:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TCompactProtocol(transport)));

                case Protocol.Json:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TJsonProtocol(transport)));

                case Protocol.Multiplexed:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));
                }
            }

            return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));
        }
Beispiel #2
0
        private static Tuple <Protocol, TProtocol> GetProtocol(string[] args, TClientTransport transport)
        {
            var protocol = args.FirstOrDefault(x => x.StartsWith("-p"))?.Split(':')?[1];

            Protocol selectedProtocol;

            if (Enum.TryParse(protocol, true, out selectedProtocol))
            {
                switch (selectedProtocol)
                {
                case Protocol.Binary:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));

                case Protocol.Compact:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TCompactProtocol(transport)));

                case Protocol.Json:
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TJsonProtocol(transport)));

                case Protocol.Multiplexed:
                    // it returns BinaryProtocol to avoid making wrapped protocol as public in TProtocolDecorator (in RunClientAsync it will be wrapped into Multiplexed protocol)
                    return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));
                }
            }

            return(new Tuple <Protocol, TProtocol>(selectedProtocol, new TBinaryProtocol(transport)));
        }
Beispiel #3
0
        /// <summary>
        ///     TJsonProtocol Constructor
        /// </summary>
        public TJsonProtocol(TClientTransport trans)
            : base(trans)
        {
            //throw new NotImplementedException("TJsonProtocol is not fully ready for usage");

            Context = new JsonBaseContext(this);
            Reader  = new LookaheadReader(this);
        }
Beispiel #4
0
        public HbaseRWTest()
        {
            _clientTransport = new TSocketClientTransport(IPAddress.Loopback, 9090);
            TProtocol protocol = new TBinaryProtocol(_clientTransport);

            _client      = new Hbase.Client(protocol);
            _hbaseHelper = new HbaseHelper();
            _HbaseParser = new HbaseParser();
        }
        //TODO: should support only specified input transport?
        public TBufferedClientTransport(TClientTransport transport, int bufSize = 1024)
        {
            if (bufSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufSize), "Buffer size must be a positive number.");
            }

            _transport = transport ?? throw new ArgumentNullException(nameof(transport));
            _bufSize   = bufSize;
        }
        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());
            }
        }
Beispiel #7
0
        public TFramedClientTransport(TClientTransport transport)
        {
            if (transport == null)
            {
                throw new ArgumentNullException(nameof(transport));
            }

            _transport = transport;

            InitWriteBuffer();
        }
Beispiel #8
0
        public TFramedClientTransport(TClientTransport transport)
        {
            throw new NotImplementedException("TFramedClientTransport is not fully ready for usage");

            if (transport == null)
            {
                throw new ArgumentNullException(nameof(transport));
            }

            _transport = transport;

            InitWriteBuffer();
        }
Beispiel #9
0
        public TFramedClientTransport(TClientTransport transport)
        {
            throw new NotImplementedException("TFramedClientTransport is not fully ready for usage");

            if (transport == null)
            {
                throw new ArgumentNullException(nameof(transport));
            }

            _transport = transport;

            InitWriteBuffer();
        }
Beispiel #10
0
            public TProtocol CreateProtocol(TClientTransport transport)
            {
                if (protocol == "compact")
                {
                    return(new TCompactProtocol(transport));
                }

                if (protocol == "json")
                {
                    return(new TJsonProtocol(transport));
                }

                return(new TBinaryProtocol(transport));
            }
        //TODO: should support only specified input transport?
        public TBufferedClientTransport(TClientTransport transport, int bufSize = 1024)
        {
            if (transport == null)
            {
                throw new ArgumentNullException(nameof(transport));
            }

            if (bufSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufSize), "Buffer size must be a positive number.");
            }

            _transport = transport;
            _bufSize = bufSize;
        }
Beispiel #12
0
        /// <summary>
        /// Dispose the SDK instance, don't miss this after SDK usage.
        /// </summary>
        public void Dispose()
        {
            Subscriber?.Dispose();
            Subscriber = null;

            Storage?.Dispose();
            Storage = null;

            RuyiNetService?.Dispose();
            RuyiNetService = null;

            SettingSys?.Dispose();
            SettingSys = null;

            L10NService?.Dispose();
            L10NService = null;

            UserService?.Dispose();
            UserService = null;

            InputMgr?.Dispose();
            InputMgr = null;

            SpeechService?.Dispose();
            SpeechService = null;

            Media?.Dispose();
            Media = null;

            OverlayService?.Dispose();
            OverlayService = null;

            lowLatencyTransport?.Close();
            LowLatencyProtocol?.Dispose();
            lowLatencyTransport = null;
            LowLatencyProtocol  = null;

            highLatencyTransport?.Close();
            HighLatencyProtocol?.Dispose();
            HighLatencyProtocol  = null;
            highLatencyTransport = null;

            InstanceCount--;
            if (InstanceCount <= 0)
            {
                factory.SDKCleanup();
            }
        }
Beispiel #13
0
            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));
            }
Beispiel #14
0
 public TCompactProtocol(TClientTransport trans)
     : base(trans)
 {
     TTypeToCompactType[(int)TType.Stop]   = Types.Stop;
     TTypeToCompactType[(int)TType.Bool]   = Types.BooleanTrue;
     TTypeToCompactType[(int)TType.Byte]   = Types.Byte;
     TTypeToCompactType[(int)TType.I16]    = Types.I16;
     TTypeToCompactType[(int)TType.I32]    = Types.I32;
     TTypeToCompactType[(int)TType.I64]    = Types.I64;
     TTypeToCompactType[(int)TType.Double] = Types.Double;
     TTypeToCompactType[(int)TType.String] = Types.Binary;
     TTypeToCompactType[(int)TType.List]   = Types.List;
     TTypeToCompactType[(int)TType.Set]    = Types.Set;
     TTypeToCompactType[(int)TType.Map]    = Types.Map;
     TTypeToCompactType[(int)TType.Struct] = Types.Struct;
 }
Beispiel #15
0
        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
            {
                var client = await _server.AcceptTcpClientAsync();

                client.SendTimeout = client.ReceiveTimeout = _clientTimeout;

                //wrap the client in an SSL Socket passing in the SSL cert
                var tTlsSocket = new TTlsSocketClientTransport(client, _serverCertificate, true, _clientCertValidator,
                                                               _localCertificateSelectionCallback, _sslProtocols);

                await tTlsSocket.SetupTlsAsync();

                TClientTransport trans = tTlsSocket;

                if (_useBufferedSockets)
                {
                    trans = new TBufferedClientTransport(trans);
                }

                if (_useFramedTransport)
                {
                    trans = new TFramedClientTransport(trans);
                }

                return(trans);
            }
            catch (Exception ex)
            {
                throw new TTransportException(ex.ToString());
            }
        }
Beispiel #16
0
        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 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));
            }
Beispiel #18
0
        private static async Task RunAsync(string[] args, CancellationToken cancellationToken)
        {
            var numClients = GetNumberOfClients(args);

            Logger.LogInformation($"Selected # of clients: {numClients}");

            var transports = new TClientTransport[numClients];

            for (var i = 0; i < numClients; i++)
            {
                var t = GetTransport(args);
                transports[i] = t;
            }

            Logger.LogInformation($"Selected client transport:{transports[0]}");

            var protocols = new Tuple <Protocol, TProtocol> [numClients];

            for (var i = 0; i < numClients; i++)
            {
                var p = GetProtocol(args, transports[i]);
                protocols[i] = p;
            }

            Logger.LogInformation($"Selected client protocol: {protocols[0].Item1}");

            var tasks = new Task[numClients];

            for (int i = 0; i < numClients; i++)
            {
                var task = RunClientAsync(protocols[i], cancellationToken);
                tasks[i] = task;
            }

            Task.WaitAll(tasks);

            //await Task.FromResult(1);
            await Task.CompletedTask;
        }
Beispiel #19
0
 public ThriftClient(TClientTransport transport, IDisposable thriftclient, URL url)
 {
     this.Transport    = transport;
     this.Thriftclient = thriftclient;
     this.Url          = url;
 }
 public ITAsyncProcessor GetAsyncProcessor(TClientTransport trans, TBaseServer baseServer = null)
 {
     return _tAsyncProcessor;
 }
Beispiel #21
0
 public TProtocol GetProtocol(TClientTransport trans)
 {
     return(new TBinaryProtocol(trans, StrictRead, StrictWrite));
 }
Beispiel #22
0
        bool Init()
        {
            // Default to using binary protocol
            Func <TClientTransport, TProtocol> createProtocolFunc = (transport) => new TBinaryProtocolTS(transport);

            if (context.Transport == null)
            {
                if (context.endpoint == RuyiSDKContext.Endpoint.Web)
                {
                    lowLatencyTransport  = new THttpClientTransport(new Uri(context.RemoteAddress), null);
                    highLatencyTransport = lowLatencyTransport;
                    createProtocolFunc   = (transport) => new TJsonProtocol(transport);
                }
                else
                {
                    var host = OS.Util.GetIPAddress(context.RemoteAddress);
                    // init and open high/low latency transport, create protocols
                    var lowLatencyPort = context.LowLatencyPort == 0 ? ConstantsSDKDataTypesConstants.low_latency_socket_port : context.LowLatencyPort;
                    lowLatencyTransport = new TSocketTransportTS(host, lowLatencyPort, context.Timeout <= 0 ? SDKUtility.Instance.LowLatencyTimeout : context.Timeout);

                    var highLatencyPort = context.HighLatencyPort == 0 ? ConstantsSDKDataTypesConstants.high_latency_socket_port : context.HighLatencyPort;
                    highLatencyTransport = new TSocketTransportTS(host, highLatencyPort, context.Timeout <= 0 ? SDKUtility.Instance.HighLatencyTimeout : context.Timeout);
                }
            }
            else
            {
                lowLatencyTransport  = context.Transport;
                highLatencyTransport = context.Transport;
            }

            // If we have 1 transport we need 1 protocol, if 2 transports 2 protocol
            if (Object.ReferenceEquals(lowLatencyTransport, highLatencyTransport))
            {
                LowLatencyProtocol  = createProtocolFunc(lowLatencyTransport);
                HighLatencyProtocol = LowLatencyProtocol;
                lowLatencyTransport.OpenAsync().Wait();
            }
            else
            {
                LowLatencyProtocol  = createProtocolFunc(lowLatencyTransport);
                HighLatencyProtocol = createProtocolFunc(highLatencyTransport);

                Task.WaitAll(
                    lowLatencyTransport.OpenAsync(),
                    highLatencyTransport.OpenAsync()
                    );
            }

            if (!ValidateVersion())
            {
                return(false);
            }

            var pubout = ConstantsSDKDataTypesConstants.layer0_publisher_out_uri.SetAddress(context.RemoteAddress);

            factory = new Layer0.ZeroMQ.MDPSDKFactory(string.Empty, pubout);

            // init subscriber
            if (context.endpoint != RuyiSDKContext.Endpoint.Web && IsFeatureEnabled(SDKFeatures.Subscriber))
            {
                Subscriber = factory.CreateSubscriber();
            }

            // init storage layer
            if (IsFeatureEnabled(SDKFeatures.Storage))
            {
                var stoProtocol = new TMultiplexedProtocol(HighLatencyProtocol, ServiceIDs.STORAGELAYER.ServiceID());
                Storage = new StorageLayerService.Client(stoProtocol);
            }

            // init braincloud service
            if (IsFeatureEnabled(SDKFeatures.Online))
            {
                var bcProtocol = new TMultiplexedProtocol(HighLatencyProtocol, ServiceIDs.BCSERVICE.ServiceID());
                RuyiNetService = new RuyiNetClient(bcProtocol, Storage);
                //BCService = new BrainCloudService.Client(bcProtocal);
            }

            // init setting system
            if (IsFeatureEnabled(SDKFeatures.Settings))
            {
                var proto = new TMultiplexedProtocol(LowLatencyProtocol, ServiceIDs.SETTINGSYSTEM_EXTERNAL.ServiceID());
                SettingSys = new SDK.SettingSystem.Api.SettingSystemService.Client(proto);
            }

            // init L10N
            if (IsFeatureEnabled(SDKFeatures.L10N))
            {
                var proto = new TMultiplexedProtocol(LowLatencyProtocol, ServiceIDs.L10NSERVICE.ServiceID());
                L10NService = new LocalizationService.Client(proto);
            }

            // user manager
            if (IsFeatureEnabled(SDKFeatures.Users))
            {
                var proto = new TMultiplexedProtocol(HighLatencyProtocol, ServiceIDs.USER_SERVICE_EXTERNAL.ServiceID());
                UserService = new UserServExternal.Client(proto);
            }

            // input manger
            if (IsFeatureEnabled(SDKFeatures.Input))
            {
                var proto = new TMultiplexedProtocol(LowLatencyProtocol, ServiceIDs.INPUTMANAGER_EXTERNAL.ServiceID());
                InputMgr = new InputManagerService.Client(proto);
            }

            if (IsFeatureEnabled(SDKFeatures.Speech))
            {
                var proto = new TMultiplexedProtocol(HighLatencyProtocol, ServiceIDs.SPEECH.ServiceID());
                SpeechService = new SpeechService.Client(proto);
            }

            if (IsFeatureEnabled(SDKFeatures.Media))
            {
                var proto = new TMultiplexedProtocol(HighLatencyProtocol, ServiceIDs.MEDIA.ServiceID());
                Media = new MediaService.Client(proto);
            }
            if (IsFeatureEnabled(SDKFeatures.Overlay))
            {
                var proto = new TMultiplexedProtocol(LowLatencyProtocol, ServiceIDs.OVERLAYMANAGER_EXTERNAL.ServiceID());
                OverlayService = new ExternalOverlayManagerService.Client(proto);
            }
            return(true);
        }
 /// <summary>
 ///     TJsonProtocol Constructor
 /// </summary>
 public TJsonProtocol(TClientTransport trans)
     : base(trans)
 {
     Context = new JSONBaseContext(this);
     Reader  = new LookaheadReader(this);
 }
Beispiel #24
0
 public ClientTest(TestParams param)
 {
     transport     = param.CreateTransport();
     client        = new ThriftTest.Client(param.CreateProtocol(transport));
     numIterations = param.numIterations;
 }
 public TJSONProtocolWrapper(TClientTransport trans) : base(trans)
 {
 }
Beispiel #26
0
 public TProtocol GetProtocol(TClientTransport trans)
 {
     return(new TCompactProtocol(trans));
 }
 public Task ProcessContextAsync(object serverContext, TClientTransport transport, CancellationToken cancellationToken)
 {
     callCount++;
     return(Task.CompletedTask);
 }
Beispiel #28
0
 public TBinaryProtocol(TClientTransport trans, bool strictRead, bool strictWrite)
     : base(trans)
 {
     StrictRead  = strictRead;
     StrictWrite = strictWrite;
 }
Beispiel #29
0
 public virtual TClientTransport GetTransport(TClientTransport trans)
 {
     return trans;
 }
Beispiel #30
0
        private async Task Execute(TClientTransport client, CancellationToken cancellationToken)
        {
            Logger.LogTrace("Started client request processing");

            var processor = ItProcessorFactory.GetAsyncProcessor(client, this);

            TClientTransport inputTransport    = null;
            TClientTransport outputTransport   = null;
            TProtocol        inputProtocol     = null;
            TProtocol        outputProtocol    = null;
            object           connectionContext = null;

            try
            {
                inputTransport  = InputTransportFactory.GetTransport(client);
                outputTransport = OutputTransportFactory.GetTransport(client);

                inputProtocol  = InputProtocolFactory.GetProtocol(inputTransport);
                outputProtocol = OutputProtocolFactory.GetProtocol(outputTransport);

                if (ServerEventHandler != null)
                {
                    connectionContext = await ServerEventHandler.CreateContextAsync(inputProtocol, outputProtocol, cancellationToken);
                }

                while (!cancellationToken.IsCancellationRequested)
                {
                    if (!await inputTransport.PeekAsync(cancellationToken))
                    {
                        break;
                    }

                    if (ServerEventHandler != null)
                    {
                        await ServerEventHandler.ProcessContextAsync(connectionContext, inputTransport, cancellationToken);
                    }

                    if (!await processor.ProcessAsync(inputProtocol, outputProtocol, cancellationToken))
                    {
                        break;
                    }
                }
            }
            catch (TTransportException ttx)
            {
                Logger.LogTrace($"Transport exception: {ttx}");
            }
            catch (Exception x)
            {
                Logger.LogError($"Error: {x}");
            }

            if (ServerEventHandler != null)
            {
                await ServerEventHandler.DeleteContextAsync(connectionContext, inputProtocol, outputProtocol, cancellationToken);
            }

            inputTransport?.Close();
            outputTransport?.Close();

            Logger.LogTrace("Completed client request processing");
        }
 public virtual TClientTransport GetTransport(TClientTransport trans)
 {
     return(trans);
 }
Beispiel #32
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="trans"></param>
 public TBinaryProtocolTS(TClientTransport trans) : base(trans)
 {
 }
 protected TProtocol(TClientTransport trans)
 {
     Trans          = trans;
     RecursionLimit = DefaultRecursionDepth;
     RecursionDepth = 0;
 }