/// <summary> /// For KeyValueSchema, please make use of Value<TK, TV>(T value) /// to supply the key and value type /// </summary> /// <param name="value"></param> /// <returns></returns> public ITypedMessageBuilder <T> Value(T value) { if (value == null) { _metadata.NullValue = true; return(this); } if (_schema.SchemaInfo != null && _schema.SchemaInfo.Type == SchemaType.KeyValue) { throw new Exception("Get method only support non keyvalue schema"); } _content = new ReadOnlySequence <byte>(_schema.Encode(value)); return(this); }
private void TestBytesSchema(ISchema <byte[]> schema) { var data = Encoding.UTF8.GetBytes("hello world"); var serializedData = schema.Encode(data); Assert.Same(data, serializedData); var deserializedData = schema.Decode(serializedData); Assert.Same(data, deserializedData); }
// encode as bytes: [key.length][key.bytes][value.length][value.bytes] or [value.bytes] public virtual byte[] Encode(KeyValue <K, V> Message) { if (_keyValueEncodingType == KeyValueEncodingType.INLINE) { return(KeyValue <K, V> .Encode(Message.Key, _keySchema, Message.Value, _valueSchema)); } else { if (Message.Value == null) { return(null); } return(_valueSchema.Encode(Message.Value)); } }
public void SendMessage(string message, string toIP, string dateTime) { string EncodeMsg; bool status; string fromIP = "127.0.0.1"; ///string frIP = fromIP.ToString(); /// var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { fromIP = ip.ToString(); } } IPAddress targetAddress = IPAddress.Parse(toIP); Dictionary <string, string> Msg = new Dictionary <string, string>(); Msg["Msg"] = message; Msg["fromIP"] = fromIP; Msg["toIP"] = toIP; Msg["flag"] = "True"; Msg["dateTime"] = dateTime; //added time stamp to message and using schemaObj encode the message EncodeMsg = schemaObj.Encode(Msg); //bool Send(string msg, ulong dataID, IPAddress targetIP, DataType type); //using Icommunicate send this encoded message to communication status = comm.Send(EncodeMsg, targetAddress, DataType.Message); }
/// <summary> /// Encode a <tt>key</tt> and <tt>value</tt> pair into a bytes array. /// </summary> /// <param name="key"> key object to encode </param> /// <param name="keyWriter"> a writer to encode key object </param> /// <param name="value"> value object to encode </param> /// <param name="valueWriter"> a writer to encode value object </param> /// <returns> the encoded bytes array </returns> public static byte[] Encode(TK key, ISchema <TK> keyWriter, TV value, ISchema <TV> valueWriter) { var keyBytes = keyWriter.Encode(key); var valueBytes = valueWriter.Encode(value); var result = new byte[4 + keyBytes.Length + 4 + valueBytes.Length]; using var stream = new MemoryStream(result); using var binaryWriter = new BinaryWriter(stream); binaryWriter.Write(keyBytes.Length.IntToBigEndian()); binaryWriter.Write(keyBytes); binaryWriter.Write(valueBytes.Length.IntToBigEndian()); binaryWriter.Write(valueBytes); return(result); }
public async ValueTask <MessageId> Send(MessageMetadata metadata, TMessage message, CancellationToken cancellationToken) => await _executor.Execute(() => InternalSend(metadata.Metadata, _schema.Encode(message), cancellationToken), cancellationToken).ConfigureAwait(false);
public async ValueTask <MessageId> Send(TMessage message, CancellationToken cancellationToken) => await Send(_schema.Encode(message), cancellationToken).ConfigureAwait(false);