Exemplo n.º 1
0
 IEnumerable <IKMessage <TNodeId> > Decode(IKMessageContext <TNodeId> resources, RepeatedField <Message> messages)
 {
     foreach (var message in messages)
     {
         yield return(Decode(resources, message));
     }
 }
        PingResponse Encode(IKMessageContext <TNodeId> context, KPingResponse <TNodeId> response)
        {
            var r = new PingResponse();

            r.Endpoints.Add(response.Endpoints.Select(i => i.ToString()));
            return(r);
        }
 IEnumerable <Message> Encode(IKMessageContext <TNodeId> context, IEnumerable <IKMessage <TNodeId> > messages)
 {
     foreach (var message in messages)
     {
         yield return(Encode(context, message));
     }
 }
Exemplo n.º 4
0
        void Write(Utf8JsonWriter writer, IKMessageContext <TNodeId> context, TNodeId nodeId)
        {
            var a = (Span <byte>) stackalloc byte[KNodeId <TNodeId> .SizeOf];

            nodeId.Write(a);
            writer.WriteBase64StringValue(a);
        }
        PingRequest Encode(IKMessageContext <TNodeId> context, KPingRequest <TNodeId> request)
        {
            var r = new PingRequest();

            r.Endpoints.Add(request.Endpoints.Select(i => i.ToString()));
            return(r);
        }
        ByteString Encode(IKMessageContext <TNodeId> context, TNodeId nodeId)
        {
            var a = (Span <byte>) stackalloc byte[KNodeId <TNodeId> .SizeOf];

            nodeId.Write(a);
            return(ByteString.CopyFrom(a));
        }
Exemplo n.º 7
0
 IEnumerable <IKMessage <TNodeId> > DecodeMessages(IKMessageContext <TNodeId> context, JsonElement element)
 {
     foreach (var message in element.EnumerateArray())
     {
         yield return(DecodeMessage(context, message));
     }
 }
Exemplo n.º 8
0
        public KMessageSequence <TNodeId> Decode(IKMessageContext <TNodeId> context, ReadOnlySequence <byte> buffer)
        {
            var p = global::MessagePack.MessagePackSerializer.Deserialize <MessageSequence>(buffer);
            var s = new KMessageSequence <TNodeId>(p.Network, Decode(context, p.Messages));

            return(s);
        }
        Message Encode(IKMessageContext <TNodeId> context, IKResponse <TNodeId> message)
        {
            var m = new Message();

            m.Response        = new Response();
            m.Response.Header = Encode(context, message.Header);

            switch (message)
            {
            case KResponse <TNodeId, KPingResponse <TNodeId> > ping:
                m.Response.PingResponse = Encode(context, ping.Body.Value);
                break;

            case KResponse <TNodeId, KStoreResponse <TNodeId> > store:
                m.Response.StoreResponse = Encode(context, store.Body.Value);
                break;

            case KResponse <TNodeId, KFindNodeResponse <TNodeId> > findNode:
                m.Response.FindNodeResponse = Encode(context, findNode.Body.Value);
                break;

            case KResponse <TNodeId, KFindValueResponse <TNodeId> > findValue:
                m.Response.FindValueResponse = Encode(context, findValue.Body.Value);
                break;

            default:
                throw new InvalidOperationException();
            }

            return(m);
        }
Exemplo n.º 10
0
        public KMessageSequence <TNodeId> Decode(IKMessageContext <TNodeId> resources, ReadOnlySequence <byte> buffer)
        {
            var p = MessageSequence.Parser.ParseFrom(buffer);
            var s = new KMessageSequence <TNodeId>(p.Network, Decode(resources, p.Messages));

            return(s);
        }
 StoreRequest.Types.StoreRequestMode Encode(IKMessageContext <TNodeId> context, KStoreRequestMode mode)
 {
     return(mode switch
     {
         KStoreRequestMode.Primary => StoreRequest.Types.StoreRequestMode.Primary,
         KStoreRequestMode.Replica => StoreRequest.Types.StoreRequestMode.Replica,
         _ => throw new InvalidOperationException(),
     });
        public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> sequence)
        {
            var p = new MessageSequence();

            p.Network = sequence.Network;
            p.Messages.AddRange(Encode(context, sequence));
            p.WriteTo(buffer);
        }
        Header Encode(IKMessageContext <TNodeId> context, KMessageHeader <TNodeId> header)
        {
            var h = new Header();

            h.Sender  = Encode(context, header.Sender);
            h.ReplyId = header.ReplyId;
            return(h);
        }
