/// <summary> /// Serializes array of spans using Thrift protocol. /// </summary> public static void WriteSpans(Zipkin.Span[] spans, Stream outputStream) { var transport = new TStreamTransport(null, outputStream); var protocol = new TBinaryProtocol(transport); protocol.WriteListBegin(new TList(TType.Struct, spans.Length)); foreach (var span in spans) { var thrift = span.ToThrift(); thrift.Write(protocol); } protocol.WriteListEnd(); transport.Flush(); }
public static byte[] Serialize <T>(T data) where T : TBase { using (var memStream = new MemoryStream()) using (var binaryReader = new BinaryReader(memStream)) using (var thriftTransport = new TStreamTransport(null, memStream)) { var binaryProtocol = new TCompactProtocol(thriftTransport); data.Write(binaryProtocol); //TODO Refactor memStream.Position = 0; return(binaryReader.ReadBytes((int)memStream.Length)); } }
public static byte[] Serialize(object instance) { if (instance == null) { return(null); } var type = instance.GetType(); if (type.IsValueType || type == typeof(string) || type == typeof(byte[])) { var genericListType = typeof(ValueContainer <>).MakeGenericType(type); instance = Activator.CreateInstance(genericListType, instance); } else if (type.IsList()) { var genericType = type.GetGenericArguments()[0]; var genericListType = typeof(ListContainer <>).MakeGenericType(genericType); instance = Activator.CreateInstance(genericListType, instance); } else if (type.IsMap()) { var genericTypes = type.GetGenericArguments(); var genericTypeKey = genericTypes[0]; var genericTypeValue = genericTypes[1]; var genericMapType = typeof(MapContainer <,>).MakeGenericType(genericTypeKey, genericTypeValue); instance = Activator.CreateInstance(genericMapType, instance); } else if (type.IsArray) { var genericType = type.GetElementType(); var genericListType = typeof(ArrayContainer <>).MakeGenericType(genericType); instance = Activator.CreateInstance(genericListType, instance); } using (var stream = new MemoryStream()) using (var transport = new TStreamTransport(null, stream)) using (var protocol = new TBinaryProtocol(transport)) { protocol.WriteStruct(new TStruct(), instance); protocol.Transport.Flush(); return(stream.ToArray()); } }
public static byte[] SerializeCompact(TBase tbase) { if (tbase == null) { return(null); } using (MemoryStream outputStream = new MemoryStream()) { TStreamTransport transport = new TStreamTransport(null, outputStream); TProtocol protocol = new TCompactProtocol(transport); tbase.Write(protocol); return(outputStream.ToArray()); } }
public TObject Deserialize <TObject>(byte[] objectBytes) { var codec = _thriftCodecManager.GetCodec(typeof(TObject)) as IThriftCodec; using (var ins = new MemoryStream(objectBytes)) { using (var tt = new TStreamTransport(ins, null)) { using (var protocol = this.GetProtocol(tt)) { return((TObject)codec.ReadObject(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 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 T Read <T>(byte[] serializedStruct, TProtocolFactory protocolFactory) { Guard.ArgumentNotNull(serializedStruct, nameof(serializedStruct)); using (MemoryStream istream = new MemoryStream(serializedStruct)) { using (TStreamTransport resultIOStream = new TStreamTransport(istream, null)) { using (TProtocol resultProtocolBuffer = protocolFactory.GetProtocol(resultIOStream)) { return(Read <T>(resultProtocolBuffer)); } } } }
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); } }
public static object Deserialize(Type type, byte[] buffer) { if (buffer == null || buffer.Length == 0) { return(null); } var isContainer = false; if (type.IsValueType || type == typeof(string) || type == typeof(byte[])) { type = typeof(ValueContainer <>).MakeGenericType(type); isContainer = true; } else if (type.IsList()) { var genericArg = type.GetGenericArguments()[0]; type = typeof(ListContainer <>).MakeGenericType(genericArg); isContainer = true; } else if (type.IsMap()) { var genericTypes = type.GetGenericArguments(); var genericTypeKey = genericTypes[0]; var genericTypeValue = genericTypes[1]; type = typeof(MapContainer <,>).MakeGenericType(genericTypeKey, genericTypeValue); isContainer = true; } using (var stream = new MemoryStream(buffer)) using (var transport = new TStreamTransport(stream, null)) using (var protocol = new TBinaryProtocol(transport)) { var result = protocol.ReadStruct(type); if (isContainer) { return(type.GetProperty("Value").GetValue(result)); } return(result); } }
public byte[] Serialize <TObject>(TObject s) { byte[] objectBytes = null; var codec = _thriftCodecManager.GetCodec(typeof(TObject)) as IThriftCodec; using (var outs = new MemoryStream()) { using (var tt = new TStreamTransport(null, outs)) { using (var protocol = this.GetProtocol(tt)) { codec.WriteObject(s, protocol); objectBytes = outs.ToArray(); } } } return(objectBytes); }
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"); }
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); } }
public async Task ProcessRequestAsync(HttpContext context, CancellationToken cancellationToken) { var transport = new TStreamTransport(context.Request.Body, context.Response.Body, Configuration); try { var intrans = (InputTransportFactory != null) ? InputTransportFactory.GetTransport(transport) : transport; var outtrans = (OutputTransportFactory != null) ? OutputTransportFactory.GetTransport(transport) : transport; var input = InputProtocolFactory.GetProtocol(intrans); var output = OutputProtocolFactory.GetProtocol(outtrans); context.Response.ContentType = ContentType; while (await Processor.ProcessAsync(input, output, cancellationToken)) { if (!context.Response.HasStarted) // oneway method called { await context.Response.Body.FlushAsync(cancellationToken); } } } catch (TTransportException) { if (!context.Response.HasStarted) // if something goes bust, let the client know { context.Response.StatusCode = 500; // internal server error } } catch (TProtocolException) { if (!context.Response.HasStarted) // if something goes bust, let the client know { context.Response.StatusCode = 400; // bad request, e.g. required field missing } } finally { transport.Close(); } }
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); } }
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); }
//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); }
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); }
public async Task ProcessRequestAsync(HttpContext context, CancellationToken cancellationToken) { var transport = new TStreamTransport(context.Request.Body, context.Response.Body); try { var input = InputProtocolFactory.GetProtocol(transport); var output = OutputProtocolFactory.GetProtocol(transport); while (await Processor.ProcessAsync(input, output, cancellationToken)) { } } catch (TTransportException) { // Client died, just move on } finally { transport.Close(); } }
public override void Map(string inputLine, MapperContext context) { var delivery = new Delivery(); double result = 0.0; context.Log("MAPPER:::START"); context.Log(inputLine); context.Log("UTF-8: " + Encoding.UTF8.GetBytes(inputLine).Length); context.Log("ASCII: " + Encoding.ASCII.GetBytes(inputLine).Length); // Read the incoming string as a Thrift Binary serialized object var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputLine)); using (var transport = new TStreamTransport(inputStream, null)) { delivery.Read(new TBinaryProtocol(transport)); context.Log("MAPPER:::AFTER_READ"); // Get the driven kilometers from the vehicle's odometer sensor var sensorData = delivery.Vehicle.SensorHistory; var minOdo = sensorData.Min(d => d.OdoMeter); var maxOdo = sensorData.Max(d => d.OdoMeter); result = maxOdo - minOdo; context.Log("MAPPER:::BEFORE_STREAM_CLOSE"); } context.Log("MAPPER:::AFTER_STREAM_CLOSE"); // Emit the vehicle id, and the driven kilometers. if (result > 0.1) { context.EmitKeyValue(delivery.Vehicle.VehicleId, result.ToString(CultureInfo.InvariantCulture)); } context.Log("MAPPER:::END"); }
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName) { string contentType = controllerContext.HttpContext.Request.ContentType; if (contentType.StartsWith(ThriftMimeType, StringComparison.InvariantCultureIgnoreCase)) { var inputStream = GetThriftStream != null?GetThriftStream(controllerContext.HttpContext) : controllerContext.HttpContext.Request.InputStream; ThriftActionResult context = new ThriftActionResult(); TTransport trans = new TStreamTransport(inputStream, controllerContext.HttpContext.Response.OutputStream); if (contentType.Equals(ThriftMimeTypeJson)) { context.Output = context.Input = new TJSONProtocol(trans); } else if (contentType.Equals(ThriftMimeTypeCompact)) { context.Output = context.Input = new TCompactProtocol(trans); } else { context.Output = context.Input = new TBinaryProtocol(trans); } TMessage tmsg = context.Input.ReadMessageBegin(); context.SeqID = tmsg.SeqID; context.GetParameterValues = ProcessRequestMap[tmsg.Name]; context.ProcessResult = ProcessResultMap[tmsg.Name]; context.Name = tmsg.Name; actionName = context.Name; ThriftActionResult.SetThriftResult(controllerContext.HttpContext, context); } return(base.FindAction(controllerContext, controllerDescriptor, actionName)); }
public void Unpack(byte[] data) { stream = new MemoryStream(data); if (transport == null) { transport = new TStreamTransport(stream, null); } else { transport.InputStream = stream; } if (protocol == null) { protocol = new TCompactProtocol(transport); } else { protocol.reset(); protocol.Transport = transport; } protocol.ReadStructBegin(); while (true) { fieldReader = protocol.ReadFieldBegin(); if (fieldReader.Type == TType.Stop) { break; } switch (fieldReader.ID) { case 1: if (fieldReader.Type == TType.Map) { if (context == null) { context = new ADAGEEventInfoDictionary(); } else { context.Clear(); } TMap _map17 = protocol.ReadMapBegin(); for (int _i18 = 0; _i18 < _map17.Count; ++_i18) { tempKey = protocol.ReadString(); tempValue = new ADAGEEventInfo(); tempValue.Read(fieldReader, protocol); context[tempKey] = tempValue; } protocol.ReadMapEnd(); } else { TProtocolUtil.Skip(protocol, fieldReader.Type); } break; case 2: if (fieldReader.Type == TType.Struct) { if (events == null) { events = new ADAGEEventInfoDictionary(); } else { events.Clear(); } TMap _map17 = protocol.ReadMapBegin(); for (int _i18 = 0; _i18 < _map17.Count; ++_i18) { tempKey = protocol.ReadString(); tempValue = new ADAGEEventInfo(); tempValue.Read(fieldReader, protocol); events[tempKey] = tempValue; } protocol.ReadMapEnd(); } else { TProtocolUtil.Skip(protocol, fieldReader.Type); } break; default: TProtocolUtil.Skip(protocol, fieldReader.Type); break; } protocol.ReadFieldEnd(); } protocol.ReadStructEnd(); }
static void Main(string[] args) { if (args.Length < 4) { Console.WriteLine("server [ns] [hc] [keyname] [key]"); return; } var ns = args[0]; var hc = args[1]; var keyname = args[2]; var key = args[3]; var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyname, key); var sendAddress = new Uri($"sb://{ns}/{hc}"); var relayClient = new HybridConnectionClient(sendAddress, tokenProvider); try { var relayConnection = relayClient.CreateConnectionAsync().GetAwaiter().GetResult(); TTransport transport = new TStreamTransport(relayConnection, relayConnection); TProtocol protocol = new TBinaryProtocol(transport); Calculator.Client client = new Calculator.Client(protocol); transport.Open(); try { client.ping(); Console.WriteLine("ping()"); int sum = client.add(1, 1); Console.WriteLine("1+1={0}", sum); Work work = new Work(); work.Op = Operation.DIVIDE; work.Num1 = 1; work.Num2 = 0; try { int quotient = client.calculate(1, work); Console.WriteLine("Whoa we can divide by 0"); } catch (InvalidOperation io) { Console.WriteLine("Invalid operation: " + io.Why); } work.Op = Operation.SUBTRACT; work.Num1 = 15; work.Num2 = 10; try { int diff = client.calculate(1, work); Console.WriteLine("15-10={0}", diff); } catch (InvalidOperation io) { Console.WriteLine("Invalid operation: " + io.Why); } SharedStruct log = client.getStruct(1); Console.WriteLine("Check log: {0}", log.Value); } finally { transport.Close(); } } catch (TApplicationException x) { Console.WriteLine(x.StackTrace); } }
public async Task Run(string sendAddress, string sendToken) { try { var relayClient = new RelayClient(sendAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken)); var relayConnection = relayClient.Connect(); TTransport transport = new TStreamTransport(relayConnection, relayConnection); TProtocol protocol = new TBinaryProtocol(transport); Calculator.Client client = new Calculator.Client(protocol); transport.Open(); try { client.ping(); Console.WriteLine("ping()"); int sum = client.add(1, 1); Console.WriteLine("1+1={0}", sum); Work work = new Work(); work.Op = Operation.DIVIDE; work.Num1 = 1; work.Num2 = 0; try { int quotient = client.calculate(1, work); Console.WriteLine("Whoa we can divide by 0"); } catch (InvalidOperation io) { Console.WriteLine("Invalid operation: " + io.Why); } work.Op = Operation.SUBTRACT; work.Num1 = 15; work.Num2 = 10; try { int diff = client.calculate(1, work); Console.WriteLine("15-10={0}", diff); } catch (InvalidOperation io) { Console.WriteLine("Invalid operation: " + io.Why); } SharedStruct log = client.getStruct(1); Console.WriteLine("Check log: {0}", log.Value); } finally { transport.Close(); } } catch (TApplicationException x) { Console.WriteLine(x.StackTrace); } }
/// <summary> /// Builds a DataWriter that writes data to a stream. This uses the provided stream directly. /// </summary> /// <param name="outPipeStream">The stream to use</param> public DataWriter(Stream outPipeStream) { _outPipe = new TStreamTransport(null, outPipeStream); _outProto = new TBinaryProtocol(_outPipe); }