public byte[] Encode() { TarsOutputStream os = new TarsOutputStream(); os.SetServerEncoding(EncodeName); if (IsVersionTup3) { os.Write((IDictionary)newData, 0); return(os.ToByteArray()); } else { os.Write((IDictionary)data, 0); return(os.ToByteArray()); } }
public virtual byte[] EncodeMethodParameters(object[] parameters, RpcMethodMetadata metdata) { if (metdata.Parameters.Length == 0) { return(new byte[0]); } var buf = Unpooled.Buffer(128); var output = new TarsOutputStream(buf); foreach (var item in metdata.Parameters) { output.Write(parameters[item.Position], item.Position + 1); } return(output.ToByteArray()); }
public void Put <T>(string name, T t) { if (name == null) { throw new ArgumentException("put key can not be null"); } if (t == null) { throw new ArgumentException("put value can not be null"); } TarsOutputStream _out = new TarsOutputStream(); _out.SetServerEncoding(EncodeName); _out.Write(t, 0); byte[] sBuffer = _out.ToByteArray(); if (IsVersionTup3) { if (newData.ContainsKey(name)) { newData[name] = sBuffer; } else { newData.Add(name, sBuffer); } } else { List <string> listType = new List <string>(); CheckObjectType(listType, t); string className = BasicClassTypeUtil.TransTypeList(listType); Dictionary <string, byte[]> pair = new Dictionary <string, byte[]>(1) { { className, sBuffer } }; cachedData.Remove(name); if (data.ContainsKey(name)) { data[name] = pair; } else { data.Add(name, pair); } } }
public override byte[] EncodeReturnValue(object returnValue, object[] parameters, RpcMethodMetadata metdata) { var unaOut = CreateUniAttribute(); if (returnValue != null && metdata.RealReturnType != typeof(void)) { unaOut.Put(string.Empty, returnValue); } foreach (var item in metdata.Parameters.Where(i => i.ParameterType.IsByRef)) { unaOut.Put(item.Name, parameters[item.Position]); } var buf = Unpooled.Buffer(128); var output = new TarsOutputStream(buf); unaOut.WriteTo(output); return(output.ToByteArray()); }
public override byte[] EncodeMethodParameters(object[] parameters, RpcMethodMetadata metdata) { if (metdata.Parameters.Length == 0) { return(new byte[0]); } var unaOut = CreateUniAttribute(); foreach (var item in metdata.Parameters.Where(i => i.ParameterType.IsByRef)) { unaOut.Put(item.Name, parameters[item.Position]); } var buf = Unpooled.Buffer(128); var output = new TarsOutputStream(buf); unaOut.WriteTo(output); return(output.ToByteArray()); }
public virtual byte[] EncodeReturnValue(object returnValue, object[] parameters, RpcMethodMetadata metdata) { var buf = Unpooled.Buffer(128); var output = new TarsOutputStream(buf); if (metdata.RealReturnType != typeof(void)) { output.Write(returnValue, 0); } if (parameters != null) { foreach (var item in metdata.Parameters.Where(i => i.ParameterType.IsByRef)) { output.Write(parameters[item.Position], item.Position + 1); } } return(output.ToByteArray()); }
/* * 结构体赋值并序列化的例子 */ public void TestTars(ref TestInfo ti, ref byte[] buffer) { // 给结构TestInfo赋值 ti.ibegin = 1111; ti.ii = 54321; ti.li = -1000; ti.iend = 9999; ti.f = 0.999f; ti.d = 123.456f; ti.s = "teststring0"; ti.uii = 123456789; // test vector<byte> ti.vb = new List <byte>(); ////////////////////////////////////////////// // vector<int> 类型的成员赋值 ti.vi = new List <int>(); for (int il = 0; il < 10; il++) { ti.vi.Add(10000 + il); } ////////////////////////////////////////////// // map<int, string> 类型的成员赋值 ti.mi = new Dictionary <int, string>(); for (int idl = 0; idl < 10; idl++) { ti.mi.Add(20000 + idl, "testmap" + idl); } ////////////////////////////////////////////// // 结构类型的成员赋值 ti.aa = new A(); ti.aa.a = 200; ti.aa.b = new B(); ti.aa.b.a = 300; ti.aa.b.f = 0.300f; ////////////////////////////////////////////// // vector<struct>类型的成员赋值 ti.vi2 = new List <A>(); for (int j = 0; j < 10; j++) { A a = new A(); a.a = 200000 + j; a.b = new B(); a.b.a = 300; a.b.f = 0.300f; ti.vi2.Add(a); } // List<float>类型成员赋值 ti.vf = new List <float>(); float ff = 1.23f; for (int il = 0; il < 5; il++) { ti.vf.Add(ff); } ////////////////////////////////////////////// // map<int, struct>类型的成员赋值 ti.mi2 = new Dictionary <int, A>(); for (int il = 0; il < 10; il++) { A a = new A(); a.a = 200 + il; a.b = new B(); a.b.a = 300; a.b.f = 0.300f; ti.mi2.Add(300000 + il, a); } ////////////////////////////////////////////// // map<string, vector<struct>>类型的成员赋值 ti.msv = new Dictionary <string, List <A> >(); for (int il = 0; il < 10; il++) { List <A> listA = new List <A>(); for (int j = 0; j < 2; j++) { A a = new A(); a.a = 200 + il; a.b = new B(); a.b.a = 300; a.b.f = 0.300f; listA.Add(a); } ti.msv.Add("key" + 400000 + il, listA); } // 结构体赋值结束 TarsOutputStream os = new TarsOutputStream(0); // 把结构序列化 ti.WriteTo(os); buffer = os.ToByteArray(); System.IO.File.WriteAllBytes(@"tarsbuffer.txt", buffer); TarsInputStream _is = new TarsInputStream(); _is.Wrap(buffer, 0); TestInfo stTest = new TestInfo(); stTest.ReadFrom(_is); Console.WriteLine("stTest.ibegin: " + stTest.ibegin); Console.WriteLine("stTest.li: " + stTest.li); Console.WriteLine("stTest.s: " + stTest.s); Console_PressAnyKey(); }