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)); }
public void TestJsonResolveResponseWriteRead() { // Arrange ProxySocketAddress address = new ProxySocketAddress(); address.Flow = 0xff; address.Port = 65500; address.Host = "rawrawrawraw"; ResolveResponse args = new ResolveResponse(); for (UInt32 i = 0; i < 10; i++) { var ai = new AddressInfo(); ai.CanonicalName = String.Format("name{0}", i); var sa = new Inet4SocketAddress(i | 0xf0000, (UInt16)i, i + 10); ai.SocketAddress = sa; args.Results.Add(ai); } 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)); }
public void TestMpackDataWriteRead1() { // Arrange var source = new Inet4SocketAddress(0xffaabbcc, 9643); var args = DataMessage.Create(new byte[145], source, 12); new Random().NextBytes(args.Payload); var datagramIn = Message.Create(null, null, null, args); var stream = new MemoryStream(); // Act datagramIn.Encode(stream, CodecId.Mpack); byte[] buf = Interop.MessageDecodeEncode(CodecId.Mpack, 1, stream.GetBuffer(), stream.Length, buffer, buffer.Length); var returned = Serializable.Decode <Message>(new MemoryStream(buf), CodecId.Mpack); // Assert Assert.IsTrue(datagramIn.Equals(returned)); }
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)); }
public void TestJsonDataWriteRead1() { // Arrange Inet4SocketAddress source = new Inet4SocketAddress(0xffaabbcc, 9643, 1); DataMessage args = new DataMessage(); args.Source = source; args.Payload = new byte[145]; new Random().NextBytes(args.Payload); Message datagramIn = new Message(null, null, null, args); MemoryStream stream = new MemoryStream(); // Act datagramIn.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(datagramIn.Equals(returned)); }
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); }