Example #1
0
 public static byte[] Serialize(NetMsg msg)
 {
     MemoryStream stream = new MemoryStream();
     TCompactProtocol protocol = new TCompactProtocol(new TStreamTransport(stream, stream));
     msg.Write(protocol);
     return stream.ToArray();
 }
        public void RunExpt1()
        {
            // Prepare input entity, we reuse existing init code
            // and just Inject (via Value Injecter library)
            var origMsg = new InheritedEntity();
            origMsg.FillDummyData();
            var origMsgJson = JsonConvert.SerializeObject(origMsg);
            Console.WriteLine("Original object is {0}", origMsgJson);

            // Serialization
            var tmsg = new InheritedEntityThrift();
            tmsg.InjectFrom(origMsg);
            var ms = new MemoryStream();
            var tproto = new TCompactProtocol(new TStreamTransport(ms, ms));
            tmsg.Write(tproto);
            var tbytes = ms.ToArray();

            Console.WriteLine("Serialized to {0} bytes: {1}", tbytes.Length, BitConverter.ToString(tbytes));

            // Deserialization
            var ms2 = new MemoryStream(tbytes);
            var tproto2 = new TCompactProtocol(new TStreamTransport(ms2, ms2));
            var regenTMsg = new InheritedEntityThrift();
            regenTMsg.Read(tproto2);
            var regenMsg = new InheritedEntity();
            regenMsg.InjectFrom(regenTMsg);

            var regenMsgJson = JsonConvert.SerializeObject(regenMsg);
            Console.WriteLine("Regenerated object is {0}", regenMsgJson);
        }
Example #3
0
 public static NetMsg Deserialize(byte[] byteArr)
 {
     MemoryStream stream = new MemoryStream(byteArr);
     TCompactProtocol protocol = new TCompactProtocol(new TStreamTransport(stream, stream));
     NetMsg _msg = new NetMsg();
     _msg.Read(protocol);
     return _msg;
 }
        public ThriftClient(string host, int port)
        {
            if (host == null) throw new ArgumentNullException("host");

            _transport = new TFramedTransport(new TSocket(host, port));
            TProtocol protocol = new TCompactProtocol(_transport);
            _client = new ThriftSourceProtocol.Client(protocol);
            _transport.Open();
        }
Example #5
0
        //This is internal because other classes will use this client to make
        //thrift calls to get their required information
        //this is hugely important

        public LineClient()
        {
            //Queue of operations for dispatch
            Operations = new Queue<Operation>();
            
            //This shit is for serialization
            _thriftTransport = new LineTransport(Protocol.UserAgent, Protocol.LineApplication);
            TProtocol thriftProtocol = new TCompactProtocol(_thriftTransport);

            //This is the heart and soul of all the function calls here.
            Client = new TalkService.Client(thriftProtocol);

            //This reads for operations/events and reports them where they belong
            _operationHandler = new OperationHandler(this);
        }
 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 TCompactProtocol(transport);
         tbase.Read(protocol);
     }
 }
 public static byte[] Serialize(TBase tbase)
 {
     if (tbase == null)
     {
         return null;
     }
     using (Stream outputStream = new MemoryStream(64))
     {
         TStreamTransport transport = new TStreamTransport(null, outputStream);
         TProtocol protocol = new TCompactProtocol(transport);
         tbase.Write(protocol);
         byte[] bytes = new byte[outputStream.Length];
         outputStream.Position = 0;
         outputStream.Read(bytes, 0, bytes.Length);
         return bytes;
     }
 }
Example #8
0
        internal static byte[] SerializeOperation(Operation O)
        {
            var serialstream = new MemoryStream(4096);
            TTransport transport = new TStreamTransport(serialstream, serialstream);
            transport.Open();
            TProtocol protocol = new TCompactProtocol(transport);
            var client = new TalkService.Client(protocol);

            //.MakeGenericMethod(
            //hook.Invoke(Client, parameters);

            O.Write(protocol);


            byte[] data = serialstream.ToArray();
            //MemoryStream serialstream = new MemoryStream(4096);
            //TTransport transport = new TStreamTransport(serialstream, serialstream); transport.Open();


            return data;
        }
