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); } }
bool Receive() { try { var msg = socket?.ReceiveMultipartMessage(); if (msg == null || PubsubUtil.IsStoppingMessage(msg)) { PublisherLogger.Info("subscribe client stopped by stpping message!"); return(false); } var topic = msg[0].ConvertToString(); var msgType = msg[1].ConvertToString(); if (MsgHandlers.TryGetValue(msgType, out Delegate mh)) { var respBytes = msg[2].Buffer; var stream = new MemoryStream(respBytes, 0, respBytes.Length); using (TStreamTransport trans = new TStreamTransport(stream, stream)) using (TBinaryProtocol proto = new TBinaryProtocol(trans)) { Type t = GeneratedTypeCache.GetType(msgType); if (t == null) { throw new TargetInvocationException(new Exception($"can't get type for: {msgType}")); } TBase ret = Activator.CreateInstance(t) as TBase; ret.Read(proto); mh.DynamicInvoke(topic, ret); } } return(true); } catch (Exception e) { if (e is TargetInvocationException) { var ee = e as TargetInvocationException; Log($"SubscribeClient, invoke exception: {(ee).InnerException.Message} \n {ee.InnerException.StackTrace}", Logging.LogLevel.Warn); return(true); } if (e is TerminatingException) { Log($"SubscribeClient, terminated: {e.Message}", Logging.LogLevel.Info); return(false); } else { Log(disposing ? $"SubscribeClient, disposing exception: {e.Message}" : $"subscribe client receive exception: {e.Message} \n {e.StackTrace}", disposing ? Logging.LogLevel.Info : Logging.LogLevel.Error ); return(false); } } }
public TBase Deserialize(byte[] buffer) { Reset(); baos.Write(buffer, 0, buffer.Length); baos.Seek(0, SeekOrigin.Begin); baos = new MemoryStream(buffer); Header header = ReadHeader(); TBase @base = locator.TBaseLookup(header.Type); @base.Read(protocol); return(@base); }
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 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); } }
/// <summary> /// deSerialize msg data to thrift msg object /// </summary> /// <param name="tbase"></param> /// <param name="bytes"></param> public static void DeSerialize(TBase tbase, byte[] bytes) { if (tbase == null || bytes == null) { return; } INPUT_STREAM.Seek(0, SeekOrigin.Begin); INPUT_STREAM.SetLength(0); INPUT_STREAM.Write(bytes, 0, bytes.Length); INPUT_STREAM.Seek(0, SeekOrigin.Begin); tbase.Read(DESERIALIZE_PROTOCOL); }
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 void DeSerialize(TBase tbase, byte[] bytes, int startPos, int length) { if (tbase == null || bytes == null) { return; } // init input stream inputStream.Seek(0, SeekOrigin.Begin); inputStream.SetLength(0); // write data to input stream inputStream.Write(bytes, startPos, length); inputStream.Seek(0, SeekOrigin.Begin); // deSerialize object by protocol tbase.Read(deSerializeProtocol); }
public static void Deserialize(byte[] buffer, TBase message) { MemoryStream inputStream = new MemoryStream(buffer); TStreamTransport transport = new TStreamTransport(inputStream, null); TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport); message.Read(binaryProtocol); }