List of usage examples for io.netty.channel.socket DatagramPacket content
ByteBuf content();
From source file:IncommingPacketHandler.java
License:Open Source License
@Override protected void messageReceived(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) throws Exception { final InetAddress srcAddr = packet.sender().getAddress(); final ByteBuf buf = packet.content(); try {//from w ww. j a v a 2s . c o m CapwapEvent event = new CapwapEvent(CapwapEventType.DECODE); event.setPacket(packet); buf.retain(); event.setIncomingBuf(buf); event.setCtx(channelHandlerContext); ; msgProcessor.tell(event, this.msgProcessor); } catch (Exception e) { e.printStackTrace(); } }
From source file:IncommingPacketFSMHandler.java
License:Open Source License
@Override protected void messageReceived(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) throws Exception { final InetAddress srcAddr = packet.sender().getAddress(); final ByteBuf buf = packet.content(); try {/*from w w w .ja v a 2 s . c o m*/ System.out.printf("Inside incomming packet handler %s wtpId %d \n", ODLCapwapConsts.msgTypetoString(buf.getInt(0)), buf.getInt(4)); CapwapEvent event = new CapwapEvent(CapwapEventType.DECODE); int wtpId = buf.getInt(4); Integer WTPID = new Integer(wtpId); event.setWtpID("wtpID" + WTPID.toString()); msgProcessor.tell(event, this.msgProcessor); } catch (Exception e) { e.printStackTrace(); } }
From source file:books.netty.protocol.udp.ChineseProverbClientHandler.java
License:Apache License
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) { String response = msg.content().toString(CharsetUtil.UTF_8); if (response.startsWith(": ")) { System.out.println(response); ctx.close();// w w w.j a va 2s .co m } }
From source file:books.netty.protocol.udp.ChineseProverbServerHandler.java
License:Apache License
public void messageReceived(ChannelHandlerContext ctx, DatagramPacket packet) { String req = packet.content().toString(CharsetUtil.UTF_8); System.out.println(req);/*from ww w . j a v a 2 s. com*/ if ("?".equals(req)) { ctx.writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer(": " + nextQuote(), CharsetUtil.UTF_8), packet.sender())); } }
From source file:c5db.codec.UdpProtostuffDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, DatagramPacket dgram, List<Object> out) throws Exception { ByteBuf msg = dgram.content(); ByteBufferInput input = new ByteBufferInput(msg.nioBuffer(), protostuffEncoded); T newMsg = schema.newMessage();//from ww w . j a v a 2 s . c o m schema.mergeFrom(input, newMsg); out.add(newMsg); }
From source file:ccostello.server.NetworkGameServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { //System.err.println(packet); GameMessage gm;/* w w w .j a va 2 s. co m*/ ByteBufInputStream is = new ByteBufInputStream(packet.content()); gm = GameMessage.parseFrom(is); System.out.println("Received message: " + gm.toString()); switch (gm.getMsgType()) { case REGISTER_CLIENT: IncomingMessagesManager.registerNewClient(gm, ctx, packet.sender()); break; case USER_COMMAND: IncomingMessagesManager.processUserCommand(gm, ctx, packet.sender()); break; case END_CLIENT: IncomingMessagesManager.unregisterClient(gm, ctx, packet.sender()); break; default: break; } }
From source file:cl.uandes.so.client.LoginClientHandler.java
@Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { if (msg.content().readableBytes() < 3) { return;/*from w w w.ja va 2s . co m*/ } byte[] address = null; String ip = ""; byte[] test = { msg.content().readByte() }; System.out.println(byteArray2Hex(test)); int readbytes = 1; if (msg.content().readByte() == (byte) 0x52) { System.out.println("Byte 0x52 found, readablebytes: " + msg.content().readableBytes()); ByteBuf data = msg.content().readBytes(msg.content().readableBytes()); ip += new String(data.array()); //System.out.println(byteArray2Hex(data.array())); this.address.append(ip); ctx.close(); } }
From source file:cl.uandes.so.client.MultiCastClientHandler.java
@Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { if (msg.content().readableBytes() < 6) { return;/* w w w . j a v a 2 s .c o m*/ } msg.content().readByte(); //0xFF msg.content().readByte(); //0xFF msg.content().readByte(); //0xFF msg.content().readByte(); //0xFF // Payload type char a = (char) msg.content().readByte(); byte[] ab = { (byte) a }; short length; byte[] payload; byte[] shortlength; switch (a) { // File Announcement case 'A': if (appStatus.hasFileAnnouncement) { // We already know about the file we are going to receive break; } byte[] a_length = { msg.content().readByte(), msg.content().readByte() }; System.out.println(); System.out.println("[A] packet received - Length: " + Utils.getShortFromLittleEndianRange(a_length)); payload = msg.content().readBytes(msg.content().readableBytes()).array(); // TODO: Deserializar payload con protobuf FileAnnouncementProtos.FileAnnouncement fa = FileAnnouncementProtos.FileAnnouncement.parseFrom(payload); System.out.println("==============================================="); System.out.println("Announcement Packet Received"); System.out.println(" >cheksum: " + fa.getChecksum()); System.out.println(" >fileName: " + fa.getFileName()); System.out.println(" >fileSize: " + fa.getFileSize()); System.out.println(" >chunksTotal: " + fa.getChunksTotal()); System.out.println("==============================================="); appStatus.fa = fa; appStatus.filename = fa.getFileName(); appStatus.filesize = fa.getFileSize(); // Prepare byte array to store chunks appStatus.filecontent = new byte[fa.getFileSize()]; // Bool array for received chunks appStatus.receivedChunks = new boolean[fa.getChunksTotal()]; for (int i = 0; i < fa.getChunksTotal(); i++) { appStatus.receivedChunks[i] = false; } appStatus.hasFileAnnouncement = true; break; case 'B': //System.err.println("Ignoring B Packet..."); break; case 'C': if (!appStatus.hasFileAnnouncement) { // We don't know what we are receiving return; } // Reset Timer appStatus.timeSinceLastResponse = 0; byte[] c_length = { msg.content().readByte(), msg.content().readByte() }; //System.out.println("[C] packet received - Length: " + Utils.getShortFromLittleEndianRange(c_length)); payload = msg.content().readBytes(msg.content().readableBytes()).array(); FileFragment f = FileFragment.parseFrom(payload); if (appStatus.receivedChunks[f.getId()] == true) { // We already received this chunk return; } // Verify Chunk integrity if (SHAsum(f.getData().toByteArray()).equals(f.getChecksum())) { //System.out.printf("Got fragment %d, checksum OK", f.getId()); appStatus.receivedChunks[f.getId()] = true; for (long i = f.getStart(); i < f.getEnd() + 1; i++) { int chunkindex = (int) (i - f.getStart()); appStatus.filecontent[(int) i] = f.getData().toByteArray()[chunkindex]; } appStatus.incrementReceivedChunks(); } break; default: System.out.println(String.format("Unknown packet type received, got %c", a)); System.out.println(byteArray2Hex(ab)); } //ByteBuf data = msg.content().readBytes(msg.content().readableBytes()); //System.out.println(new String(data.array())); }
From source file:cl.uandes.so.server.LoginServerHandler.java
@Override public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception { //if(packet.content().readableBytes() < 24) { // return; //}/* w w w .java2 s.c o m*/ // TEST Cliente: echo -e "\xff\xff\xff\xff\xff\xff\xff\xff\x41\x67\x65\x74\x41\x64\x64\x72\x65\x73\x73\x26\x50\x6f\x72\x74" | nc -4 -w 1 -u 127.0.0.1 9990 String expected_payload = "ffffffffffffffff416765744164647265737326506f7274"; //System.err.println(packet); //System.out.println(packet.toString()); byte[] payload = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x41, (byte) 0x67, (byte) 0x65, (byte) 0x74, (byte) 0x41, (byte) 0x64, (byte) 0x64, (byte) 0x72, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x26, (byte) 0x50, (byte) 0x6f, (byte) 0x72, (byte) 0x74 }; ByteBuf data = packet.content().readBytes(packet.content().readableBytes()); System.out.println(data.array().length); byte[] header = { (byte) 0xff, (byte) 0x52 }; System.out.println(Utils.byteArray2Hex(data.array())); if (Utils.byteArray2Hex(data.array()).equals(Utils.byteArray2Hex(payload))) { System.out.println("Got login of client, sending multicast group address (header+multicastgroup)"); ctx.write(new DatagramPacket( Unpooled.copiedBuffer(ArrayUtils.addAll(header, multicastgroup.getBytes())), packet.sender())); } }
From source file:co.rsk.net.discovery.PacketDecoder.java
License:Open Source License
@Override public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception { ByteBuf buf = packet.content(); byte[] encoded = new byte[buf.readableBytes()]; buf.readBytes(encoded);//from w w w . j a va2 s . c o m out.add(this.decodeMessage(ctx, encoded, packet.sender())); }