Search in sources :

Example 11 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method send.

private int send(int fd, ByteBuffer src, int assocId, SocketAddress target, MessageInfo messageInfo) throws IOException {
    int streamNumber = messageInfo.streamNumber();
    boolean unordered = messageInfo.isUnordered();
    int ppid = messageInfo.payloadProtocolID();
    if (src instanceof DirectBuffer)
        return sendFromNativeBuffer(fd, src, target, assocId, streamNumber, unordered, ppid);
    /* Substitute a native buffer */
    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim && streamNumber >= 0);
    int rem = (pos <= lim ? lim - pos : 0);
    ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
    try {
        bb.put(src);
        bb.flip();
        /* Do not update src until we see how many bytes were written */
        src.position(pos);
        int n = sendFromNativeBuffer(fd, bb, target, assocId, streamNumber, unordered, ppid);
        if (n > 0) {
            /* now update src */
            src.position(pos + n);
        }
        return n;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 12 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.

the class SctpMultiChannelImpl method sendFromNativeBuffer.

private int sendFromNativeBuffer(int fd, ByteBuffer bb, SocketAddress target, int assocId, int streamNumber, boolean unordered, int ppid) throws IOException {
    // no preferred address
    InetAddress addr = null;
    int port = 0;
    if (target != null) {
        InetSocketAddress isa = Net.checkAddress(target);
        addr = isa.getAddress();
        port = isa.getPort();
    }
    int pos = bb.position();
    int lim = bb.limit();
    assert (pos <= lim);
    int rem = (pos <= lim ? lim - pos : 0);
    int written = send0(fd, ((DirectBuffer) bb).address() + pos, rem, addr, port, assocId, streamNumber, unordered, ppid);
    if (written > 0)
        bb.position(pos + written);
    return written;
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 13 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project jdk8u_jdk by JetBrains.

the class SctpChannelImpl method send.

private int send(int fd, ByteBuffer src, MessageInfo messageInfo) throws IOException {
    int streamNumber = messageInfo.streamNumber();
    SocketAddress target = messageInfo.address();
    boolean unordered = messageInfo.isUnordered();
    int ppid = messageInfo.payloadProtocolID();
    if (src instanceof DirectBuffer)
        return sendFromNativeBuffer(fd, src, target, streamNumber, unordered, ppid);
    /* Substitute a native buffer */
    int pos = src.position();
    int lim = src.limit();
    assert (pos <= lim && streamNumber >= 0);
    int rem = (pos <= lim ? lim - pos : 0);
    ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
    try {
        bb.put(src);
        bb.flip();
        /* Do not update src until we see how many bytes were written */
        src.position(pos);
        int n = sendFromNativeBuffer(fd, bb, target, streamNumber, unordered, ppid);
        if (n > 0) {
            /* now update src */
            src.position(pos + n);
        }
        return n;
    } finally {
        Util.releaseTemporaryDirectBuffer(bb);
    }
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer)

Example 14 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project fast-cast by RuedigerMoeller.

the class ShareMemPingPong method oneRun.

public static void oneRun(boolean odd, int runs, long start, File counters) throws IOException {
    Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    byte[] msg = new byte[40];
    try (FileChannel fc = new RandomAccessFile(counters, "rw").getChannel()) {
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
        long address = ((DirectBuffer) mbb).address();
        for (int i = -1; i < runs; i++) {
            for (; ; ) {
                long tim = System.nanoTime();
                long value = UNSAFE.getLongVolatile(null, address);
                boolean isOdd = (value & 1) != 0;
                if (isOdd != odd)
                    // wait for the other side.
                    continue;
                // simulate reading message
                mbb.position(8);
                mbb.get(msg, 0, msg.length);
                // simulate writing msg
                msg[12] = (byte) i;
                mbb.position(8);
                mbb.put(msg, 0, msg.length);
                histo.recordValue(System.nanoTime() - tim);
                // make the change atomic, just in case there is more than one odd/even process
                if (UNSAFE.compareAndSwapLong(null, address, value, value + 1))
                    break;
            }
            if (i == 0) {
                System.out.println("Started");
                start = System.nanoTime();
            }
        }
    }
    System.out.printf("... Finished, average ping/pong took %,d ns%n", (System.nanoTime() - start) / runs);
    histo.outputPercentileDistribution(System.out, 1000.0);
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) Histogram(org.HdrHistogram.Histogram) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel)

Example 15 with DirectBuffer

use of sun.nio.ch.DirectBuffer in project ignite by apache.

the class GridUnsafe method wrapPointer.

/**
     * @param ptr Pointer to wrap.
     * @param len Memory location length.
     * @return Byte buffer wrapping the given memory.
     */
public static ByteBuffer wrapPointer(long ptr, int len) {
    ByteBuffer buf = nioAccess.newDirectByteBuffer(ptr, len, null);
    assert buf instanceof DirectBuffer;
    buf.order(NATIVE_BYTE_ORDER);
    return buf;
}
Also used : DirectBuffer(sun.nio.ch.DirectBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DirectBuffer (sun.nio.ch.DirectBuffer)15 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.Test)4 InetSocketAddress (java.net.InetSocketAddress)3 InetAddress (java.net.InetAddress)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Cleaner (sun.misc.Cleaner)2 RandomAccessFile (java.io.RandomAccessFile)1 SocketAddress (java.net.SocketAddress)1 FileChannel (java.nio.channels.FileChannel)1 Histogram (org.HdrHistogram.Histogram)1