當前位置: 首頁>>代碼示例>>Java>>正文


Java DatagramPacket.content方法代碼示例

本文整理匯總了Java中io.netty.channel.socket.DatagramPacket.content方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramPacket.content方法的具體用法?Java DatagramPacket.content怎麽用?Java DatagramPacket.content使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.netty.channel.socket.DatagramPacket的用法示例。


在下文中一共展示了DatagramPacket.content方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
	ByteBuf buffer = msg.content();
	
	int seq = buffer.readUnsignedShort();
	int msgid = buffer.readByte();
	
	IMessage message = TF2weapons.network.messages[msgid].newInstance();
	//buffer.discardReadBytes();
	message.fromBytes(buffer);
	IMessageHandler<IMessage, IMessage> handler = TF2weapons.network.handlerList.get(message.getClass());
	if(constr == null) {
		constr =MessageContext.class.getDeclaredConstructor(INetHandler.class, Side.class);
		constr.setAccessible(true);
	}
	MessageContext context = constr.newInstance(Minecraft.getMinecraft().player.connection, Side.CLIENT); 
	handler.onMessage(message, context);
	System.out.println("PacketFrom: "+msg.sender().getAddress()+ " "+msg.sender().getPort()+" ");
	
}
 
開發者ID:rafradek,項目名稱:Mods,代碼行數:21,代碼來源:UdpChannelHandlerClient.java

示例2: createListenerHandler

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
private SimpleChannelInboundHandler<DatagramPacket> createListenerHandler(SeedNode thisNode, ByteBuf seedNodeInfo) {
    return new SimpleChannelInboundHandler<DatagramPacket>() {
        @Override
        public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
            ByteBuf buf = msg.content();

            if (buf.readableBytes() > 4 && buf.readInt() == Utils.MAGIC_BYTES) {
                MessageTYpe msgType = MessageTYpe.values()[buf.readByte()];

                if (msgType == MessageTYpe.DISCOVERY) {
                    String cluster = decodeUtf(buf);
                    InetSocketAddress address = decodeAddress(buf);

                    if (thisNode.cluster().equals(cluster) && !address.equals(thisNode.address())) {
                        onDiscoveryMessage(address);

                        DatagramPacket response = new DatagramPacket(seedNodeInfo.copy(), msg.sender());

                        ctx.writeAndFlush(response);
                    }
                }
            }
        }
    };
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:26,代碼來源:MulticastSeedNodeProvider.java

示例3: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
	channel = ctx.channel();
	sender = msg.sender();

	ByteBuf content = msg.content();
	
	int methodID = content.readInt();
	int invID = content.readInt();
	int argCount = content.readInt();
	Object[] args = new Object[argCount];
	for(int i = 0; i < argCount; i++) {
		Class<?> clazz = Class.forName(readString(content));
		args[i] = gson.fromJson(readString(content), clazz);
	}

	provider.invoke(methodID, invID, args);
}
 
開發者ID:thilokru,項目名稱:Controller-Support,代碼行數:19,代碼來源:NetworkHandler.java

示例4: add

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
/**
 * Try to add the given {@link DatagramPacket}. Returns {@code true} on success,
 * {@code false} otherwise.
 */
boolean add(DatagramPacket packet) {
    if (count == packets.length) {
        return false;
    }
    ByteBuf content = packet.content();
    int len = content.readableBytes();
    if (len == 0) {
        return true;
    }
    NativeDatagramPacket p = packets[count];
    InetSocketAddress recipient = packet.recipient();
    if (!p.init(content, recipient)) {
        return false;
    }

    count++;
    return true;
}
 
開發者ID:wuyinxian124,項目名稱:netty4.0.27Learn,代碼行數:23,代碼來源:NativeDatagramPacketArray.java

示例5: channelRead

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

	DatagramPacket packet = (DatagramPacket) msg;
	try {

		InputStream stream = new ByteBufInputStream(packet.content());
		Object data = new CompactObjectInputStream(stream, ClassResolvers.cacheDisabled(null)).readObject();
		MDC.put("id", LogSourceId.getInstance().getId());
		logger.callAppenders(((LoggingEventWrapper) data).event);

	} catch (Throwable e){
		System.out.println(e);
	}
	ReferenceCountUtil.release(msg);
}
 
開發者ID:d0k1,項目名稱:DistributedLog4j,代碼行數:17,代碼來源:Log4jHandler.java

