Exemple #1
0
        public static MapD.Client get_client(string host_or_uri, int port, bool http)
        {
            THttpClient     httpTransport;
            TTransport      transport;
            TBinaryProtocol protocol;
            TJSONProtocol   jsonProtocol;

            MapD.Client client;

            try{
                if (http)
                {
                    Uri httpuri = new Uri(host_or_uri);
                    httpTransport = new THttpClient(httpuri);
                    jsonProtocol  = new TJSONProtocol(httpTransport);
                    client        = new MapD.Client(jsonProtocol);
                    httpTransport.Open();
                    return(client);
                }
                else
                {
                    transport = new TSocket(host_or_uri, port);
                    protocol  = new TBinaryProtocol(transport);
                    client    = new MapD.Client(protocol);
                    transport.Open();
                    return(client);
                }
            } catch (TException x) {
                Console.WriteLine(x.StackTrace);
            }
            return(null);
        }
Exemple #2
0
        public static void TestThrift2365()
        {
            var rnd = new Random();

            for (var len = 0; len < 10; ++len)
            {
                byte[] dataWritten = new byte[len];
                rnd.NextBytes(dataWritten);

                Stream     stm   = new MemoryStream();
                TTransport trans = new TStreamTransport(null, stm);
                TProtocol  prot  = new TJSONProtocol(trans);
                prot.WriteBinary(dataWritten);

                stm.Position = 0;
                trans        = new TStreamTransport(stm, null);
                prot         = new TJSONProtocol(trans);
                byte[] dataRead = prot.ReadBinary();

                Debug.Assert(dataRead.Length == dataWritten.Length);
                for (var i = 0; i < dataRead.Length; ++i)
                {
                    Debug.Assert(dataRead[i] == dataWritten[i]);
                }
            }
        }
Exemple #3
0
        public static void Read <T>(this Stream rs, out T val) where T : TBase, new()
        {
            TProtocol prt = new TJSONProtocol(new TStreamTransport(rs, null));

            val = new T();
            val.Read(prt);
        }
Exemple #4
0
        public static void Write <T>(this Stream ws, T val) where T : TBase
        {
            TProtocol prt = new TJSONProtocol(new TStreamTransport(null, ws));

            val.Write(prt);
            ws.Flush();
        }
Exemple #5
0
        private static Calculator.Iface CreateCalculatorClient()
        {
            var uri          = new Uri("http://localhost:43857/any-url.thrift");
            var httpClient   = new THttpClient(uri);
            var jsonProtocol = new TJSONProtocol(httpClient);
            var calculator   = new Calculator.Client(jsonProtocol);

            return(calculator);
        }
Exemple #6
0
        internal static byte[] Serialize <T>(T thriftStruct)
            where T : TBase
        {
            var trans = new TMemoryBuffer();
            var prot  = new TJSONProtocol(trans);

            thriftStruct.Write(prot);
            return(trans.GetBuffer());
        }
Exemple #7
0
        internal static T DeSerialize <T>(byte[] data)
            where T : TBase
        {
            var trans  = new TMemoryBuffer(data);
            var prot   = new TJSONProtocol(trans);
            var result = Activator.CreateInstance <T>();

            result.Read(prot);
            return(result);
        }
Exemple #8
0
        public static void TestThrift3403()
        {
            string       GCLEF_TEXT = "\ud834\udd1e";
            const string GCLEF_JSON = "\"\\ud834\\udd1e\"";

            // parse and check
            var stm   = new MemoryStream(Encoding.UTF8.GetBytes(GCLEF_JSON));
            var trans = new TStreamTransport(stm, null);
            var prot  = new TJSONProtocol(trans);

            Debug.Assert(prot.ReadString() == GCLEF_TEXT, "reading JSON with surrogate pair hex-encoded chars");
        }
