当前位置: 首页>>代码示例>>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;未经允许,请勿转载。