public static T FromXml(string xml) { TMemoryBuffer trans = new TMemoryBuffer(Encoding.UTF8.GetBytes(xml)); TProtocol prot = new TXmlProtocol(trans); return(Proto <T> .Read(prot)); }
static void UnitTest_14_TestMemoryBuffer() { Blog blog = new Blog(); blog.Content = new byte[] { 1 }; blog.CreatedTime = 2; blog.Id = "TestBlog"; blog.IpAddress = "HereIsIpAddress"; blog.Topic = "TestTopic"; Dictionary <string, string> tmp = new Dictionary <string, string>(); tmp.Add("testKey", "testValue"); blog.Props = tmp; TMemoryBuffer transport = new TMemoryBuffer(); TBinaryProtocol prot = new TBinaryProtocol(transport); blog.Write(prot); byte[] data = transport.GetBuffer(); //byte[] data = TMemoryBuffer.Serialize(blog); //Blog result = TMemoryBuffer.DeSerialize<Blog>(data); TMemoryBuffer transport2 = new TMemoryBuffer(data); TBinaryProtocol prot2 = new TBinaryProtocol(transport2); Blog result = new Blog(); result.Read(prot2); Console.WriteLine("Id " + result.Id + " IpAddress " + result.IpAddress + " Props.testKey " + result.Props["testKey"]); }
public static T FromJson(string json) { TMemoryBuffer trans = new TMemoryBuffer(Encoding.UTF8.GetBytes(json)); TProtocol prot = new TSimpleJSONProtocol(trans); return(Proto <T> .Read(prot)); }
public static T FromBytes(byte[] bytes) { TMemoryBuffer trans = new TMemoryBuffer(bytes); TProtocol prot = new TCompactProtocol(trans); return(Proto <T> .Read(prot)); }
static void DumpThrift(string url, CookieCollection cookies) { List <webcrap.web.Cookie> list = new List <webcrap.web.Cookie>(); foreach (System.Net.Cookie cookie in cookies) { webcrap.web.Cookie c = new webcrap.web.Cookie { Name = cookie.Name, Value = cookie.Value, Version = cookie.Version, Path = cookie.Path, Domain = cookie.Domain, Port = cookie.Port, Comment = cookie.Comment, CommentUri = cookie.CommentUri?.AbsolutePath, Expired = cookie.Expired, Expires = cookie.Expires.ToUniversalTime().Ticks, HttpOnly = cookie.HttpOnly, Secure = cookie.Secure, Timestamp = cookie.TimeStamp.ToUniversalTime().Ticks }; list.Add(c); } var dict = new Dictionary <string, List <webcrap.web.Cookie> >(); dict.Add(url, list); Cookies cc = new Cookies { Cookies_ = dict }; TByteBuffer trans = new TByteBuffer(102400); TProtocol prot = new TSimpleJSONProtocol(trans); cc.Write(prot); string json = Encoding.UTF8.GetString(trans.GetBuffer(), 0, trans.Length); Console.WriteLine(json); Console.WriteLine(); TMemoryBuffer trans1 = new TMemoryBuffer(Encoding.UTF8.GetBytes(json)); TProtocol prot1 = new TSimpleJSONProtocol(trans1); Cookies cs = new Cookies(); cs.Read(prot1); TByteBuffer trans2 = new TByteBuffer(102400); TProtocol prot2 = new TSimpleJSONProtocol(trans2); cs.Write(prot2); string json2 = Encoding.UTF8.GetString(trans2.GetBuffer(), 0, trans2.Length); Console.WriteLine(json2); Console.WriteLine(); }
public static byte[] ToBytes(T value) { TMemoryBuffer trans = new TMemoryBuffer(); TProtocol prot = new TCompactProtocol(trans); Proto <T> .Write(prot, value); return(trans.GetBuffer()); }
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()); }
public static string GetJson(T value) { TMemoryBuffer trans = new TMemoryBuffer(); TProtocol prot = new TSimpleJSONProtocol(trans); Proto <T> .Write(prot, value); byte[] buffer = trans.GetBuffer(); return(Encoding.UTF8.GetString(buffer)); }
public static string GetXml(T value) { TMemoryBuffer trans = new TMemoryBuffer(); TXmlProtocol prot = new TXmlProtocol(trans); Proto <T> .Write(prot, value); trans.Flush(); return(Encoding.UTF8.GetString(trans.GetBuffer())); }
public static T Deserialize <T>(byte[] buffer) where T : new() { using (TMemoryBuffer trans = new TMemoryBuffer(buffer)) { TProtocol proto = new TCompactProtocol(trans); TBase deserialized = (TBase) new T(); deserialized.Read(proto); return((T)deserialized); } }
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); }
public static byte[] SerializeGetBytes(object value) { using (TMemoryBuffer trans = new TMemoryBuffer()) { TProtocol proto = new TCompactProtocol(trans); TBase trade = (TBase)value; trade.Write(proto); byte[] bytes = trans.GetBuffer(); return(bytes); } }
public StreamBuffer GetStream() { TMemoryBuffer membuf = new TMemoryBuffer(); TCompactProtocol protocal = new TCompactProtocol(membuf); Write(protocal); byte[] buf = membuf.GetBuffer(); StreamBuffer stream = new StreamBuffer((int)_messageId, buf); return(stream); }
public static T Deserialize <T>(byte[] data) where T : TBase { T instance = Activator.CreateInstance <T>(); var buffer = new TMemoryBuffer(data); using (TProtocol tProtocol = new TBinaryProtocol(buffer)) { instance.Read(tProtocol); return(instance); } }
public static Stream Serialize(object value) { using (TMemoryBuffer trans = new TMemoryBuffer()) { TProtocol proto = new TCompactProtocol(trans); TBase trade = (TBase)value; trade.Write(proto); byte[] bytes = trans.GetBuffer(); var memoryStream = new MemoryStream(bytes); return(memoryStream); } }
public static ProtoBase ConstructThriftProto(StreamBuffer sb) { MessageId messageId = (MessageId)sb.m_protocolHeader.ProtocolID; if (s_protoConstructorDic.ContainsKey(messageId)) { TMemoryBuffer membuf = new TMemoryBuffer(sb.ByteBuffer); TCompactProtocol inProto = new TCompactProtocol(membuf); ProtoBase proto = s_protoConstructorDic[messageId].Invoke(inProto); return(proto); } return(null); }
/// <inheritdoc /> /// <param name="maxPacketSize">The max byte size of UDP packets to be sent to the agent</param> /// <param name="sender">The ISender that will take care of the actual sending as well as storing the items to be sent</param> /// <param name="serialization">The object that is used to serialize the spans into Thrift</param> internal JaegerUdpTransport(int maxPacketSize, ISender sender, ISerialization serialization = null) : base(sender, serialization) { // Each span is first written to thriftBuffer to determine its size in bytes. _thriftBuffer = new TMemoryBuffer(); _thriftProtocol = _sender.ProtocolFactory.GetProtocol(_thriftBuffer); if (maxPacketSize == 0) { maxPacketSize = TransportConstants.UdpPacketMaxLength; } _maxByteBufferSize = maxPacketSize - TransportConstants.UdpEmitBatchOverhead; }
private static int CalcSizeOfSerializedThrift(TBase thriftBase) { var thriftBuffer = new TMemoryBuffer(); var protocolFactory = new TCompactProtocol.Factory(); var thriftProtocol = protocolFactory.GetProtocol(thriftBuffer); try { thriftBase.WriteAsync(thriftProtocol, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception e) { throw new Exception("failed to calculate the size of a serialized thrift object", e); } return(thriftBuffer.GetBuffer().Length); }
public static T Deserialize <T>(Stream stream) where T : new() { byte[] buffer; using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); buffer = memoryStream.ToArray(); } using (TMemoryBuffer trans = new TMemoryBuffer(buffer)) { TProtocol proto = new TCompactProtocol(trans); TBase deserialized = (TBase) new T(); deserialized.Read(proto); return((T)deserialized); } }
public async void EmitBatchOverhead_ShouldNotGoOverOverheadConstant() { var transport = new TMemoryBuffer(); var protocolFactory = new TCompactProtocol.Factory(); var client = new Agent.Client(protocolFactory.GetProtocol(transport)); var jSpan = new JaegerSpan(10, 11, 12, 13, "opName", 0, 1234, 1235); var jSpanSize = CalcSizeOfSerializedThrift(jSpan); var tests = new[] { 1, 2, 14, 15, 377, 500, 65000, 0xFFFF }; foreach (var test in tests) { transport.Reset(); var batch = new List <JaegerSpan>(); var processTags = new List <JaegerTag>(); for (var j = 0; j < test; j++) { batch.Add(jSpan); processTags.Add(new JaegerTag("testingTag", TagType.BINARY) { VBinary = new byte[] { 0x20 } }); } var jProcess = new JaegerProcess("testing") { Tags = processTags }; await client.emitBatchAsync(new JaegerBatch(jProcess, batch), CancellationToken.None); var jProcessSize = CalcSizeOfSerializedThrift(jProcess); var overhead = transport.GetBuffer().Length - test * jSpanSize - jProcessSize; Assert.True(overhead <= TransportConstants.UdpEmitBatchOverhead); } }
/// <param name="protocolType">Protocol type (compact or binary)</param< /// <param name="maxPacketSize">If 0 it will use default value <see cref="ThriftUdpTransport.MAX_PACKET_SIZE"/>.</param> public ThriftSenderBase(ProtocolType protocolType, int maxPacketSize) { switch (protocolType) { case ProtocolType.Binary: ProtocolFactory = new TBinaryProtocol.Factory(); break; case ProtocolType.Compact: ProtocolFactory = new TCompactProtocol.Factory(); break; default: throw new NotSupportedException("Unknown thrift protocol type specified: " + protocolType); } if (maxPacketSize == 0) { maxPacketSize = ThriftUdpClientTransport.MaxPacketSize; } MaxSpanBytes = maxPacketSize - EmitBatchOverhead; _memoryTransport = new TMemoryBuffer(); }
void ParsePacket() { int nCursor = 0; int nMsgTotalLength = BitConverter.ToInt32(m_buffer, nCursor); nCursor += sizeof(Int32); Array.Reverse(m_buffer, nCursor, sizeof(Int32)); int nHeaderLength = BitConverter.ToInt32(m_buffer, nCursor); nCursor += sizeof(Int32); byte[] headerBytes = new byte[nHeaderLength]; Array.Copy(m_buffer, nCursor, headerBytes, 0, nHeaderLength); nCursor += nHeaderLength; Array.Reverse(m_buffer, nCursor, sizeof(Int32)); int nBodyLength = BitConverter.ToInt32(m_buffer, nCursor); nCursor += sizeof(Int32); byte[] bodyBytes = new byte[nBodyLength]; Array.Copy(m_buffer, nCursor, bodyBytes, 0, nBodyLength); nCursor += nBodyLength; TMemoryBuffer trans = new TMemoryBuffer(headerBytes); TBinaryProtocol proto = new TBinaryProtocol(trans); ProtocolHeader msgThriftHead = new ProtocolHeader(); msgThriftHead.Read(proto); if (m_msgHash.ContainsKey(msgThriftHead.ProtocolHash)) { MsgReceiveBase msgRec = Activator.CreateInstance(m_msgHash[msgThriftHead.ProtocolHash]) as MsgReceiveBase; msgRec.ParsePacket(bodyBytes); m_queWillDo.Enqueue(msgRec); } m_nMsgLength = 0; m_nCurReadPos = 0; // CloseSocket(); }
public byte[] T_W_Byte() { return(TMemoryBuffer.Serialize((T.ByteContainer)ByteContainer.Sample)); }
public byte[] T_W_Binary() { return(TMemoryBuffer.Serialize((T.BinaryContainer)BinaryContainer.Sample)); }
public byte[] T_W_All() { return(TMemoryBuffer.Serialize((T.AllTypesContainer)AllTypesContainer.Sample)); }
public object T_R_TimePeriod() { return(TMemoryBuffer.DeSerialize <T.TimePeriod>(T_TimePeriod)); }
public object T_R_Struct() { return(TMemoryBuffer.DeSerialize <T.StructContainer>(T_Struct)); }
public object T_R_String() { return(TMemoryBuffer.DeSerialize <T.StringContainer>(T_String)); }
public object T_R_PersonAvailability() { return(TMemoryBuffer.DeSerialize <T.PersonAvailability>(T_PersonAvailability)); }
public object T_R_Person() { return(TMemoryBuffer.DeSerialize <T.Person>(T_Person)); }