示例6: decode

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
/**
   * Decodes a {@link DatagramPacket} to a {@link DataPacket} wrapped into an {@link AddressedEnvelope} to allow multicast on
   * the used {@link SocketChannel}. 
   * 
   * @param ctx The context of the ChannelHandler
   * @param msg the message which should be encoded
   * @param out a list where all messages are written to
   */
  @Override
  protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
  	final ByteBuf content = msg.content();
final SocketAddress sender = msg.sender();
final SocketAddress recipient = msg.recipient();

      try {
          final DataPacket dataPacket = DataPacket.decode(content);
          final AddressedEnvelope<DataPacket, SocketAddress> newMsg = 
  				new DefaultAddressedEnvelope<>(
  						dataPacket, recipient, sender);
  		out.add(newMsg);
      } catch (Exception e) {
          LOG.debug("Failed to decode RTP packet.", e);
      }
  }
 
開發者ID:SAS-Systems,項目名稱:imflux,代碼行數:25,代碼來源:UdpDataPacketDecoder.java

示例7: channelRead

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    DatagramPacket datagram = (DatagramPacket) msg;
    InetSocketAddress sender = datagram.sender();
    Channel tcpChannel = XChannelMapper.getTcpChannel(sender);
    if (tcpChannel == null) {
        // udpSource not registered, actively discard this packet
        // without register, an udp channel cannot relate to any tcp channel, so remove the map
        XChannelMapper.removeUdpMapping(sender);
        log.warn("Bad Connection! (unexpected udp datagram from {})", sender);
    } else if (tcpChannel.isActive()) {
        ByteBuf byteBuf = datagram.content();
        try {
            if (!byteBuf.hasArray()) {
                byte[] bytes = new byte[byteBuf.readableBytes()];
                byteBuf.getBytes(0, bytes);
                log.info("\t          Proxy << Target \tFrom   {}:{}", sender.getHostString(), sender.getPort());

                // write udp payload via tcp channel
                tcpChannel.writeAndFlush(Unpooled.wrappedBuffer(wrapper.wrap(requestResolver.wrap(XRequest.Channel.UDP, bytes))));
                log.info("\tClient << Proxy           \tGet [{} bytes]", bytes.length);
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }
}
 
開發者ID:ZhangJiupeng,項目名稱:AgentX,代碼行數:28,代碼來源:Udp2TcpHandler.java

示例8: decode

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@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);
    try {
        Message msg = Message.decode(encoded);
        DiscoveryEvent event = new DiscoveryEvent(msg, packet.sender());
        out.add(event);
    } catch (Exception e) {
        throw new RuntimeException("Exception processing inbound message from " + ctx.channel().remoteAddress() + ": " + Hex.toHexString(encoded), e);
    }
}
 
開發者ID:talentchain,項目名稱:talchain,代碼行數:14,代碼來源:PacketDecoder.java

示例9: channelRead

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	DatagramPacket datagramPacket = (DatagramPacket) msg;
	ByteBuf buf = datagramPacket.content();
	if (isHelloPacket(buf)) {
		FreeroamTalker freeroamTalker = new FreeroamTalker(ctx, datagramPacket);
		FreeroamAllTalkers.put(freeroamTalker);
		freeroamTalker.send(welcomePacket(freeroamTalker));
	}
	super.channelRead(ctx, msg);
}
 
開發者ID:nilzao,項目名稱:sbrw-freeroam-srv,代碼行數:12,代碼來源:HelloHandler.java

示例10: channelRead

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	DatagramPacket datagramPacket = (DatagramPacket) msg;
	ByteBuf buf = datagramPacket.content();
	if (isPlayerInfoPacket(buf)) {
		FreeroamTalker freeroamTalker = FreeroamAllTalkers.get(datagramPacket);
		if (freeroamTalker != null) {
			freeroamTalker.parsePlayerInfo(ByteBufUtil.getBytes(buf));
		}
	}
	super.channelRead(ctx, msg);
}
 
開發者ID:nilzao,項目名稱:sbrw-freeroam-srv,代碼行數:13,代碼來源:PlayerInfoHandler.java

示例11: decode

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@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);
    out.add(this.decodeMessage(ctx, encoded, packet.sender()));
}
 
開發者ID:rsksmart,項目名稱:rskj,代碼行數:8,代碼來源:PacketDecoder.java

