Java Code Examples for io.netty.channel.socket.DatagramPacket#retain()

The following examples show how to use io.netty.channel.socket.DatagramPacket#retain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KcpTestServer.java    From dfactor with MIT License 6 votes vote down vote up
@Override
		protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket pack) throws Exception {
			final ByteBuf buf = pack.content();
			final int size = buf.readableBytes();
			if(size > 0){
				int connId = 0;
				if(buf.readByte()==Kcp.FLAG && size > 1 + 4){ //valid kcp head
					connId = buf.getInt(buf.readerIndex());
				}
				if(connId > 0){ //valid kcp pack
					pack.retain();
					kcpSys.onReceive(pack, connId);
//					log.I("Recv kcp pack, sender="+pack.sender().toString());
				}else{  //normal udp pack
					log.I("Recv udp pack, sender="+pack.sender().toString());
				}
			}else{
				log.E("Invalid pack, len=0, sender="+pack.sender().toString());
			}
		}
 
Example 2
Source File: KcpTestClient.java    From dfactor with MIT License 6 votes vote down vote up
@Override
		protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket pack) throws Exception {
			final ByteBuf buf = pack.content();
			final int size = buf.readableBytes();
			if(size > 0){
				int connId = 0;
				if(buf.readByte()==Kcp.FLAG && size > 1 + 4){ //valid kcp head
					connId = buf.getInt(buf.readerIndex());
				}
				if(connId > 0){ //valid kcp pack
					pack.retain();
					queueRecv.offer(pack);
//					log.I("Recv kcp pack, sender="+pack.sender().toString());
				}else{  //normal udp pack
					log.I("Recv udp pack, sender="+pack.sender().toString());
				}
			}else{
				log.E("Invalid pack, len=0, sender="+pack.sender().toString());
			}
		}
 
Example 3
Source File: UdpTestClient.java    From dfactor with MIT License 6 votes vote down vote up
@Override
		protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket pack) throws Exception {
			final ByteBuf buf = pack.content();
			final int size = buf.readableBytes();
			if(size > 0){
				int connId = 0;
				if(buf.readByte()==Kcp.FLAG && size > 1 + 4){ //valid kcp head
					connId = buf.getInt(buf.readerIndex());
				}
				if(connId > 0){ //valid kcp pack
					pack.retain();
					queueRecv.offer(pack);
//					log.I("Recv kcp pack, sender="+pack.sender().toString());
				}else{  //normal udp pack
					log.I("Recv udp pack, sender="+pack.sender().toString());
				}
			}else{
				log.E("Invalid pack, len=0, sender="+pack.sender().toString());
			}
		}
 
Example 4
Source File: DFSocketManager.java    From dfactor with MIT License 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket pack) throws Exception {
	try{
		Object msg = null;
		if(decoder != null){
			msg = decoder.onDecode(pack);
		}
		boolean isPack = false;
		if(msg == null){
			msg = pack;
			isPack = true;
		}
		int actorId = 0;
		if(dispatcher != null){
			actorId = dispatcher.onQueryMsgActorId(msg);
		}
		if(actorId == 0){
			actorId = actorIdDef;
		}
		if(actorId > 0){
			if(actorMgr.send(0, actorId, requestId, DFActorDefine.SUBJECT_NET, 
					DFActorDefine.NET_UDP_MESSAGE, msg, true, channel, null, false) == 0){ //send to queue succ
				if(isPack){
					pack.retain();
				}
			}
		}
	}catch(Throwable e){
		e.printStackTrace();
	}
}
 
Example 5
Source File: PacketQueueUDPHandler.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
  packet.retain();
  final boolean succeeded = queue.offer(packet);
  if (succeeded) {
    gaugeMap.put(GAUGE_NUM_QUEUED_PACKETS, queuedPacketCount.incrementAndGet());
    gaugeMap.put(GAUGE_PACKET_QUEUE_SIZE, queue.size());
  } else {
    gaugeMap.put(GAUGE_NUM_DROPPED_PACKETS, droppedPacketCount.incrementAndGet());
    // allow Netty to collect the buffer
    packet.release();
  }
}
 
Example 6
Source File: KafkaUDPConsumer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public Dispatcher(DatagramPacket packet) {
  received = System.currentTimeMillis();
  this.packet = packet;
  packet.retain();
}
 
Example 7
Source File: ProtocolDecoder.java    From plog with Apache License 2.0 4 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out)
        throws Exception {
    final ByteBuf content = msg.content();
    final byte versionIdentifier = content.getByte(0);
    // versions are non-printable characters, push down the pipeline send as-is.
    if (versionIdentifier < 0 || versionIdentifier > 31) {
        log.debug("Unboxed UDP message");
        stats.receivedUdpSimpleMessage();
        msg.retain();
        out.add(new MessageImpl(content, null));
    } else if (versionIdentifier == 0) {
        final byte typeIdentifier = content.getByte(1);
        switch (typeIdentifier) {
            case 0:
                final FourLetterCommand cmd = readCommand(msg);
                if (cmd != null) {
                    log.debug("v0 command");
                    out.add(cmd);
                } else {
                    stats.receivedUnknownCommand();
                }
                break;
            case 1:
                log.debug("v0 multipart message: {}", msg);
                try {
                    final Fragment fragment = Fragment.fromDatagram(msg);
                    stats.receivedV0MultipartFragment(fragment.getFragmentIndex());
                    msg.retain();
                    out.add(fragment);
                } catch (IllegalArgumentException e) {
                    log.error("Invalid header", e);
                    stats.receivedV0InvalidMultipartHeader();
                }
                break;
            default:
                stats.receivedV0InvalidType();
        }
    } else {
        stats.receivedUdpInvalidVersion();
    }
}