Esempio n. 1
0
        public void TestJsonLinkResponseWriteRead()
        {
            // Arrange
            Inet4SocketAddress sal = new Inet4SocketAddress(0xffaabbcc, 9643);
            var ab = new byte[16];

            new Random().NextBytes(ab);
            Inet6SocketAddress sap = new Inet6SocketAddress(ab, 443, 1, 0);

            LinkResponse args = new LinkResponse();

            args.LocalAddress = sal;
            args.PeerAddress  = sap;
            args.LinkId       = new Reference();

            Message      response = new Message(null, null, null, args);
            MemoryStream stream   = new MemoryStream();

            // Act
            response.Encode(stream, CodecId.Json);
            byte[]  buf      = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length);
            Message returned = Message.Decode(new MemoryStream(buf), CodecId.Json);

            // Assert
            Assert.IsTrue(response.Equals(returned));
        }
Esempio n. 2
0
        public void TestJsonLinkResponseWriteRead()
        {
            // Arrange
            var sal = new Inet4SocketAddress(0xffaabbcc, 9643);
            var ab  = new byte[16];

            new Random().NextBytes(ab);
            var sap = new Inet6SocketAddress(ab, 443, 1, 0);

            var args     = LinkResponse.Create(new Reference(), sal, sap, 0x18);
            var response = Message.Create(null, null, null, args);
            var stream   = new MemoryStream();

            // Act
            response.Encode(stream, CodecId.Json);
            byte[] buf      = Interop.MessageDecodeEncode(CodecId.Json, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length);
            var    returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Json);

            // Assert
            Assert.IsTrue(response.Equals(returned));
        }
 /// <summary>
 /// Convert to system enum
 /// </summary>
 /// <param name="address"></param>
 /// <returns></returns>
 public static Reference AsReference(this Inet6SocketAddress address) =>
 new Reference(address.Address);
Esempio n. 4
0
 /// <summary>
 /// Converts this reference to a socket address
 /// </summary>
 /// <returns></returns>
 public static Reference FromSocketAddress(Inet6SocketAddress address)
 {
     return(new Reference(address.Address));
 }
Esempio n. 5
0
        public async override Task <SocketAddress> ReadAsync(Reader reader,
                                                             SerializerContext context, CancellationToken ct)
        {
            SocketAddress result  = null;
            int           members = await reader.ReadObjectHeaderAsync(ct).ConfigureAwait(false);

            if (members < 1)
            {
                throw new FormatException(
                          $"Unexpected number of properties {members}");
            }
            AddressFamily family = (AddressFamily)
                                   await reader.ReadInt32Async(ct).ConfigureAwait(false);

            if (family == AddressFamily.Unspecified)
            {
                result = new NullSocketAddress();
            }
            else if (family == AddressFamily.Unix)
            {
                if (members < 2)
                {
                    throw new FormatException(
                              $"Unexpected number of properties {members}");
                }
                string path = await reader.ReadStringAsync(ct).ConfigureAwait(false);

                result = new UnixSocketAddress(path);
            }
            else if (
                family != AddressFamily.Proxy &&
                family != AddressFamily.InterNetwork &&
                family != AddressFamily.InterNetworkV6)
            {
                throw new FormatException($"Bad address family {family}");
            }
            else
            {
                if (members < 4)
                {
                    throw new FormatException(
                              $"Unexpected number of properties {members}");
                }
                uint flow = await reader.ReadUInt32Async(ct).ConfigureAwait(false);

                ushort port = await reader.ReadUInt16Async(ct).ConfigureAwait(false);

                byte[] address;
                switch (family)
                {
                case AddressFamily.InterNetwork:
                    address = await reader.ReadBinAsync(ct).ConfigureAwait(false);

                    if (address.Length != 4)
                    {
                        throw new FormatException(
                                  $"Bad v4 address size {address.Length}");
                    }
                    result = new Inet4SocketAddress(address, port, flow);
                    break;

                case AddressFamily.InterNetworkV6:
                    address = await reader.ReadBinAsync(ct).ConfigureAwait(false);

                    if (address.Length != 16)
                    {
                        throw new FormatException(
                                  $"Bad v6 address size {address.Length}");
                    }
                    if (members < 5)
                    {
                        throw new FormatException(
                                  $"Unexpected number of properties {members}");
                    }
                    uint scopeId = await reader.ReadUInt32Async(ct).ConfigureAwait(false);

                    result = new Inet6SocketAddress(address, port, flow, scopeId);
                    break;

                case AddressFamily.Proxy:
                default:
                    string host = await reader.ReadStringAsync(ct).ConfigureAwait(false);

                    result = new ProxySocketAddress(host, port, flow);
                    break;
                }
            }
            return(result);
        }