Example #9
0
        //This shit only exists because the internal LINE client transport doesn't handle
        //more than a single connection at a time, so if it was being used for long polling,
        //we'd be f****d because it would probably freeze the entire thing and prevent any
        //other calls from happening. It's not something I'm proud to have written but it
        //works wonders if you need to call something in parallel. It's just expensive. :O

        internal static byte[] Serialize(string clientFunction, object[] parameters)
        {
            var serialstream = new MemoryStream(4096);
            TTransport transport = new TStreamTransport(serialstream, serialstream);
            transport.Open();
            TProtocol protocol = new TCompactProtocol(transport);
            var client = new TalkService.Client(protocol);

            //.MakeGenericMethod(
            //hook.Invoke(Client, parameters);
            MethodInfo callingFunction = client.GetType().GetMethod(clientFunction);

            callingFunction.Invoke(client, parameters);

            byte[] data = serialstream.ToArray();
            //MemoryStream serialstream = new MemoryStream(4096);
            //TTransport transport = new TStreamTransport(serialstream, serialstream); transport.Open();


            return data;
        }
Example #10
0
        internal static object Deserialize(string ClientFunction, byte[] data)
        {
            MemoryStream serialstream = new MemoryStream(data);
            TTransport transport = new TStreamTransport(serialstream, serialstream); transport.Open();
            TProtocol protocol = new TCompactProtocol(transport);
            TalkService.Client Client = new TalkService.Client(protocol);
            MethodInfo CallingFunction = Client.GetType().GetMethod(ClientFunction);

            try
            {
                return CallingFunction.Invoke(Client, null);
            }
            catch (TargetInvocationException E)
            {
                if (E.InnerException is TalkException)
                {
                    throw E.InnerException;
                }
            }
            return null;
        }
Example #11
0
 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);
   }
 }
Example #12
0
        internal static object Deserialize(string clientFunction, byte[] data)
        {
            var serialstream = new MemoryStream(data);
            TTransport transport = new TStreamTransport(serialstream, serialstream);
            transport.Open();
            TProtocol protocol = new TCompactProtocol(transport);
            var client = new TalkService.Client(protocol);
            MethodInfo callingFunction = client.GetType().GetMethod(clientFunction);

            //Magic to redirect the possible exception to the end user's code
            //or at least the nearest TalkException catch
            try
            {
                return callingFunction.Invoke(client, null);
            }
            catch (TargetInvocationException E)
            {
                if (E.InnerException is TalkException)
                {
                    throw E.InnerException;
                }
            }
            return null;
        }
Example #13
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);

            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" );
        }
Example #14
0
        static void TestFunc(string svrAddr, int threadCount)
        {
            List<Reserve> list = new List<Reserve>();
            for (int j = 0; j < packageCount; j++)
            {
                Reserve r = CreateTestReserve(j);
                list.Add(r);
            }

            ThriftPool pool = new ThriftPool(new ThriftConfig()
            {
                #region 配置连接池

                Host = svrAddr,
                Port = 9090,
                Timeout = 60,
                MaxActive = 100,
                MaxIdle = 20,
                MinIdle = 5,
                Encode = Encoding.UTF8

                #endregion
            });

            using (FileStream fs = new FileStream(".\\output.log", FileMode.Create, FileAccess.Write))
            {
                PerformanceTestHelper helper = new PerformanceTestHelper((index) =>
                {
                    TTransport transport = pool.BorrowInstance();
                    //TTransport transport = new TFramedTransport(new TSocket(svrAddr, 9090));
                    //TProtocol protocol = new TBinaryProtocol(transport);
                    TProtocol protocol = new TCompactProtocol(transport);
                    //transport.Open();
                    Serv.Client client = new Serv.Client(protocol);

                    client.createBatch(list);

                    pool.ReturnInstance(transport);
                    //transport.Close();

                }, threadCount, 1000, true, fs);
                helper.Run();
            }
        }