本文整理汇总了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()+" ");
}
示例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);
}
}
}
}
};
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
}
示例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);
}
}
}
示例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);
}
}
示例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);
}
示例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);
}
示例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()));
}
示例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()+" ");
}
示例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();
}
}
示例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;
}
}
示例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;
}
}