示例12: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
	ByteBuf buffer = msg.content();
	
	int plid = buffer.readShort();
	int seq = buffer.readUnsignedShort();
	int msgid = buffer.readByte();
	
	if(msgid == (byte)-128) {
		server.outboundTargets.put((short) plid, SocketUtils.socketAddress(msg.sender().getAddress().getHostAddress(),msg.sender().getPort()));
	}
	else {
		EntityPlayerMP player = server.playerList.get((short)plid);
		IMessage message = TF2weapons.network.messages[msgid].newInstance();
		//buffer.discardReadBytes();
		message.fromBytes(buffer);
		IMessageHandler<IMessage, IMessage> handler = TF2weapons.network.handlerList.get(message.getClass());
		if(constr == null) {
			constr =MessageContext.class.getDeclaredConstructor(INetHandler.class, Side.class);
			constr.setAccessible(true);
		}
		MessageContext context = constr.newInstance(player.connection, Side.SERVER); 
		handler.onMessage(message, context);
	}
	System.out.println("PacketFrom: "+msg.sender().getAddress()+ " "+msg.sender().getPort()+" ");
	
}
 
開發者ID:rafradek,項目名稱:Mods,代碼行數:28,代碼來源:UdpChannelHandlerServer.java

示例13: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket dp) throws Exception {
  ByteBufInputStream bis = new ByteBufInputStream(dp.content());
  try {
    ClusterStatusProtos.ClusterStatus csp = ClusterStatusProtos.ClusterStatus.parseFrom(bis);
    ClusterStatus ncs = ClusterStatus.convert(csp);
    receive(ncs);
  } finally {
    bis.close();
  }
}
 
開發者ID:fengchen8086,項目名稱:ditb,代碼行數:12,代碼來源:ClusterStatusListener.java

示例14: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    ByteBuf buffer = msg.content();
    int packetType = buffer.readInt();
    long timestamp = buffer.readLong();
    if(packetType!= PacketType.PACKET_SCENE_STATE)
    System.out.println("server sends message type: " + packetType);

    switch (packetType) {
        case PacketType.PACKET_CONNECTED:
            connected(buffer.readInt());
            break;
        case PacketType.PACKET_CONNECTION_REFUSED:
            System.out.println("Connection refused!");
            break;

        case PacketType.PACKET_SCENE_STATE:
            sceneHolder.offerScene(timestamp, buffer);
            break;
        case PacketType.PACKET_EVENT:
            int eventType = buffer.readInt();
            int dependencyId = buffer.readInt();
            int targetComponentId = buffer.readInt();
            eventReceiver.addEvent(buffer, targetComponentId, eventType, dependencyId, timestamp);
            connectionService.confirmEvent(dependencyId);
            break;

        case PacketType.PACKET_EVENT_CONFIRMATION:
            int id = buffer.readInt();
            eventQueue.confirmEvent(id);
            break;
        case PacketType.PACKET_CLOCK_SYNCHRONIZATION_RESPONSE:
            System.out.println(System.currentTimeMillis());
            System.out.println(timestamp);
            handleClockSynchronizationResponse(timestamp);
            break;
    }
}
 
開發者ID:WarpOrganization,項目名稱:warp,代碼行數:39,代碼來源:ServerConnectionHandler.java

示例15: channelRead0

import io.netty.channel.socket.DatagramPacket; //導入方法依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
    ByteBuf buffer = msg.content();
    int type = buffer.readInt();
    long timestamp = buffer.readLong();
    System.out.println("server got a message type: " + type);
    int clientId;
    switch (type) {
        case PacketType.PACKET_CONNECT:
            registerClient(ctx.channel(), msg.sender());
            break;
        case PacketType.PACKET_KEEP_ALIVE:
            clientRegistry.updateKeepAlive(buffer.readInt());
            break;
        case PacketType.PACKET_EVENT:
            handleEvent(timestamp, buffer);
            break;
        case PacketType.PACKET_EVENT_CONFIRMATION:
            handleEventConfirmation(buffer.readInt(), buffer.readInt());
            break;
        case PacketType.PACKET_CLOCK_SYNCHRONIZATION_REQUEST:
            handleClockSynchronizationRequest(buffer.readInt());
            break;
        case PacketType.PACKET_CLOCK_SYNCHRONIZATION_RESPONSE:
            handleClockSynchronizationResponse(buffer.readInt(), timestamp);
            break;

    }
}
 
開發者ID:WarpOrganization,項目名稱:warp,代碼行數:30,代碼來源:ConnectionHandler.java


注:本文中的io.netty.channel.socket.DatagramPacket.content方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。