use of java.net.SocketAddress in project netty by netty.
the class OioDatagramChannel method doWrite.
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
for (; ; ) {
final Object o = in.current();
if (o == null) {
break;
}
final ByteBuf data;
final SocketAddress remoteAddress;
if (o instanceof AddressedEnvelope) {
@SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) o;
remoteAddress = envelope.recipient();
data = envelope.content();
} else {
data = (ByteBuf) o;
remoteAddress = null;
}
final int length = data.readableBytes();
try {
if (remoteAddress != null) {
tmpPacket.setSocketAddress(remoteAddress);
} else {
if (!isConnected()) {
// NioDatagramChannel
throw new NotYetConnectedException();
}
}
if (data.hasArray()) {
tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
} else {
byte[] tmp = new byte[length];
data.getBytes(data.readerIndex(), tmp);
tmpPacket.setData(tmp);
}
socket.send(tmpPacket);
in.remove();
} catch (IOException e) {
// Continue on write error as a DatagramChannel can write to multiple remote peers
//
// See https://github.com/netty/netty/issues/2665
in.remove(e);
}
}
}
use of java.net.SocketAddress in project netty by netty.
the class BootstrapTest method testLateRegisterSuccessBindFailed.
@Test
public void testLateRegisterSuccessBindFailed() throws Exception {
TestEventLoopGroup group = new TestEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group);
bootstrap.channelFactory(new ChannelFactory<ServerChannel>() {
@Override
public ServerChannel newChannel() {
return new LocalServerChannel() {
@Override
public ChannelFuture bind(SocketAddress localAddress) {
// Close the Channel to emulate what NIO and others impl do on bind failure
// See https://github.com/netty/netty/issues/2586
close();
return newFailedFuture(new SocketException());
}
@Override
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
// Close the Channel to emulate what NIO and others impl do on bind failure
// See https://github.com/netty/netty/issues/2586
close();
return promise.setFailure(new SocketException());
}
};
}
});
bootstrap.childHandler(new DummyHandler());
bootstrap.localAddress(new LocalAddress("1"));
ChannelFuture future = bootstrap.bind();
assertFalse(future.isDone());
group.promise.setSuccess();
final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
queue.add(future.isSuccess());
}
});
assertTrue(queue.take());
assertFalse(queue.take());
} finally {
group.shutdownGracefully();
group.terminationFuture().sync();
}
}
use of java.net.SocketAddress in project netty by netty.
the class BootstrapTest method testAsyncResolutionFailure.
@Test
public void testAsyncResolutionFailure() throws Exception {
final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA);
bootstrapA.channel(LocalChannel.class);
bootstrapA.resolver(new TestAddressResolverGroup(false));
bootstrapA.handler(dummyHandler);
final ServerBootstrap bootstrapB = new ServerBootstrap();
bootstrapB.group(groupB);
bootstrapB.channel(LocalServerChannel.class);
bootstrapB.childHandler(dummyHandler);
SocketAddress localAddress = bootstrapB.bind(LocalAddress.ANY).sync().channel().localAddress();
// Connect to the server using the asynchronous resolver.
ChannelFuture connectFuture = bootstrapA.connect(localAddress);
// Should fail with the UnknownHostException.
assertThat(connectFuture.await(10000), is(true));
assertThat(connectFuture.cause(), is(instanceOf(UnknownHostException.class)));
assertThat(connectFuture.channel().isOpen(), is(false));
}
use of java.net.SocketAddress in project pinpoint by naver.
the class FutureGetInterceptor method getEndPoint.
private String getEndPoint(MemcachedNode handlingNode) {
// TODO duplicated code : ApiInterceptor, FutureGetInterceptor
final SocketAddress socketAddress = handlingNode.getSocketAddress();
if (socketAddress instanceof InetSocketAddress) {
final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
final String hostAddress = getHostAddress(inetSocketAddress);
if (hostAddress == null) {
// TODO return "Unknown Host";
logger.debug("hostAddress is null");
return null;
}
return hostAddress + ":" + inetSocketAddress.getPort();
} else {
if (logger.isDebugEnabled()) {
logger.debug("invalid socketAddress:{}", socketAddress);
}
return null;
}
}
use of java.net.SocketAddress in project netty by netty.
the class NioSocketChannelTest method testFlushAfterGatheredFlush.
/**
* Reproduces the issue #1679
*/
@Test
public void testFlushAfterGatheredFlush() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
try {
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(NioServerSocketChannel.class);
sb.childHandler(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
// Trigger a gathering write by writing two buffers.
ctx.write(Unpooled.wrappedBuffer(new byte[] { 'a' }));
ChannelFuture f = ctx.write(Unpooled.wrappedBuffer(new byte[] { 'b' }));
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
// This message must be flushed
ctx.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'c' }));
}
});
ctx.flush();
}
});
SocketAddress address = sb.bind(0).sync().channel().localAddress();
Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) address).getPort());
DataInput in = new DataInputStream(s.getInputStream());
byte[] buf = new byte[3];
in.readFully(buf);
assertThat(new String(buf, CharsetUtil.US_ASCII), is("abc"));
s.close();
} finally {
group.shutdownGracefully().sync();
}
}
Aggregations