Exemple #9
0
        public override void Test(UpdateDelegate action)
        {
            TTransport transport = new TSocket(m_Ip, 7916);
            TProtocol  protocol  = new TJSONProtocol(transport);

            PrintServices.Client client = new PrintServices.Client(protocol);

            m_StrTransport = "TSocket";
            m_StrProtocol  = "ProtocolJson";

            HandleAdd(transport, client, action);
        }
Exemple #10
0
        public override void Test(UpdateDelegate action)
        {
            string     myUri     = @"http://" + m_Ip + @":7913";
            TTransport transport = new THttpClient(new Uri(myUri));
            TProtocol  protocol  = new TJSONProtocol(transport);

            PrintServices.Client client = new PrintServices.Client(protocol);

            m_StrTransport = "THttpClient";
            m_StrProtocol  = "TBinaryProtocol";

            HandleAdd(transport, client, action);
        }
 public static void DeSerialize(TBase tbase, byte[] bytes)
 {
     if (tbase == null || bytes == null)
     {
         return;
     }
     using (Stream inputStream = new MemoryStream(64))
     {
         inputStream.Write(bytes, 0, bytes.Length);
         inputStream.Position = 0;
         TStreamTransport transport = new TStreamTransport(inputStream, null);
         TProtocol        protocol  = new TJSONProtocol(transport);
         tbase.Read(protocol);
     }
 }
        public static string Serialize(TBase tbase)
        {
            if (tbase == null)
            {
                return(null);
            }

            using (MemoryStream outputStream = new MemoryStream())
            {
                TStreamTransport transport = new TStreamTransport(null, outputStream);
                TProtocol        protocol  = new TJSONProtocol(transport);
                tbase.Write(protocol);
                return(Encoding.UTF8.GetString(outputStream.ToArray()));
            }
        }
 public static byte[] Serialize(TBase tbase)
 {
     if (tbase == null)
     {
         return(null);
     }
     using (Stream outputStream = new MemoryStream(64))
     {
         TStreamTransport transport = new TStreamTransport(null, outputStream);
         TJSONProtocol    protocol  = new TJSONProtocol(transport);
         tbase.Write(protocol);
         byte[] bytes = new byte[outputStream.Length];
         outputStream.Position = 0;
         outputStream.Read(bytes, 0, bytes.Length);
         return(bytes);
     }
 }
Exemple #14
0
        public static void TestThrift2336()
        {
            const string RUSSIAN_TEXT = "\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435";
            const string RUSSIAN_JSON = "\"\\u0420\\u0443\\u0441\\u0441\\u043a\\u043e\\u0435 \\u041d\\u0430\\u0437\\u0432\\u0430\\u043d\\u0438\\u0435\"";

            // prepare buffer with JSON data
            byte[] rawBytes = new byte[RUSSIAN_JSON.Length];
            for (var i = 0; i < RUSSIAN_JSON.Length; ++i)
            {
                rawBytes[i] = (byte)(RUSSIAN_JSON[i] & (char)0xFF);  // only low bytes
            }
            // parse and check
            var stm   = new MemoryStream(rawBytes);
            var trans = new TStreamTransport(stm, null);
            var prot  = new TJSONProtocol(trans);

            Debug.Assert(prot.ReadString() == RUSSIAN_TEXT, "reading JSON with hex-encoded chars > 8 bit");
        }
