Search in sources :

Example 6 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project j2objc by google.

the class InheritedChannel method createChannel.

/*
     * If standard inherited channel is connected to a socket then return a Channel
     * of the appropriate type based standard input.
     */
private static Channel createChannel() throws IOException {
    // dup the file descriptor - we do this so that for two reasons :-
    // 1. Avoids any timing issues with FileDescriptor.in being closed
    // or redirected while we create the channel.
    // 2. Allows streams based on file descriptor 0 to co-exist with
    // the channel (closing one doesn't impact the other)
    int fdVal = dup(0);
    // Examine the file descriptor - if it's not a socket then we don't
    // create a channel so we release the file descriptor.
    int st;
    st = soType0(fdVal);
    if (st != SOCK_STREAM && st != SOCK_DGRAM) {
        close0(fdVal);
        return null;
    }
    // Next we create a FileDescriptor for the dup'ed file descriptor
    // Have to use reflection and also make assumption on how FD
    // is implemented.
    Class[] paramTypes = { int.class };
    Constructor ctr = Reflect.lookupConstructor("java.io.FileDescriptor", paramTypes);
    Object[] args = { new Integer(fdVal) };
    FileDescriptor fd = (FileDescriptor) Reflect.invoke(ctr, args);
    // Now create the channel. If the socket is a streams socket then
    // we see if tthere is a peer (ie: connected). If so, then we
    // create a SocketChannel, otherwise a ServerSocketChannel.
    // If the socket is a datagram socket then create a DatagramChannel
    SelectorProvider provider = SelectorProvider.provider();
    assert provider instanceof sun.nio.ch.SelectorProviderImpl;
    Channel c;
    if (st == SOCK_STREAM) {
        InetAddress ia = peerAddress0(fdVal);
        if (ia == null) {
            c = new InheritedServerSocketChannelImpl(provider, fd);
        } else {
            int port = peerPort0(fdVal);
            assert port > 0;
            InetSocketAddress isa = new InetSocketAddress(ia, port);
            c = new InheritedSocketChannelImpl(provider, fd, isa);
        }
    } else {
        c = new InheritedDatagramChannelImpl(provider, fd);
    }
    return c;
}
Also used : Constructor(java.lang.reflect.Constructor) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) DatagramChannel(java.nio.channels.DatagramChannel) Channel(java.nio.channels.Channel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) FileDescriptor(java.io.FileDescriptor) SelectorProvider(java.nio.channels.spi.SelectorProvider) InetAddress(java.net.InetAddress)

Example 7 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project baseio by generallycloud.

the class JavaEventLoop method open_selector.

@SuppressWarnings("rawtypes")
private static Selector open_selector(final SelectionKeySet keySet) throws IOException {
    final SelectorProvider provider = SelectorProvider.provider();
    final Selector selector = provider.openSelector();
    Object res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                return Class.forName("sun.nio.ch.SelectorImpl");
            } catch (Throwable cause) {
                return cause;
            }
        }
    });
    if (res instanceof Throwable) {
        return selector;
    }
    final Class selectorImplClass = (Class) res;
    res = AccessController.doPrivileged(new PrivilegedAction<Object>() {

        @Override
        public Object run() {
            try {
                Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
                Throwable cause = Util.trySetAccessible(selectedKeysField);
                if (cause != null) {
                    return cause;
                }
                cause = Util.trySetAccessible(publicSelectedKeysField);
                if (cause != null) {
                    return cause;
                }
                selectedKeysField.set(selector, keySet);
                publicSelectedKeysField.set(selector, keySet);
                return null;
            } catch (Throwable e) {
                return e;
            }
        }
    });
    if (res instanceof Throwable) {
        return selector;
    }
    return selector;
}
Also used : Field(java.lang.reflect.Field) SelectorProvider(java.nio.channels.spi.SelectorProvider) PrivilegedAction(java.security.PrivilegedAction) Selector(java.nio.channels.Selector)

Example 8 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project robovm by robovm.

the class OldSystemTest method test_inheritedChannel.

public void test_inheritedChannel() throws IOException {
    Channel iChannel = System.inheritedChannel();
    assertNull("Incorrect value of channel", iChannel);
    SelectorProvider sp = SelectorProvider.provider();
    assertEquals("Incorrect value of channel", sp.inheritedChannel(), iChannel);
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider) Channel(java.nio.channels.Channel)

Example 9 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.