Exemplo n.º 14
0
 IKMessage <TNodeId> Decode(IKMessageContext <TNodeId> context, Message message)
 {
     return(message.MessageCase switch
     {
         Message.MessageOneofCase.Request => Decode(context, message.Request),
         Message.MessageOneofCase.Response => Decode(context, message.Response),
         _ => throw new InvalidOperationException(),
     });
        public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> sequence)
        {
            var p = new MessageSequence();

            p.Network  = sequence.Network;
            p.Messages = Encode(context, sequence).ToArray();
            global::MessagePack.MessagePackSerializer.Serialize(buffer, p);
        }
 Message Encode(IKMessageContext <TNodeId> context, IKMessage <TNodeId> message)
 {
     return(message switch
     {
         IKRequest <TNodeId> request => Encode(context, request),
         IKResponse <TNodeId> response => Encode(context, response),
         _ => throw new InvalidOperationException(),
     });
Exemplo n.º 17
0
 void Write(Utf8JsonWriter writer, IKMessageContext <TNodeId> context, KResponseStatus status)
 {
     writer.WriteStringValue(status switch
     {
         KResponseStatus.Success => "SUCCESS",
         KResponseStatus.Failure => "FAILURE",
         _ => throw new InvalidOperationException(),
     });
Exemplo n.º 18
0
 public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> sequence)
 {
     using var writer = new Utf8JsonWriter(buffer);
     writer.WriteStartObject();
     writer.WriteNumber("network", sequence.Network);
     writer.WritePropertyName("messages");
     Write(writer, context, sequence);
     writer.WriteEndObject();
 }
Exemplo n.º 19
0
 IKRequest <TNodeId> Decode(IKMessageContext <TNodeId> context, Request message)
 {
     return(message.Body switch
     {
         PingRequest ping => new KRequest <TNodeId, KPingRequest <TNodeId> >(Decode(context, message.Header), Decode(context, ping)),
         StoreRequest store => new KRequest <TNodeId, KStoreRequest <TNodeId> >(Decode(context, message.Header), Decode(context, store)),
         FindNodeRequest findNode => new KRequest <TNodeId, KFindNodeRequest <TNodeId> >(Decode(context, message.Header), Decode(context, findNode)),
         FindValueRequest findValue => new KRequest <TNodeId, KFindValueRequest <TNodeId> >(Decode(context, message.Header), Decode(context, findValue)),
         _ => throw new InvalidOperationException(),
     });
Exemplo n.º 20
0
        void Write(Utf8JsonWriter writer, IKMessageContext <TNodeId> context, IKResponse <TNodeId> message)
        {
            writer.WriteStartObject();
            writer.WritePropertyName("header");
            Write(writer, context, message.Header);

            writer.WritePropertyName("status");
            Write(writer, context, message.Status);

            writer.WritePropertyName("type");

            switch (message)
            {
            case KResponse <TNodeId, KPingResponse <TNodeId> > ping:
                writer.WriteStringValue("PING_RESPONSE");
                if (ping.Body.HasValue)
                {
                    writer.WritePropertyName("body");
                    Write(writer, context, ping.Body.Value);
                }
                break;

            case KResponse <TNodeId, KStoreResponse <TNodeId> > store:
                writer.WriteStringValue("STORE_RESPONSE");
                if (store.Body.HasValue)
                {
                    writer.WritePropertyName("body");
                    Write(writer, context, store.Body.Value);
                }
                break;

            case KResponse <TNodeId, KFindNodeResponse <TNodeId> > findNode:
                writer.WriteStringValue("FIND_NODE_RESPONSE");
                if (findNode.Body.HasValue)
                {
                    writer.WritePropertyName("body");
                    Write(writer, context, findNode.Body.Value);
                }
                break;

            case KResponse <TNodeId, KFindValueResponse <TNodeId> > findValue:
                writer.WriteStringValue("FIND_VALUE_RESPONSE");
                if (findValue.Body.HasValue)
                {
                    writer.WritePropertyName("body");
                    Write(writer, context, findValue.Body.Value);
                }
                break;

            default:
                throw new InvalidOperationException();
            }

            writer.WriteEndObject();
        }
Exemplo n.º 21
0
        void Write(Utf8JsonWriter writer, IKMessageContext <TNodeId> context, IEnumerable <IKMessage <TNodeId> > messages)
        {
            writer.WriteStartArray();

            foreach (var message in messages)
            {
                Write(writer, context, message);
            }

            writer.WriteEndArray();
        }
Exemplo n.º 22
0
        IKMessage <TNodeId> Decode(IKMessageContext <TNodeId> context, Message message)
        {
            if (message is Request request)
            {
                return(Decode(context, request));
            }
            if (message is Response response)
            {
                return(Decode(context, response));
            }

            throw new InvalidOperationException();
        }
        Message Encode(IKMessageContext <TNodeId> context, IKMessage <TNodeId> message)
        {
            if (message is IKRequest <TNodeId> request)
            {
                return(Encode(context, request));
            }
            if (message is IKResponse <TNodeId> response)
            {
                return(Encode(context, response));
            }

            throw new InvalidOperationException();
        }
Exemplo n.º 24
0
 IKMessage <TNodeId> DecodeMessage(IKMessageContext <TNodeId> context, JsonElement message)
 {
     return((message.GetProperty("type").GetString()) switch
     {
         "PING" => CreateRequest(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodePingRequest(context, message.GetProperty("body"))),
         "PING_RESPONSE" => CreateResponse(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeResponseStatus(context, message.GetProperty("status")), DecodePingResponse(context, message.GetProperty("body"))),
         "STORE" => CreateRequest(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeStoreRequest(context, message.GetProperty("body"))),
         "STORE_RESPONSE" => CreateResponse(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeResponseStatus(context, message.GetProperty("status")), DecodeStoreResponse(context, message.GetProperty("body"))),
         "FIND_NODE" => CreateRequest(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeFindNodeRequest(context, message.GetProperty("body"))),
         "FIND_NODE_RESPONSE" => CreateResponse(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeResponseStatus(context, message.GetProperty("status")), DecodeFindNodeResponse(context, message.GetProperty("body"))),
         "FIND_VALUE" => CreateRequest(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeFindValueRequest(context, message.GetProperty("body"))),
         "FIND_VALUE_RESPONSE" => CreateResponse(context, DecodeMessageHeader(context, message.GetProperty("header")), DecodeResponseStatus(context, message.GetProperty("status")), DecodeFindValueResponse(context, message.GetProperty("body"))),
         _ => throw new InvalidOperationException(),
     });
Exemplo n.º 25
0
        void Write(Utf8JsonWriter writer, IKMessageContext <TNodeId> context, IKMessage <TNodeId> message)
        {
            switch (message)
            {
            case IKRequest <TNodeId> request:
                Write(writer, context, request);
                break;

            case IKResponse <TNodeId> response:
                Write(writer, context, response);
                break;

            default:
                throw new InvalidOperationException();
            }
        }
        StoreRequest Encode(IKMessageContext <TNodeId> context, KStoreRequest <TNodeId> request)
        {
            var r = new StoreRequest();

            r.Key  = Encode(context, request.Key);
            r.Mode = Encode(context, request.Mode);
            if (request.Value is KValueInfo value)
            {
                r.HasValue      = true;
                r.Value         = new ValueInfo();
                r.Value.Data    = ByteString.CopyFrom(value.Data);
                r.Value.Version = value.Version;
                r.Value.Ttl     = new Google.Protobuf.WellKnownTypes.Duration()
                {
                    Seconds = (long)(value.Expiration - DateTime.UtcNow).TotalSeconds
                };
            }
            return(r);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Attempts to read a message sequence from the input data.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public KUdpPacket <TNodeId> Read(ReadOnlyMemory <byte> buffer, IKMessageContext <TNodeId> context)
        {
            if (BinaryPrimitives.ReadUInt32LittleEndian(buffer.Span) != magic)
            {
                throw new KProtocolException(KProtocolError.Invalid, "Packet missing magic.");
            }

            // advance past magic number
            buffer = buffer.Slice(sizeof(uint));

            // format ends at first NUL
            var formatEnd = buffer.Span.IndexOf((byte)0x00);

            if (formatEnd < 0)
            {
                throw new KProtocolException(KProtocolError.Invalid, "Malformed packet.");
            }

            // extract encoded format type
#if NETSTANDARD2_0
            var contentType = Encoding.UTF8.GetString(buffer.Span.Slice(0, formatEnd).ToArray());
#else
            var contentType = Encoding.UTF8.GetString(buffer.Span.Slice(0, formatEnd));
#endif
            if (contentType == null)
            {
                throw new KProtocolException(KProtocolError.Invalid, "Packet missing content type.");
            }

            var format = formats.FirstOrDefault(i => i.ContentType == contentType);
            if (format == null)
            {
                throw new KProtocolException(KProtocolError.Invalid, $"Unknown format: '{contentType}'.");
            }

            // advance past format
            buffer = buffer.Slice(formatEnd + 1);

            // decode message sequence
            return(new KUdpPacket <TNodeId>(format.ContentType, format.Decode(context, new ReadOnlySequence <byte>(buffer))));
        }
Exemplo n.º 28
0
 /// <summary>
 /// Writes a message sequence to the writer.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="context"></param>
 /// <param name="format"></param>
 /// <param name="messages"></param>
 public void Write(IBufferWriter <byte> writer, IKMessageContext <TNodeId> context, in KMessageSequence <TNodeId> messages)
 public void Encode(IKMessageContext <TNodeId> context, IBufferWriter <byte> buffer, KMessageSequence <TNodeId> messages)
 {
     encoder.Encode(context, buffer, messages);
 }
 public KMessageSequence <TNodeId> Decode(IKMessageContext <TNodeId> context, ReadOnlySequence <byte> buffer)
 {
     return(decoder.Decode(context, buffer));
 }