public IList <MemoryStream> GetBytes(Protocol local, Requestor requestor) { if (local == null) { throw new ArgumentNullException("local"); } if (requestor == null) { throw new ArgumentNullException("requestor"); } if (requestBytes == null) { using (var bbo = new ByteBufferOutputStream()) { var o = new BinaryEncoder(bbo); // use local protocol to write request Message m = GetMessage(local); Context.Message = m; requestor.WriteRequest(m.Request, request, o); // write request payload o.Flush(); List <MemoryStream> payload = bbo.GetBufferList(); requestor.WriteHandshake(o); // prepend handshake if needed Context.RequestPayload = payload; IDictionary <string, object> responseCallMeta = Context.ResponseCallMeta; Requestor.MetaWriter.Write(responseCallMeta, o); o.WriteString(m.Name); // write message name o.Flush(); bbo.Append(payload); requestBytes = bbo.GetBufferList(); } } return(requestBytes); }
private static GenericRecord ConvertSpecificToGeneric <T>(T obj, Schema schema) { var stream = new MemoryStream(); var encoder = new BinaryEncoder(stream); var decoder = new BinaryDecoder(stream); var writer = new SpecificWriter <T>(schema); writer.Write(obj, encoder); encoder.Flush(); stream.Position = 0; return(new GenericReader <GenericRecord>(schema, schema).Read(null, decoder)); }
static void Main(string[] args) { GenericRecord originalEvent; GenericRecord deserializedEvent; var timestamp = DateTime.UtcNow; // Create a UserLoginEvent using GenericRecord originalEvent = new GenericRecord(UserLoginEventSchema); originalEvent.Add("timestamp", DateTimeToAvroTimestampMillis(timestamp)); originalEvent.Add("userID", "blachniet"); originalEvent.Add("wasSuccessful", true); using (var memoryStream = new MemoryStream()) { // Write the record to a memory stream { var binaryEncoder = new BinaryEncoder(memoryStream); var defaultWriter = new DefaultWriter(UserLoginEventSchema); defaultWriter.Write(originalEvent, binaryEncoder); binaryEncoder.Flush(); } // Reset the stream position before we read it memoryStream.Position = 0; // Read the record from the memory stream { var binaryDecoder = new BinaryDecoder(memoryStream); var defaultReader = new DefaultReader(UserLoginEventSchema, UserLoginEventSchema); deserializedEvent = defaultReader.Read <GenericRecord>(null, binaryDecoder); } } Console.WriteLine($@" Original Event: timestamp : {originalEvent["timestamp"]} userID : {originalEvent["userID"]} wasSuccessful : {originalEvent["wasSuccessful"]} Deserialized Event: timestamp : {deserializedEvent["timestamp"]} userID : {deserializedEvent["userID"]} wasSuccessful : {deserializedEvent["wasSuccessful"]} Press 'Enter' to exit. ".TrimStart()); Console.ReadLine(); }
/// <inheritdoc /> public override void Serialize(Stream stream, object value, Type inputType, CancellationToken cancellationToken) { Argument.AssertNotNull(stream, nameof(stream)); Argument.AssertNotNull(value, nameof(value)); Argument.AssertNotNull(inputType, nameof(inputType)); var supportedType = GetSupportedTypeOrThrow(inputType); var writer = GetWriterAndSchema(value, supportedType, out var schema); var schemaId = GetSchemaId(schema, cancellationToken); var binaryEncoder = new BinaryEncoder(stream); stream.Write(EmptyRecordFormatIndicator, 0, RecordFormatIndicatorLength); stream.Write(Utf8Encoding.GetBytes(schemaId), 0, SchemaIdLength); writer.Write(value, binaryEncoder); binaryEncoder.Flush(); }
private byte[] SpecificRecordsToAvro <T>(T record) where T : ISpecificRecord { using (MemoryStream outputStream = new MemoryStream()) { SpecificDatumWriter <T> writer = new SpecificDatumWriter <T>(record.Schema); BinaryEncoder encoder = new BinaryEncoder(outputStream); for (int i = 0; i < _numberOfRecordsInAvro; i++) { writer.Write(record, encoder); } encoder.Flush(); return(outputStream.ToArray()); } }
private byte[] GenericRecordsToAvro(GenericRecord record) { using (MemoryStream outputStream = new MemoryStream()) { GenericDatumWriter <GenericRecord> writer = new GenericDatumWriter <GenericRecord>(record.Schema); BinaryEncoder encoder = new BinaryEncoder(outputStream); for (int i = 0; i < _numberOfRecordsInAvro; i++) { writer.Write(record, encoder); } encoder.Flush(); return(outputStream.ToArray()); } }
/// <inheritdoc /> public override async ValueTask SerializeAsync(Stream stream, object value, Type inputType, CancellationToken cancellationToken) { Argument.AssertNotNull(stream, nameof(stream)); Argument.AssertNotNull(value, nameof(value)); Argument.AssertNotNull(inputType, nameof(inputType)); var supportedType = GetSupportedTypeOrThrow(inputType); var writer = GetWriterAndSchema(value, supportedType, out var schema); var schemaId = await GetSchemaIdAsync(schema, cancellationToken).ConfigureAwait(false); var binaryEncoder = new BinaryEncoder(stream); await stream.WriteAsync(EmptyRecordFormatIndicator, 0, RecordFormatIndicatorLength, cancellationToken).ConfigureAwait(false); await stream.WriteAsync(Utf8Encoding.GetBytes(schemaId), 0, SchemaIdLength, cancellationToken).ConfigureAwait(false); writer.Write(value, binaryEncoder); binaryEncoder.Flush(); }
public byte[] Write(IGenericRecord message) { lock (this) { try { _writer.Write(((GenericAvroRecord)message).AvroRecord, _encoder); _encoder.Flush(); var bytes = _byteArrayOutputStream.ToArray(); return(bytes); } catch (Exception e) { throw new SchemaSerializationException(e); } finally { _byteArrayOutputStream.SetLength(0); } } }
public IList <MemoryStream> Respond(IList <MemoryStream> buffers, Transceiver connection) { Decoder input = new BinaryDecoder(new ByteBufferInputStream(buffers)); var bbo = new ByteBufferOutputStream(); var output = new BinaryEncoder(bbo); Exception error = null; var context = new RpcContext(); List <MemoryStream> handshake = null; bool wasConnected = connection != null && connection.IsConnected; try { Protocol remote = Handshake(input, output, connection); output.Flush(); if (remote == null) // handshake failed { return(bbo.GetBufferList()); } handshake = bbo.GetBufferList(); // read request using remote protocol specification context.RequestCallMeta = META_READER.Read(null, input); String messageName = input.ReadString(); if (messageName.Equals("")) // a handshake ping { return(handshake); } Message rm = remote.Messages[messageName]; if (rm == null) { throw new AvroRuntimeException("No such remote message: " + messageName); } Message m = Local.Messages[messageName]; if (m == null) { throw new AvroRuntimeException("No message named " + messageName + " in " + Local); } Object request = ReadRequest(rm.Request, m.Request, input); context.Message = rm; // create response using local protocol specification if ((m.Oneway.GetValueOrDefault() != rm.Oneway.GetValueOrDefault()) && wasConnected) { throw new AvroRuntimeException("Not both one-way: " + messageName); } Object response = null; try { response = Respond(m, request); context.Response = response; } catch (Exception e) { error = e; context.Error = error; log.Warn("user error", e); } if (m.Oneway.GetValueOrDefault() && wasConnected) // no response data { return(null); } output.WriteBoolean(error != null); if (error == null) { WriteResponse(m.Response, response, output); } else { try { WriteError(m.SupportedErrors, error, output); } catch (Exception) { // Presumably no match on the exception, throw the original throw error; } } } catch (Exception e) { // system error log.Warn("system error", e); context.Error = e; bbo = new ByteBufferOutputStream(); output = new BinaryEncoder(bbo); output.WriteBoolean(true); WriteError(errorSchema /*Protocol.SYSTEM_ERRORS*/, e.ToString(), output); if (null == handshake) { handshake = new ByteBufferOutputStream().GetBufferList(); } } output.Flush(); List <MemoryStream> payload = bbo.GetBufferList(); // Grab meta-data from plugins context.ResponsePayload = payload; META_WRITER.Write(context.ResponseCallMeta, output); output.Flush(); // Prepend handshake and append payload bbo.Prepend(handshake); bbo.Append(payload); return(bbo.GetBufferList()); }