/** * This is an example function that creates an Avro schema object based on pre-defined Avro JSON-like schema. It then * adds some example data into the record fields, creates a buffer associated with the record, and sends the buffer * out through UDP connection. **/ public static void sender() { /** * Creating a schema object by loading schema object file (.avsc) * Example.AVSC looks like this: * * {"namespace": "example.avro", * "type": "record", * "name": "User", * "fields": [ * {"name": "name", "type": "string"}, * {"name": "favorite_number", "type": ["int", "null"]}, * {"name": "favorite_color", "type": ["string", "null"]} * ] * } **/ var schema = RecordSchema.Parse(File.ReadAllText(@"C:\Users\user\src\example.avsc")) as RecordSchema; //Passing in schema object to get a record object var exampleRecorder = new GenericRecord(schema); //Filling out records with the corresponding schema exampleRecorder.Add("name", "myExample"); exampleRecorder.Add("favorite_number", 999); exampleRecorder.Add("favorite_color", "read"); //Creating an Avro buffer stream ByteBufferOutputStream buffer = new ByteBufferOutputStream(); //Wraping the buffer stream with the encoder that does low level serialization Avro.IO.Encoder encoder = new BinaryEncoder(buffer); //Creating a writer with the corresponding schema object var writer = new DefaultWriter(schema); //Write (serialize) record object into buffer outputStream with encoder writer.Write <GenericRecord>(exampleRecorder, encoder); //And flush buffer.Flush(); //Creating a UDP client UdpClient udpClient = new UdpClient(0); //Connect to endpoint with host and port number arguments udpClient.Connect("my_udp_end_point.com", 9999); //Get buffer list from buffer stream List <MemoryStream> bufferList = buffer.GetBufferList(); //For each memory stream, creating a byte array, and deliver the byte array to endpoint //You actually do not need a foreach loop, because you will only have one memory stream foreach (MemoryStream ms in bufferList) { byte[] bufferArray; bufferArray = ms.ToArray(); udpClient.Send(bufferArray, bufferArray.Length); } udpClient.Close(); }
public void decompress(ByteBuffer @in, ByteBuffer @out) { using (ByteBufferInputStream input = new ByteBufferInputStream(@in.duplicate())) using (DeflateStream inflater = new DeflateStream(input, CompressionMode.Decompress)) using (ByteBufferOutputStream output = new ByteBufferOutputStream(@out)) { inflater.CopyTo(output); } @out.flip(); @in.position(@in.limit()); }
public bool compress(ByteBuffer @in, ByteBuffer @out, ByteBuffer overflow) { using (ByteBufferOutputStream output = new ByteBufferOutputStream(@out, overflow)) { using (DeflateStream deflater = new DeflateStream(output, level)) using (ByteBufferInputStream input = new ByteBufferInputStream(@in.duplicate())) { input.CopyTo(deflater); } return output.Okay; } }
public bool compress(ByteBuffer @in, ByteBuffer @out, ByteBuffer overflow) { using (ByteBufferOutputStream output = new ByteBufferOutputStream(@out, overflow)) { using (DeflateStream deflater = new DeflateStream(output, level)) using (ByteBufferInputStream input = new ByteBufferInputStream(@in.duplicate())) { input.CopyTo(deflater); } return(output.Okay); } }
public byte[] Serialize(GenericRecord payload) { var buffer = new ByteBufferOutputStream(); var encoder = new BinaryEncoder(buffer); var writer = new DefaultWriter(Schema); writer.Write <GenericRecord>(payload, encoder); buffer.Flush(); var streams = buffer.GetBufferList(); return(streams[0].ToArray()); }
public Protocol GetRemote() { if (remoteProtocol != null) { return(remoteProtocol); // already have it } lock (remoteHashLock) { MD5 remoteHash; if (RemoteHashes.TryGetValue(transceiver.RemoteName, out remoteHash)) { lock (remoteProtocolsLock) { remoteProtocol = RemoteProtocols[remoteHash]; if (remoteProtocol != null) { return(remoteProtocol); // already cached } } } } Monitor.Enter(handshakeLock); try { // force handshake var bbo = new ByteBufferOutputStream(); // direct because the payload is tiny. Encoder outp = new BinaryEncoder(bbo); WriteHandshake(outp); outp.WriteInt(0); // empty metadata outp.WriteString(""); // bogus message name IList <MemoryStream> response = Transceiver.Transceive(bbo.GetBufferList()); var bbi = new ByteBufferInputStream(response); var inp = new BinaryDecoder(bbi); ReadHandshake(inp); return(remoteProtocol); } finally { Monitor.Exit(handshakeLock); } }
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); }
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()); }