use of io.netty.channel.socket.DatagramPacket in project vert.x by eclipse.
the class DatagramServerHandler method safeObject.
@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
if (msg instanceof DatagramPacket) {
DatagramPacket packet = (DatagramPacket) msg;
ByteBuf content = packet.content();
if (content.isDirect()) {
content = safeBuffer(content, allocator);
}
return new DatagramPacketImpl(packet.sender(), Buffer.buffer(content));
}
return msg;
}
use of io.netty.channel.socket.DatagramPacket in project vert.x by eclipse.
the class DatagramSocketImpl method doSend.
private void doSend(Buffer packet, InetSocketAddress addr, Handler<AsyncResult<DatagramSocket>> handler) {
ChannelFuture future = channel().writeAndFlush(new DatagramPacket(packet.getByteBuf(), addr));
addListener(future, handler);
}
use of io.netty.channel.socket.DatagramPacket in project netty by netty.
the class DatagramUnicastTest method testSimpleSend0.
@SuppressWarnings("deprecation")
private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient, final byte[] bytes, int count) throws Throwable {
final CountDownLatch latch = new CountDownLatch(count);
sb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
ByteBuf buf = msg.content();
assertEquals(bytes.length, buf.readableBytes());
for (byte b : bytes) {
assertEquals(b, buf.readByte());
}
latch.countDown();
}
});
}
});
cb.handler(new SimpleChannelInboundHandler<Object>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msgs) throws Exception {
// Nothing will be sent.
}
});
Channel sc = null;
BindException bindFailureCause = null;
for (int i = 0; i < 3; i++) {
try {
sc = sb.bind().sync().channel();
break;
} catch (Exception e) {
if (e instanceof BindException) {
logger.warn("Failed to bind to a free port; trying again", e);
bindFailureCause = (BindException) e;
refreshLocalAddress(sb);
} else {
throw e;
}
}
}
if (sc == null) {
throw bindFailureCause;
}
Channel cc;
if (bindClient) {
cc = cb.bind().sync().channel();
} else {
cb.option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION, true);
cc = cb.register().sync().channel();
}
for (int i = 0; i < count; i++) {
cc.write(new DatagramPacket(buf.retain().duplicate(), addr));
}
// release as we used buf.retain() before
buf.release();
cc.flush();
assertTrue(latch.await(10, TimeUnit.SECONDS));
sc.close().sync();
cc.close().sync();
}
use of io.netty.channel.socket.DatagramPacket in project java-in-action by xinghalo.
the class LogEventEncoder method encode.
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, LogEvent logEvent, List<Object> list) throws Exception {
byte[] file = logEvent.getLogfile().getBytes(CharsetUtil.UTF_8);
byte[] msg = logEvent.getMsg().getBytes(CharsetUtil.UTF_8);
ByteBuf buf = channelHandlerContext.alloc().buffer(file.length + msg.length + 1);
buf.writeBytes(file);
buf.writeByte(LogEvent.SEPARATOR);
buf.writeBytes(msg);
list.add(new DatagramPacket(buf, address));
}
use of io.netty.channel.socket.DatagramPacket in project java-tron by tronprotocol.
the class MessageHandler method sendPacket.
void sendPacket(byte[] wire, InetSocketAddress address) {
DatagramPacket packet = new DatagramPacket(Unpooled.copiedBuffer(wire), address);
channel.write(packet);
channel.flush();
}
Aggregations