Exemple #15
0
        public void TestThrift2336()
        {
            const string russianText = "\u0420\u0443\u0441\u0441\u043a\u043e\u0435 \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435";
            const string russianJson = "\"\\u0420\\u0443\\u0441\\u0441\\u043a\\u043e\\u0435 \\u041d\\u0430\\u0437\\u0432\\u0430\\u043d\\u0438\\u0435\"";

            // prepare buffer with JSON data
            var rawBytes = new byte[russianJson.Length];

            for (var i = 0; i < russianJson.Length; ++i)
            {
                rawBytes[i] = (byte)(russianJson[i] & (char)0xFF);  // only low bytes
            }
            // parse and check
            var stm   = new MemoryStream(rawBytes);
            var trans = new TStreamTransport(stm, null);
            var prot  = new TJSONProtocol(trans);

            Assert.AreEqual(russianText, prot.ReadString(), "reading JSON with hex-encoded chars > 8 bit");
        }
        public static void DeSerialize <T>(T tbase, string inJSON) where T : TBase
        {
            if ((tbase == null) || (inJSON == null))
            {
                return;
            }

            byte[] bytes = Encoding.UTF8.GetBytes(inJSON);
            using (MemoryStream inputStream = new MemoryStream())
            {
                TStreamTransport transport = new TStreamTransport(inputStream, null);
                TProtocol        protocol  = new TJSONProtocol(transport);

                inputStream.Write(bytes, 0, bytes.Length);
                inputStream.Seek(0, SeekOrigin.Begin);

                tbase.Read(protocol);
            }
        }
Exemple #17
0
 private void addProtocol(string url, Hashtable cfg, ProtocolType protocol_type)
 {
     if (protocol_type != ProtocolType.BINARY && protocol_type != ProtocolType.JSON)
     {
         throw new ArgumentException("no support for " + protocol_type);
     }
     if (!protocols_.ContainsKey(url))
     {
         string     p_url     = url.Substring(0, url.IndexOf(':'));
         TTransport transport = creators_[p_url](url, cfg);
         transport.Open();
         if (protocol_type == ProtocolType.BINARY)
         {
             protocols_[url] = new TBinaryProtocol(transport);
         }
         else if (protocol_type == ProtocolType.JSON)
         {
             protocols_[url] = new TJSONProtocol(transport);
         }
     }
 }
        public ThriftDemoService()
        {
            this.Transport = new TSocket(this.Host, this.Port);
            if (this.ProtocolType == "compact")
            {
                this.Protocol = new TCompactProtocol(this.Transport);
            }
            else if (this.ProtocolType == "json")
            {
                Protocol = new TJSONProtocol(this.Transport);
            }
            else
            {
                Protocol = new TBinaryProtocol(this.Transport);
            }
            this.Client = new RPCDemoService.Client(this.Protocol);

            if (!this.Transport.IsOpen)
            {
                this.Transport.Open();
            }
        }
Exemple #19
0
        public void TestThrift2365()
        {
            var rnd = new Random();

            for (var len = 0; len < 10; ++len)
            {
                var dataWritten = new byte[len];
                rnd.NextBytes(dataWritten);

                Stream     stm   = new MemoryStream();
                TTransport trans = new TStreamTransport(null, stm);
                TProtocol  prot  = new TJSONProtocol(trans);
                prot.WriteBinary(dataWritten);

                stm.Position = 0;
                trans        = new TStreamTransport(stm, null);
                prot         = new TJSONProtocol(trans);
                byte[] dataRead = prot.ReadBinary();

                CollectionAssert.AreEqual(dataWritten, dataRead);
            }
        }
Exemple #20
0
        /// <summary>
        /// Creates a Thrift protocol object, using the class as described in <see cref="Protocol"/>.
        /// </summary>
        /// <param name="transport">The transport that the protocol will be wrapped around.  Must not be null.</param>
        /// <returns>A TProtocol object.  If the value for <see cref="Protocol"/> is invalid then <see cref="TBinaryProtocol"/> is used.</returns>
        public TProtocol GetThriftProtcol(TTransport transport)
        {
            TProtocol protocol;

            switch (Protocol)
            {
            case "compact":
                protocol = new TCompactProtocol(transport);
                break;

            case "json":
                protocol = new TJSONProtocol(transport);
                break;

            //                case "binary":
            default:
                protocol = new TBinaryProtocol(transport);
                break;
            }

            return(protocol);
        }
Exemple #21
0
        public static TProtocol GetProtocol(string protocolName, TTransport transport)
        {
            TProtocol protocol;

            switch (protocolName)
            {
            case "TJSONProtocol":
                protocol = new TJSONProtocol(transport);
                break;

            case "TBinaryProtocol":
                protocol = new TBinaryProtocol(transport);
                break;

            case "TCompactProtocol":
                protocol = new TCompactProtocol(transport);
                break;

            default:
                throw new InvalidOperationException();
            }
            return(protocol);
        }
