本文整理汇总了Java中io.netty.channel.socket.DatagramPacket.sender方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.sender方法的具体用法?Java DatagramPacket.sender怎么用?Java DatagramPacket.sender使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.socket.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.sender方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: channelRead
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof DatagramPacket) {
// Get packet and sender data
DatagramPacket datagram = (DatagramPacket) msg;
InetSocketAddress sender = datagram.sender();
RakNetPacket packet = new RakNetPacket(datagram);
// If an exception happens it's because of this address
this.causeAddress = sender;
// Handle the packet and release the buffer
client.handleMessage(packet, sender);
datagram.content().readerIndex(0); // Reset position
client.getListener().handleNettyMessage(datagram.content(), sender);
datagram.content().release(); // No longer needed
// No exceptions occurred, release the suspect
this.causeAddress = null;
}
}
示例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: channelRead0
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
final SocketAddress remoteAddress = msg.sender();
final CodecAggregator.Result result;
try (Timer.Context ignored = aggregationTimer.time()) {
result = aggregator.addChunk(msg.content(), remoteAddress);
}
final ByteBuf completeMessage = result.getMessage();
if (completeMessage != null) {
LOG.debug("Message aggregation completion, forwarding {}", completeMessage);
ctx.fireChannelRead(completeMessage);
} else if (result.isValid()) {
LOG.debug("More chunks necessary to complete this message");
} else {
invalidChunksMeter.mark();
LOG.debug("Message chunk was not valid and discarded.");
}
}
示例5: 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);
}
}
示例6: channelRead
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof DatagramPacket) {
// Get packet and sender data
DatagramPacket datagram = (DatagramPacket) msg;
InetSocketAddress sender = datagram.sender();
RakNetPacket packet = new RakNetPacket(datagram);
// If an exception happens it's because of this address
this.causeAddress = sender;
// Is the sender blocked?
if (this.addressBlocked(sender.getAddress())) {
BlockedAddress status = blocked.get(sender.getAddress());
if (status.getTime() <= BlockedAddress.PERMANENT_BLOCK) {
return; // Permanently blocked
}
if (System.currentTimeMillis() - status.getStartTime() < status.getTime()) {
return; // Time hasn't expired
}
this.unblockAddress(sender.getAddress());
}
// Handle the packet and release the buffer
server.handleMessage(packet, sender);
datagram.content().readerIndex(0); // Reset position
server.getListener().handleNettyMessage(datagram.content(), sender);
datagram.content().release(); // No longer needed
// No exceptions occurred, release the suspect
this.causeAddress = null;
}
}
示例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: decode
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
ByteBuf buf = msg.content();
int i = buf.indexOf(0, buf.readableBytes(), LogEvent.SEPARATOR);
String filename = buf.slice(0, i).toString(CharsetUtil.UTF_8);
String logMsg = buf.slice(i + 1, buf.readableBytes()).toString(CharsetUtil.UTF_8);
LogEvent event = new LogEvent(msg.sender(), System.currentTimeMillis(), filename, logMsg);
out.add(event);
}
示例10: sendDiscoveryResponse
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
/**
* Send a response with the ConnectInformation of this Master to the requesting Client.
*
* @param request the request sent from a {@link Client}
*/
private void sendDiscoveryResponse(DatagramPacket request) {
final ByteBuf buffer = channel.channel().alloc().heapBuffer();
// gather information
final byte[] header = DISCOVERY_PAYLOAD_RESPONSE.getBytes();
final String addressString = request.recipient().getAddress().getHostAddress();
final byte[] address = addressString.getBytes();
final InetSocketAddress serverAddress = requireComponent(Server.KEY).getAddress();
if (serverAddress == null) {
Log.w(TAG, "Could not respond to UDP discovery request as Server is not started yet");
return;
}
final int port = serverAddress.getPort();
Log.i(TAG, "sendDiscoveryResponse with connection data " + addressString + ":" + port + " to " + request.sender());
// write it to the buffer
buffer.writeInt(header.length);
buffer.writeBytes(header);
buffer.writeInt(address.length);
buffer.writeBytes(address);
buffer.writeInt(port);
// and sign the data
try {
Signature signature = Signature.getInstance("ECDSA");
signature.initSign(requireComponent(KeyStoreController.KEY).getOwnPrivateKey());
signature.update(buffer.nioBuffer());
final byte[] sign = signature.sign();
buffer.writeInt(sign.length);
buffer.writeBytes(sign);
} catch (GeneralSecurityException e) {
Log.w(TAG, "Could not send UDP discovery response", e);
}
final DatagramPacket response = new DatagramPacket(buffer, request.sender());
channel.channel().writeAndFlush(response);
}
示例11: decode
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
if (filter(msg)) {
InetSocketAddress sender = msg.sender();
LOG.info("Access denied for host(IP) : [" + sender.getHostName() + ":" + sender.getPort() + "].");
return;
}
String data = translate(msg);
if (data == null) {
LOG.info("The message data is null.");
return;
}
out.add(data);
}
示例12: onReceive
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
/**
* receive DatagramPacket
*
* @param dp
*/
private void onReceive(DatagramPacket dp)
{
if (this.running)
{
InetSocketAddress sender = dp.sender();
int hash = sender.hashCode();
hash = hash < 0 ? -hash : hash;
this.workers[hash % workers.length].input(dp);
} else
{
dp.release();
}
}
示例13: decode
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
// decode the DatagramPackage to AddressedEnvelope
DefaultAddressedEnvelope<Object, InetSocketAddress> addressEvelop =
new DefaultAddressedEnvelope<Object, InetSocketAddress>(msg.content().retain(), msg.recipient(), msg.sender());
out.add(addressEvelop);
}
示例14: receive
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
public void receive(ChannelHandlerContext ctx, DatagramPacket packet) {
ByteBuf content = packet.content();
Message message = config.serialiser.deserialiseWithStream(new ByteBufInputStream(content));
if (message == null) {
return;
}
// TODO set config and state
message.payload = content;
if (message.getFeatures() != null) {
message.getFeatures().resolve();
}
if (config.debug.simulateLossOfPacket()) {
// simulated N % loss rate
log.warn("DEBUG: simulated loss of packet: {}", message);
return;
}
message.socketAddressSender = packet.sender();
message.socketAddressRecipient = packet.recipient();
if (config.trackData) {
int frame = 0;
config.netStats.getData().add(
new NetStats.Line(false, message.getSenderId(), frame, message.getTimestamp(), message.getClass(), ((ByteBuf)message.payload).writerIndex()));
}
// Let the controller receive the message.
// Processors are called there.
config.internalReceiver.receive(message);
}
示例15: decode
import io.netty.channel.socket.DatagramPacket; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws
Exception {
ctx.attr(PipelineUtils.ADDRESS_ATTRIBUTE).set(msg.sender());
byte packetId = msg.content().readByte();
PacketHeader header = new PacketHeader(packetId);
Server.getServer().getLogger().debug(PocketLogging.Server.NETWORK, header.toString());
Packet packet = PacketRegistry.construct(packetId);
Object[] params = new Object[]{
null,
msg.sender()
};
if (Server.getServer().getLogger().isTraceEnabled()) {
params[0] = packet.toString();
} else {
params[0] = String.format("0x%02X", packetId);
}
Server.getServer().getLogger().debug(PocketLogging.Server.NETWORK, "Received {} from {}",
params);
packet.record(this);
try {
packet.read(msg.content());
out.add(packet);
} finally {
packet.close();
}
}