List of usage examples for io.netty.channel.socket DatagramPacket DatagramPacket
public DatagramPacket(ByteBuf data, InetSocketAddress recipient)
From source file:com.github.mrstampy.kitchensync.netty.channel.AbstractKiSyChannel.java
License:Open Source License
/** * Creates a DatagramPacket for sending down the socket from the specified * message.//from w w w .j a va 2 s. com * * @param <MSG> * the generic type * @param message * the message * @param address * the address * @return the object * @see ByteBufCreator */ protected <MSG extends Object> DatagramPacket createMessage(MSG message, InetSocketAddress address) { ByteBufCreator creator = getByteBufCreator(); if (creator == null) { log.error("Cannot determine ByteBufCreator for message {}, recipient {}", message, address); return null; } return new DatagramPacket(creator.createByteBuf(message, address), address); }
From source file:com.goodgamenow.source.serverquery.MasterQueryHandler.java
License:Open Source License
/** * Fires a Datagram packet with its associated query to the master server. * * @param ctx channel handler context/*from ww w.j a va2 s .c om*/ * @exception UnsupportedEncodingException */ @Override public void channelActive(ChannelHandlerContext ctx) throws UnsupportedEncodingException { if (0L == startTime) { startTime = System.currentTimeMillis(); } // create the query buffer ByteBuf buf = ctx.alloc().buffer().writeByte(MSG_TYPE).writeByte(query.region.code) .writeBytes(lastAddress.getBytes("US-ASCII")).writeByte(NULL_TERMINATOR) .writeBytes(query.filter.getBytes("UTF-8")).writeByte(NULL_TERMINATOR); // Master server results are paged, sending last address received // back to master will give us another page. ctx.writeAndFlush(new DatagramPacket(buf, masterAddress)); }
From source file:com.hop.hhxx.example.qotm.QuoteOfTheMomentClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w .ja v a 2 s.c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new QuoteOfTheMomentClientHandler()); Channel ch = b.bind(0).sync().channel(); // Broadcast the QOTM request to port 8080. ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", PORT))).sync(); // QuoteOfTheMomentClientHandler will close the DatagramChannel when a // response is received. If the channel is not closed within 5 seconds, // print an error message and quit. if (!ch.closeFuture().await(5000)) { System.err.println("QOTM request timed out."); } } finally { group.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.qotm.QuoteOfTheMomentClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*from ww w .ja v a 2 s . c om*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new QuoteOfTheMomentClientHandler()); Channel ch = b.bind(0).sync().channel(); // Broadcast the QOTM request to port 8080. ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", port))).sync(); // QuoteOfTheMomentClientHandler will close the DatagramChannel when a // response is received. If the channel is not closed within 5 seconds, // print an error message and quit. if (!ch.closeFuture().await(5000)) { System.err.println("QOTM request timed out."); } } finally { group.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.qotm.QuoteOfTheMomentServerHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { System.err.println(packet);/*ww w. j a v a 2 s. c o m*/ if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) { ctx.write(new DatagramPacket(Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8), packet.sender())); } }
From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceQueryRequestEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, SourceServerRequest request, List<Object> out) throws Exception { SourceRequestPacket packet = request.getMessage(); byte[] deconstructedPacket = builder.deconstruct(packet); if (deconstructedPacket != null && deconstructedPacket.length > 0) { ByteBuf buffer = ctx.alloc().buffer(deconstructedPacket.length).writeBytes(deconstructedPacket); out.add(new DatagramPacket(buffer, request.recipient())); }//from w ww .j a va 2s .c o m }
From source file:com.ibasco.agql.protocols.valve.steam.master.handlers.MasterServerRequestEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, MasterServerRequest request, List<Object> out) throws Exception { MasterServerRequestPacket packet = request.getMessage(); byte[] deconstructedPacket = builder.deconstruct(packet); if (deconstructedPacket != null && deconstructedPacket.length > 0) { ByteBuf buffer = ctx.alloc().buffer(deconstructedPacket.length).writeBytes(deconstructedPacket); out.add(new DatagramPacket(buffer, request.recipient())); }/*from w w w . java 2 s. com*/ }
From source file:com.jfastnet.peers.netty.KryoNettyPeer.java
License:Apache License
@Override public boolean send(Message message) { if (message.payload == null) { log.error("Payload of message {} may not be null.", message.toString()); return false; }/*from ww w .ja va2 s.c o m*/ if (message.socketAddressRecipient == null) { log.error("Recipient of message {} may not be null.", message.toString()); return false; } if (config.trackData) { int frame = 0; config.netStats.getData().add(new NetStats.Line(true, message.getSenderId(), frame, message.getTimestamp(), message.getClass(), ((ByteBuf) message.payload).writerIndex())); } channel.writeAndFlush(new DatagramPacket((ByteBuf) message.payload, message.socketAddressRecipient)) .addListener(new FutureGenericFutureListener("writeAndFlush", KryoNettyPeer.this, message)); return true; }
From source file:com.jjzhk.Chapter12.udp.ChineseProverbClient.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from w ww .j a v a 2 s .c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new ChineseProverbClientHandler()); Channel ch = b.bind(0).sync().channel(); // ???????DP ch.writeAndFlush( new DatagramPacket(Unpooled.copiedBuffer("???", CharsetUtil.UTF_8), new InetSocketAddress("255.255.255.255", port))) .sync(); if (!ch.closeFuture().await(15000)) { System.out.println("?!"); } } finally { group.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter12.udp.ChineseProverbServerHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { String req = packet.content().toString(CharsetUtil.UTF_8); System.out.println(req);/* w w w . j a va2 s . c om*/ if ("???".equals(req)) { ctx.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer("???: " + nextQuote(), CharsetUtil.UTF_8), packet.sender())); } }