Exemple #22
0
        public static void ClientTest(TTransport transport)
        {
            TProtocol proto;

            if (protocol == "compact")
            {
                proto = new TCompactProtocol(transport);
            }
            else if (protocol == "json")
            {
                proto = new TJSONProtocol(transport);
            }
            else
            {
                proto = new TBinaryProtocol(transport);
            }

            ThriftTest.Client client = new ThriftTest.Client(proto);
            try
            {
                if (!transport.IsOpen)
                {
                    transport.Open();
                }
            }
            catch (TTransportException ttx)
            {
                Console.WriteLine("Connect failed: " + ttx.Message);
                return;
            }

            long start = DateTime.Now.ToFileTime();

            Console.Write("testVoid()");
            client.testVoid();
            Console.WriteLine(" = void");

            Console.Write("testString(\"Test\")");
            string s = client.testString("Test");

            Console.WriteLine(" = \"" + s + "\"");

            Console.Write("testBool(true)");
            bool t = client.testBool((bool)true);

            Console.WriteLine(" = " + t);
            Console.Write("testBool(false)");
            bool f = client.testBool((bool)false);

            Console.WriteLine(" = " + f);

            Console.Write("testByte(1)");
            sbyte i8 = client.testByte((sbyte)1);

            Console.WriteLine(" = " + i8);

            Console.Write("testI32(-1)");
            int i32 = client.testI32(-1);

            Console.WriteLine(" = " + i32);

            Console.Write("testI64(-34359738368)");
            long i64 = client.testI64(-34359738368);

            Console.WriteLine(" = " + i64);

            Console.Write("testDouble(5.325098235)");
            double dub = client.testDouble(5.325098235);

            Console.WriteLine(" = " + dub);

            byte[] binOut = PrepareTestData(true);
            Console.Write("testBinary(" + BytesToHex(binOut) + ")");
            try
            {
                byte[] binIn = client.testBinary(binOut);
                Console.WriteLine(" = " + BytesToHex(binIn));
                if (binIn.Length != binOut.Length)
                {
                    throw new Exception("testBinary: length mismatch");
                }
                for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
                {
                    if (binIn[ofs] != binOut[ofs])
                    {
                        throw new Exception("testBinary: content mismatch at offset " + ofs.ToString());
                    }
                }
            }
            catch (Thrift.TApplicationException e)
            {
                Console.Write("testBinary(" + BytesToHex(binOut) + "): " + e.Message);
            }

            // binary equals? only with hashcode option enabled ...
            if (typeof(CrazyNesting).GetMethod("Equals").DeclaringType == typeof(CrazyNesting))
            {
                CrazyNesting one = new CrazyNesting();
                CrazyNesting two = new CrazyNesting();
                one.String_field = "crazy";
                two.String_field = "crazy";
                one.Binary_field = new byte[10] {
                    0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF
                };
                two.Binary_field = new byte[10] {
                    0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0xFF
                };
                if (!one.Equals(two))
                {
                    throw new Exception("CrazyNesting.Equals failed");
                }
            }

            Console.Write("testStruct({\"Zero\", 1, -3, -5})");
            Xtruct o = new Xtruct();

            o.String_thing = "Zero";
            o.Byte_thing   = (sbyte)1;
            o.I32_thing    = -3;
            o.I64_thing    = -5;
            Xtruct i = client.testStruct(o);

            Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");

            Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
            Xtruct2 o2 = new Xtruct2();

            o2.Byte_thing   = (sbyte)1;
            o2.Struct_thing = o;
            o2.I32_thing    = 5;
            Xtruct2 i2 = client.testNest(o2);

            i = i2.Struct_thing;
            Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");

            Dictionary <int, int> mapout = new Dictionary <int, int>();

            for (int j = 0; j < 5; j++)
            {
                mapout[j] = j - 10;
            }
            Console.Write("testMap({");
            bool first = true;

            foreach (int key in mapout.Keys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(key + " => " + mapout[key]);
            }
            Console.Write("})");

            Dictionary <int, int> mapin = client.testMap(mapout);

            Console.Write(" = {");
            first = true;
            foreach (int key in mapin.Keys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(key + " => " + mapin[key]);
            }
            Console.WriteLine("}");

            List <int> listout = new List <int>();

            for (int j = -2; j < 3; j++)
            {
                listout.Add(j);
            }
            Console.Write("testList({");
            first = true;
            foreach (int j in listout)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.Write("})");

            List <int> listin = client.testList(listout);

            Console.Write(" = {");
            first = true;
            foreach (int j in listin)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.WriteLine("}");

            //set
            THashSet <int> setout = new THashSet <int>();

            for (int j = -2; j < 3; j++)
            {
                setout.Add(j);
            }
            Console.Write("testSet({");
            first = true;
            foreach (int j in setout)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.Write("})");

            THashSet <int> setin = client.testSet(setout);

            Console.Write(" = {");
            first = true;
            foreach (int j in setin)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.WriteLine("}");


            Console.Write("testEnum(ONE)");
            Numberz ret = client.testEnum(Numberz.ONE);

            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(TWO)");
            ret = client.testEnum(Numberz.TWO);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(THREE)");
            ret = client.testEnum(Numberz.THREE);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(FIVE)");
            ret = client.testEnum(Numberz.FIVE);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(EIGHT)");
            ret = client.testEnum(Numberz.EIGHT);
            Console.WriteLine(" = " + ret);

            Console.Write("testTypedef(309858235082523)");
            long uid = client.testTypedef(309858235082523L);

            Console.WriteLine(" = " + uid);

            Console.Write("testMapMap(1)");
            Dictionary <int, Dictionary <int, int> > mm = client.testMapMap(1);

            Console.Write(" = {");
            foreach (int key in mm.Keys)
            {
                Console.Write(key + " => {");
                Dictionary <int, int> m2 = mm[key];
                foreach (int k2 in m2.Keys)
                {
                    Console.Write(k2 + " => " + m2[k2] + ", ");
                }
                Console.Write("}, ");
            }
            Console.WriteLine("}");

            Insanity insane = new Insanity();

            insane.UserMap = new Dictionary <Numberz, long>();
            insane.UserMap[Numberz.FIVE] = 5000L;
            Xtruct truck = new Xtruct();

            truck.String_thing = "Truck";
            truck.Byte_thing   = (sbyte)8;
            truck.I32_thing    = 8;
            truck.I64_thing    = 8;
            insane.Xtructs     = new List <Xtruct>();
            insane.Xtructs.Add(truck);
            Console.Write("testInsanity()");
            Dictionary <long, Dictionary <Numberz, Insanity> > whoa = client.testInsanity(insane);

            Console.Write(" = {");
            foreach (long key in whoa.Keys)
            {
                Dictionary <Numberz, Insanity> val = whoa[key];
                Console.Write(key + " => {");

                foreach (Numberz k2 in val.Keys)
                {
                    Insanity v2 = val[k2];

                    Console.Write(k2 + " => {");
                    Dictionary <Numberz, long> userMap = v2.UserMap;

                    Console.Write("{");
                    if (userMap != null)
                    {
                        foreach (Numberz k3 in userMap.Keys)
                        {
                            Console.Write(k3 + " => " + userMap[k3] + ", ");
                        }
                    }
                    else
                    {
                        Console.Write("null");
                    }
                    Console.Write("}, ");

                    List <Xtruct> xtructs = v2.Xtructs;

                    Console.Write("{");
                    if (xtructs != null)
                    {
                        foreach (Xtruct x in xtructs)
                        {
                            Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
                        }
                    }
                    else
                    {
                        Console.Write("null");
                    }
                    Console.Write("}");

                    Console.Write("}, ");
                }
                Console.Write("}, ");
            }
            Console.WriteLine("}");

            sbyte arg0 = 1;
            int   arg1 = 2;
            long  arg2 = long.MaxValue;
            Dictionary <short, string> multiDict = new Dictionary <short, string>();

            multiDict[1] = "one";
            Numberz arg4 = Numberz.FIVE;
            long    arg5 = 5000000;

            Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
            Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);

            Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
                          + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");

            Console.WriteLine("Test Oneway(1)");
            client.testOneway(1);

            Console.Write("Test Calltime()");
            var startt = DateTime.UtcNow;

            for (int k = 0; k < 1000; ++k)
            {
                client.testVoid();
            }
            Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call");
        }
