return write(sources, 0, sources.length);
@Override protected void write(byte[] data) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(data); while (buffer.hasRemaining()) { channel.write(buffer); } }
@Override public void run() { while (true) { try { WriteTask wt = tasks.take(); byte[] eventBytes = serialize(wt.event); if (eventBytes.length > wt.maxSize) { if (LOG.isDebugEnabled()) { LOG.debug("Event exceeds the send maximum event size of " + wt.maxSize + ". Dropping event."); } } else { wt.channel.write(ByteBuffer.wrap(eventBytes)); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); LOG.debug("Writer thread interrupted", ie); break; } catch (Exception e) { LOG.debug("Exception thrown while attempting to send event to agent", e); } } } }
private void handshake(DatagramChannel tunnel) throws Exception { // To build a secured tunnel, we should perform mutual authentication // and exchange session keys for encryption. To keep things simple in // this demo, we just send the shared secret in plaintext and wait // for the server to send the parameters. // Allocate the buffer for handshaking. ByteBuffer packet = ByteBuffer.allocate(1024); // Control messages always start with zero. packet.put((byte) 0).put(mSharedSecret).flip(); // Send the secret several times in case of packet loss. for (int i = 0; i < 3; ++i) { packet.position(0); tunnel.write(packet); } packet.clear(); // Wait for the parameters within a limited time. for (int i = 0; i < 50; ++i) { Thread.sleep(100); // Normally we should not receive random packets. int length = tunnel.read(packet); if (length > 0 && packet.get(0) == 0) { configure(new String(packet.array(), 1, length - 1).trim()); return; } } throw new IllegalStateException("Timed out"); }
buffer.flip(); writeChannel.write(buffer);
buffer.flip(); writeChannel.write(buffer);
@Override protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception { final SocketAddress remoteAddress; final ByteBuf data; if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg; remoteAddress = envelope.recipient(); data = envelope.content(); } else { data = (ByteBuf) msg; remoteAddress = null; } final int dataLen = data.readableBytes(); if (dataLen == 0) { return true; } final ByteBuffer nioData = data.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen) : data.nioBuffer(data.readerIndex(), dataLen); final int writtenBytes; if (remoteAddress != null) { writtenBytes = javaChannel().send(nioData, remoteAddress); } else { writtenBytes = javaChannel().write(nioData); } return writtenBytes > 0; }
@Override protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception { final SocketAddress remoteAddress; final ByteBuf data; if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg; remoteAddress = envelope.recipient(); data = envelope.content(); } else { data = (ByteBuf) msg; remoteAddress = null; } final int dataLen = data.readableBytes(); if (dataLen == 0) { return true; } final ByteBuffer nioData = data.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen) : data.nioBuffer(data.readerIndex(), dataLen); final int writtenBytes; if (remoteAddress != null) { writtenBytes = javaChannel().send(nioData, remoteAddress); } else { writtenBytes = javaChannel().write(nioData); } return writtenBytes > 0; }
@Override public void run() { byte[] bytes = message.getBytes(Charset.forName("UTF-8")); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); try (DatagramChannel channel = DatagramChannel.open()) { channel.connect(new InetSocketAddress("localhost", port)); for (int i = 0; i < numMessages; i++) { buffer.clear(); buffer.put(bytes); buffer.flip(); while (buffer.hasRemaining()) { channel.write(buffer); } Thread.sleep(delay); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } catch (InterruptedException e) { LOGGER.error(e.getMessage(), e); } } }
if (sendDatagramChannel.isConnected()) bytesSent = sendDatagramChannel.write(buffer);
@Override protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception { final SocketAddress remoteAddress; final ByteBuf data; if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg; remoteAddress = envelope.recipient(); data = envelope.content(); } else { data = (ByteBuf) msg; remoteAddress = null; } final int dataLen = data.readableBytes(); if (dataLen == 0) { return true; } final ByteBuffer nioData = data.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen) : data.nioBuffer(data.readerIndex(), dataLen); final int writtenBytes; if (remoteAddress != null) { writtenBytes = javaChannel().send(nioData, remoteAddress); } else { writtenBytes = javaChannel().write(nioData); } return writtenBytes > 0; }
@Override public void run() { while (true) { try { WriteTask wt = tasks.take(); byte[] eventBytes = serialize(wt.event); if (eventBytes.length > wt.maxSize) { if (LOG.isDebugEnabled()) { LOG.debug("Event exceeds the send maximum event size of " + wt.maxSize + ". Dropping event."); } } else { wt.channel.write(ByteBuffer.wrap(eventBytes)); } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); LOG.debug("Writer thread interrupted", ie); break; } catch (Exception e) { LOG.debug("Exception thrown while attempting to send event to agent", e); } } } }
/** * {@inheritDoc} */ @Override protected int writeDirect(Object message) { try { // Check that we can write into the channel if (!isRegisteredForWrite()) { // We don't have pending writes // First, connect if we aren't already connected if (!((DatagramChannel) channel).isConnected()) { ((DatagramChannel) channel).connect(remoteAddress); } // And try to write the data. We will either write them all, // or none return ((DatagramChannel) channel).write((ByteBuffer) message); } else { LOG.debug("Cannot write"); return -1; } } catch (final IOException e) { LOG.error("Exception while reading : ", e); processException(e); return -1; } }
public int write(ByteBuffer src) throws IOException { fastFirstPacketRead = false; int res = 0; int size = src.remaining(); while (res < size) { res += channel.write(src); } return res; }
void send(byte [] data) throws IOException { DatagramChannel channel = (DatagramChannel) key.channel(); verboseLog("UDP write", data); channel.write(ByteBuffer.wrap(data)); }
public Integer call() throws IOException { try { return channel.write(src); } catch (ClosedChannelException e) { throw Util.initCause( new AsynchronousCloseException(), e); } } }); }
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { final Object context = IoTraceHandler.datagramWriteBegin(); final long bytes = delegate.write(srcs, offset, length); if(bytes>0) { IoTraceHandler.datagramWriteEnd(context, getRemoteAddress(), bytes); } return bytes; } }
void send(byte [] data) throws IOException { DatagramChannel channel = (DatagramChannel) key.channel(); verboseLog("UDP write", channel.socket().getLocalSocketAddress(), channel.socket().getRemoteSocketAddress(), data); channel.write(ByteBuffer.wrap(data)); }
public int write(ByteBuffer src) throws IOException { final Object context = IoTraceHandler.datagramWriteBegin(); final int bytes = delegate.write(src); if(bytes>0) { IoTraceHandler.datagramWriteEnd(context, getRemoteAddress(), bytes); } return bytes; }