Java Code Examples for java.net.DatagramPacket#getSocketAddress()
The following examples show how to use
java.net.DatagramPacket#getSocketAddress() .
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: UDPDestination.java From cxf with Apache License 2.0 | 5 votes |
public void run() { while (true) { if (mcast == null) { return; } try { byte[] bytes = new byte[64 * 1024]; final DatagramPacket p = new DatagramPacket(bytes, bytes.length); mcast.receive(p); LoadingByteArrayOutputStream out = new LoadingByteArrayOutputStream() { public void close() throws IOException { super.close(); final DatagramPacket p2 = new DatagramPacket(getRawBytes(), 0, this.size(), p.getSocketAddress()); mcast.send(p2); } }; final MessageImpl m = new MessageImpl(); final Exchange exchange = new ExchangeImpl(); exchange.setDestination(UDPDestination.this); m.setDestination(UDPDestination.this); exchange.setInMessage(m); m.setContent(InputStream.class, new ByteArrayInputStream(bytes, 0, p.getLength())); m.put(OutputStream.class, out); queue.execute(() -> getMessageObserver().onMessage(m)); } catch (IOException ex) { ex.printStackTrace(); } } }
Example 2
Source File: SpanStreamUDPPacketHandlerFactory.java From pinpoint with Apache License 2.0 | 4 votes |
@Override public void receive(DatagramSocket localSocket, DatagramPacket packet) { final HeaderTBaseDeserializer deserializer = deserializerFactory.createDeserializer(); ByteBuffer requestBuffer = ByteBuffer.wrap(packet.getData()); if (requestBuffer.remaining() < SpanStreamConstants.START_PROTOCOL_BUFFER_SIZE) { return; } byte signature = requestBuffer.get(); if (signature != SpanStreamConstants.Protocol.SPAN_STREAM_SIGNATURE) { logger.warn("Wrong signature: 0x" + Integer.toHexString(signature & 0xFF) + " (expected: 0x" + Integer.toHexString(SpanStreamConstants.Protocol.SPAN_STREAM_SIGNATURE & 0xFF) + ')'); return; } byte version = requestBuffer.get(); int chunkSize = 0xff & requestBuffer.get(); InetSocketAddress remoteSocketAddress = (InetSocketAddress) packet.getSocketAddress(); try { for (int i = 0; i < chunkSize; i++) { byte[] componentData = getComponentData(requestBuffer, deserializer); if (componentData == null) { logger.warn("Buffer Wrong signature: 0x{} (expected: 0x{})", Integer.toHexString(signature & 0xFF), Integer.toHexString(SpanStreamConstants.Protocol.SPAN_STREAM_SIGNATURE & 0xFF)); break; } List<Message<TBase<?, ?>>> requestList = deserializer.deserializeList(componentData); if (CollectionUtils.isEmpty(requestList)) { continue; } if (requestList.size() == 1) { if (filter.filter(localSocket, requestList.get(0).getData(), remoteSocketAddress) == TBaseFilter.BREAK) { continue; } } List<TSpanEvent> spanEventList = getSpanEventList(requestList); Message<TBase<?, ?>> lastMessage = requestList.get(requestList.size() - 1); TBase tBase = lastMessage.getData(); if (tBase instanceof TSpan) { ((TSpan) tBase).setSpanEventList(spanEventList); } else if (tBase instanceof TSpanChunk) { ((TSpanChunk) tBase).setSpanEventList(spanEventList); } Message<TBase<?, ?>> message = new DefaultMessage<>(lastMessage.getHeader(), lastMessage.getHeaderEntity(), tBase); ServerRequest<TBase<?, ?>> mergedRequest = newServerRequest(message, remoteSocketAddress); dispatchHandler.dispatchRequestMessage(mergedRequest, fake); } } catch (Exception e) { logger.warn("Failed to handle receive packet.", e); } }