Exemple #23
0
        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();
        }
Exemple #24
0
        public static void ClientTest(TTransport transport)
        {
            TProtocol proto;

            if (protocol == "compact")
            {
                proto = new TCompactProtocol(transport);
            }
            else if (protocol == "json")
            {
                proto = new TJSONProtocol(transport);
            }
            else
            {
                proto = new TBinaryProtocol(transport);
            }

            ThriftTest.Client client = new ThriftTest.Client(proto);
            try
            {
                if (!transport.IsOpen)
                {
                    transport.Open();
                }
            }
            catch (TTransportException ttx)
            {
                Console.WriteLine("Connect failed: " + ttx.Message);
                return;
            }

            long start = DateTime.Now.ToFileTime();

            Console.Write("testVoid()");
            client.testVoid();
            Console.WriteLine(" = void");

            Console.Write("testString(\"Test\")");
            string s = client.testString("Test");

            Console.WriteLine(" = \"" + s + "\"");

            Console.Write("testByte(1)");
            sbyte i8 = client.testByte((sbyte)1);

            Console.WriteLine(" = " + i8);

            Console.Write("testI32(-1)");
            int i32 = client.testI32(-1);

            Console.WriteLine(" = " + i32);

            Console.Write("testI64(-34359738368)");
            long i64 = client.testI64(-34359738368);

            Console.WriteLine(" = " + i64);

            Console.Write("testDouble(5.325098235)");
            double dub = client.testDouble(5.325098235);

            Console.WriteLine(" = " + dub);

            Console.Write("testStruct({\"Zero\", 1, -3, -5})");
            Xtruct o = new Xtruct();

            o.String_thing = "Zero";
            o.Byte_thing   = (sbyte)1;
            o.I32_thing    = -3;
            o.I64_thing    = -5;
            Xtruct i = client.testStruct(o);

            Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");

            Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
            Xtruct2 o2 = new Xtruct2();

            o2.Byte_thing   = (sbyte)1;
            o2.Struct_thing = o;
            o2.I32_thing    = 5;
            Xtruct2 i2 = client.testNest(o2);

            i = i2.Struct_thing;
            Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");

            Dictionary <int, int> mapout = new Dictionary <int, int>();

            for (int j = 0; j < 5; j++)
            {
                mapout[j] = j - 10;
            }
            Console.Write("testMap({");
            bool first = true;

            foreach (int key in mapout.Keys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(key + " => " + mapout[key]);
            }
            Console.Write("})");

            Dictionary <int, int> mapin = client.testMap(mapout);

            Console.Write(" = {");
            first = true;
            foreach (int key in mapin.Keys)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(key + " => " + mapin[key]);
            }
            Console.WriteLine("}");

            List <int> listout = new List <int>();

            for (int j = -2; j < 3; j++)
            {
                listout.Add(j);
            }
            Console.Write("testList({");
            first = true;
            foreach (int j in listout)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.Write("})");

            List <int> listin = client.testList(listout);

            Console.Write(" = {");
            first = true;
            foreach (int j in listin)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.WriteLine("}");

            //set
            THashSet <int> setout = new THashSet <int>();

            for (int j = -2; j < 3; j++)
            {
                setout.Add(j);
            }
            Console.Write("testSet({");
            first = true;
            foreach (int j in setout)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.Write("})");

            THashSet <int> setin = client.testSet(setout);

            Console.Write(" = {");
            first = true;
            foreach (int j in setin)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    Console.Write(", ");
                }
                Console.Write(j);
            }
            Console.WriteLine("}");


            Console.Write("testEnum(ONE)");
            Numberz ret = client.testEnum(Numberz.ONE);

            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(TWO)");
            ret = client.testEnum(Numberz.TWO);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(THREE)");
            ret = client.testEnum(Numberz.THREE);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(FIVE)");
            ret = client.testEnum(Numberz.FIVE);
            Console.WriteLine(" = " + ret);

            Console.Write("testEnum(EIGHT)");
            ret = client.testEnum(Numberz.EIGHT);
            Console.WriteLine(" = " + ret);

            Console.Write("testTypedef(309858235082523)");
            long uid = client.testTypedef(309858235082523L);

            Console.WriteLine(" = " + uid);

            Console.Write("testMapMap(1)");
            Dictionary <int, Dictionary <int, int> > mm = client.testMapMap(1);

            Console.Write(" = {");
            foreach (int key in mm.Keys)
            {
                Console.Write(key + " => {");
                Dictionary <int, int> m2 = mm[key];
                foreach (int k2 in m2.Keys)
                {
                    Console.Write(k2 + " => " + m2[k2] + ", ");
                }
                Console.Write("}, ");
            }
            Console.WriteLine("}");

            Insanity insane = new Insanity();

            insane.UserMap = new Dictionary <Numberz, long>();
            insane.UserMap[Numberz.FIVE] = 5000L;
            Xtruct truck = new Xtruct();

            truck.String_thing = "Truck";
            truck.Byte_thing   = (sbyte)8;
            truck.I32_thing    = 8;
            truck.I64_thing    = 8;
            insane.Xtructs     = new List <Xtruct>();
            insane.Xtructs.Add(truck);
            Console.Write("testInsanity()");
            Dictionary <long, Dictionary <Numberz, Insanity> > whoa = client.testInsanity(insane);

            Console.Write(" = {");
            foreach (long key in whoa.Keys)
            {
                Dictionary <Numberz, Insanity> val = whoa[key];
                Console.Write(key + " => {");

                foreach (Numberz k2 in val.Keys)
                {
                    Insanity v2 = val[k2];

                    Console.Write(k2 + " => {");
                    Dictionary <Numberz, long> userMap = v2.UserMap;

                    Console.Write("{");
                    if (userMap != null)
                    {
                        foreach (Numberz k3 in userMap.Keys)
                        {
                            Console.Write(k3 + " => " + userMap[k3] + ", ");
                        }
                    }
                    else
                    {
                        Console.Write("null");
                    }
                    Console.Write("}, ");

                    List <Xtruct> xtructs = v2.Xtructs;

                    Console.Write("{");
                    if (xtructs != null)
                    {
                        foreach (Xtruct x in xtructs)
                        {
                            Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
                        }
                    }
                    else
                    {
                        Console.Write("null");
                    }
                    Console.Write("}");

                    Console.Write("}, ");
                }
                Console.Write("}, ");
            }
            Console.WriteLine("}");

            sbyte arg0 = 1;
            int   arg1 = 2;
            long  arg2 = long.MaxValue;
            Dictionary <short, string> multiDict = new Dictionary <short, string>();

            multiDict[1] = "one";
            Numberz arg4 = Numberz.FIVE;
            long    arg5 = 5000000;

            Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
            Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);

            Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
                          + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");

            Console.WriteLine("Test Oneway(1)");
            client.testOneway(1);

            Console.Write("Test Calltime()");
            var startt = DateTime.UtcNow;

            for (int k = 0; k < 1000; ++k)
            {
                client.testVoid();
            }
            Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call");
        }
Exemple #25
0
        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();
        }