下面列出了io.netty.util.internal.SocketUtils#socketAddress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected boolean handleProxyProtocol(ChannelHandlerContext ctx, Object msg) throws Exception {
if (!authenticated) {
authenticated = authenticate(ctx, msg);
return false;
}
Socks5CommandRequest req = (Socks5CommandRequest) msg;
assertThat(req.type(), is(Socks5CommandType.CONNECT));
Socks5CommandResponse res =
new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4);
intermediaryDestination = SocketUtils.socketAddress(req.dstAddr(), req.dstPort());
ctx.write(res);
ctx.pipeline().remove(ENCODER);
ctx.pipeline().remove(DECODER);
return true;
}
@Test
public void writeQueryTest() throws Exception {
InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53);
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder());
List<DnsQuery> queries = new ArrayList<DnsQuery>(5);
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
DnsSection.QUESTION,
new DefaultDnsQuestion("1.0.0.127.in-addr.arpa", DnsRecordType.PTR)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
DnsSection.QUESTION,
new DefaultDnsQuestion("www.example.com", DnsRecordType.A)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
DnsSection.QUESTION,
new DefaultDnsQuestion("example.com", DnsRecordType.AAAA)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
DnsSection.QUESTION,
new DefaultDnsQuestion("example.com", DnsRecordType.MX)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(
DnsSection.QUESTION,
new DefaultDnsQuestion("example.com", DnsRecordType.CNAME)));
for (DnsQuery query: queries) {
assertThat(query.count(DnsSection.QUESTION), is(1));
assertThat(query.count(DnsSection.ANSWER), is(0));
assertThat(query.count(DnsSection.AUTHORITY), is(0));
assertThat(query.count(DnsSection.ADDITIONAL), is(0));
embedder.writeOutbound(query);
DatagramPacket packet = embedder.readOutbound();
Assert.assertTrue(packet.content().isReadable());
packet.release();
Assert.assertNull(embedder.readOutbound());
}
}
private static EmbeddedChannel newEmbeddedInetChannel(final String ipAddress, ChannelHandler... handlers) {
return new EmbeddedChannel(handlers) {
@Override
protected SocketAddress remoteAddress0() {
return isActive()? SocketUtils.socketAddress(ipAddress, 5421) : null;
}
};
}
@Test
public void testEncode() {
InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
assertTrue(channel.writeOutbound(
new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender)));
DatagramPacket packet = channel.readOutbound();
try {
assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8));
assertEquals(recipient, packet.recipient());
assertEquals(sender, packet.sender());
} finally {
packet.release();
}
}
@Test
public void testUnmatchedMessageType() {
InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
DefaultAddressedEnvelope<Long, InetSocketAddress> envelope =
new DefaultAddressedEnvelope<Long, InetSocketAddress>(1L, recipient, sender);
assertTrue(channel.writeOutbound(envelope));
DefaultAddressedEnvelope<Long, InetSocketAddress> output = channel.readOutbound();
try {
assertSame(envelope, output);
} finally {
output.release();
}
}
@Test
public void testDecode() {
InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
ByteBuf content = Unpooled.wrappedBuffer("netty".getBytes(CharsetUtil.UTF_8));
assertTrue(channel.writeInbound(new DatagramPacket(content, recipient, sender)));
assertEquals("netty", channel.readInbound());
}
public static void main(final String[] args) throws Exception {
final int messageSize = 64 * 1024;
final InetSocketAddress self = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
new MsgEchoPeerOne(self, peer, messageSize).run();
}
public static void main(final String[] args) throws Exception {
final int messageSize = 64 * 1024;
final InetSocketAddress self = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
final InetSocketAddress peer = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
new MsgEchoPeerTwo(self, peer, messageSize).run();
}
public static void main(String[] args) throws Exception {
final int messageSize = 64 * 1024;
final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
new ByteEchoPeerOne(messageSize, myAddress, peerAddress).run();
}
public static void main(String[] args) throws Exception {
final int messageSize = 64 * 1024;
final InetSocketAddress myAddress = SocketUtils.socketAddress(Config.hostTwo, Config.portTwo);
final InetSocketAddress peerAddress = SocketUtils.socketAddress(Config.hostOne, Config.portOne);
new ByteEchoPeerTwo(messageSize, myAddress, peerAddress).run();
}
public void testMulticast(Bootstrap sb, Bootstrap cb) throws Throwable {
MulticastTestHandler mhandler = new MulticastTestHandler();
sb.handler(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// Nothing will be sent.
}
});
cb.handler(mhandler);
sb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
sb.option(ChannelOption.SO_REUSEADDR, true);
cb.option(ChannelOption.IP_MULTICAST_IF, NetUtil.LOOPBACK_IF);
cb.option(ChannelOption.SO_REUSEADDR, true);
Channel sc = sb.bind(newSocketAddress()).sync().channel();
InetSocketAddress addr = (InetSocketAddress) sc.localAddress();
cb.localAddress(addr.getPort());
if (sc instanceof OioDatagramChannel) {
// skip the test for OIO, as it fails because of
// No route to host which makes no sense.
// Maybe a JDK bug ?
sc.close().awaitUninterruptibly();
return;
}
DatagramChannel cc = (DatagramChannel) cb.bind().sync().channel();
String group = "230.0.0.1";
InetSocketAddress groupAddress = SocketUtils.socketAddress(group, addr.getPort());
cc.joinGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();
sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
assertTrue(mhandler.await());
// leave the group
cc.leaveGroup(groupAddress, NetUtil.LOOPBACK_IF).sync();
// sleep a second to make sure we left the group
Thread.sleep(1000);
// we should not receive a message anymore as we left the group before
sc.writeAndFlush(new DatagramPacket(Unpooled.copyInt(1), groupAddress)).sync();
mhandler.await();
sc.close().awaitUninterruptibly();
cc.close().awaitUninterruptibly();
}
private static InetSocketAddress newSockAddress(String ipAddress) {
return SocketUtils.socketAddress(ipAddress, 1234);
}