the class InheritedChannel method createChannel.

/*
     * If standard inherited channel is connected to a socket then return a Channel
     * of the appropriate type based standard input.
     */
private static Channel createChannel() throws IOException {
    // dup the file descriptor - we do this so that for two reasons :-
    // 1. Avoids any timing issues with FileDescriptor.in being closed
    //    or redirected while we create the channel.
    // 2. Allows streams based on file descriptor 0 to co-exist with
    //    the channel (closing one doesn't impact the other)
    int fdVal = dup(0);
    // Examine the file descriptor - if it's not a socket then we don't
    // create a channel so we release the file descriptor.
    int st;
    st = soType0(fdVal);
    if (st != SOCK_STREAM && st != SOCK_DGRAM) {
        close0(fdVal);
        return null;
    }
    // Next we create a FileDescriptor for the dup'ed file descriptor
    // Have to use reflection and also make assumption on how FD
    // is implemented.
    Class[] paramTypes = { int.class };
    Constructor<?> ctr = Reflect.lookupConstructor("java.io.FileDescriptor", paramTypes);
    Object[] args = { new Integer(fdVal) };
    FileDescriptor fd = (FileDescriptor) Reflect.invoke(ctr, args);
    // Now create the channel. If the socket is a streams socket then
    // we see if tthere is a peer (ie: connected). If so, then we
    // create a SocketChannel, otherwise a ServerSocketChannel.
    // If the socket is a datagram socket then create a DatagramChannel
    SelectorProvider provider = SelectorProvider.provider();
    assert provider instanceof sun.nio.ch.SelectorProviderImpl;
    Channel c;
    if (st == SOCK_STREAM) {
        InetAddress ia = peerAddress0(fdVal);
        if (ia == null) {
            c = new InheritedServerSocketChannelImpl(provider, fd);
        } else {
            int port = peerPort0(fdVal);
            assert port > 0;
            InetSocketAddress isa = new InetSocketAddress(ia, port);
            c = new InheritedSocketChannelImpl(provider, fd, isa);
        }
    } else {
        c = new InheritedDatagramChannelImpl(provider, fd);
    }
    return c;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketChannel(java.nio.channels.SocketChannel) DatagramChannel(java.nio.channels.DatagramChannel) Channel(java.nio.channels.Channel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) FileDescriptor(java.io.FileDescriptor) SelectorProvider(java.nio.channels.spi.SelectorProvider) InetAddress(java.net.InetAddress)

Example 10 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.

the class PipeChannel method main.

public static void main(String[] args) throws Exception {
    for (int x = 0; x < 100; x++) {
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        Pipe.SinkChannel sink = p.sink();
        Pipe.SourceChannel source = p.source();
        ByteBuffer outgoingdata = ByteBuffer.allocateDirect(10);
        byte[] someBytes = new byte[10];
        generator.nextBytes(someBytes);
        outgoingdata.put(someBytes);
        outgoingdata.flip();
        int totalWritten = 0;
        while (totalWritten < 10) {
            int written = sink.write(outgoingdata);
            if (written < 0)
                throw new Exception("Write failed");
            totalWritten += written;
        }
        ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
        int totalRead = 0;
        do {
            int bytesRead = source.read(incomingdata);
            if (bytesRead > 0)
                totalRead += bytesRead;
        } while (totalRead < 10);
        for (int i = 0; i < 10; i++) if (outgoingdata.get(i) != incomingdata.get(i))
            throw new Exception("Pipe failed");
        sink.close();
        source.close();
    }
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SelectorProvider (java.nio.channels.spi.SelectorProvider)14 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 Channel (java.nio.channels.Channel)3 SocketChannel (java.nio.channels.SocketChannel)3 FileDescriptor (java.io.FileDescriptor)2 Field (java.lang.reflect.Field)2 DatagramChannel (java.nio.channels.DatagramChannel)2 Selector (java.nio.channels.Selector)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 PrivilegedAction (java.security.PrivilegedAction)2 DefaultSelectStrategyFactory (io.netty.channel.DefaultSelectStrategyFactory)1 EventLoopTaskQueueFactory (io.netty.channel.EventLoopTaskQueueFactory)1 SelectStrategyFactory (io.netty.channel.SelectStrategyFactory)1 RejectedExecutionHandler (io.netty.util.concurrent.RejectedExecutionHandler)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 AbstractSelector (java.nio.channels.spi.AbstractSelector)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1