Search in sources :

Example 11 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project heron by twitter.

the class HeronServerTest method testClose.

/**
   * Method: stop()
   */
@Test
public void testClose() throws Exception {
    runBase();
    heronServer.stop();
    Map<SocketChannel, SocketChannelHelper> activeConnections = heronServer.getActiveConnections();
    ServerSocketChannel acceptChannel = heronServer.getAcceptChannel();
    Assert.assertNotNull(acceptChannel);
    Assert.assertTrue(!acceptChannel.isOpen());
    Assert.assertNotNull(activeConnections);
    Assert.assertEquals(0, activeConnections.size());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test)

Example 12 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project robovm by robovm.

the class AbstractSelectorTest method test_register_LSelectorI.

/**
     * @tests AbstractSelector#register(Selector,int)
     */
public void test_register_LSelectorI() throws Exception {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.configureBlocking(false);
    assertFalse(ssc.isRegistered());
    SelectionKey acceptKey = ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);
    assertTrue(ssc.isRegistered());
    assertNotNull(acceptKey);
    assertTrue(acceptSelector.keys().contains(acceptKey));
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Example 13 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project robovm by robovm.

the class SocketTest method checkSocketLocalAndRemoteAddresses.

public void checkSocketLocalAndRemoteAddresses(boolean setOptions) throws Exception {
    InetAddress host = InetAddress.getLocalHost();
    // Open a local server port.
    ServerSocketChannel ssc = ServerSocketChannel.open();
    InetSocketAddress listenAddr = new InetSocketAddress(host, 0);
    ssc.socket().bind(listenAddr, 0);
    ServerSocket ss = ssc.socket();
    // Open a socket to the local port.
    SocketChannel out = SocketChannel.open();
    out.configureBlocking(false);
    if (setOptions) {
        out.socket().setTcpNoDelay(false);
    }
    InetSocketAddress addr = new InetSocketAddress(host, ssc.socket().getLocalPort());
    out.connect(addr);
    while (!out.finishConnect()) {
        Thread.sleep(1);
    }
    SocketChannel in = ssc.accept();
    if (setOptions) {
        in.socket().setTcpNoDelay(false);
    }
    InetSocketAddress outRemoteAddress = (InetSocketAddress) out.socket().getRemoteSocketAddress();
    InetSocketAddress outLocalAddress = (InetSocketAddress) out.socket().getLocalSocketAddress();
    InetSocketAddress inLocalAddress = (InetSocketAddress) in.socket().getLocalSocketAddress();
    InetSocketAddress inRemoteAddress = (InetSocketAddress) in.socket().getRemoteSocketAddress();
    System.err.println("inLocalAddress: " + inLocalAddress);
    System.err.println("inRemoteAddress: " + inRemoteAddress);
    System.err.println("outLocalAddress: " + outLocalAddress);
    System.err.println("outRemoteAddress: " + outRemoteAddress);
    assertEquals(outRemoteAddress.getPort(), ss.getLocalPort());
    assertEquals(inLocalAddress.getPort(), ss.getLocalPort());
    assertEquals(inRemoteAddress.getPort(), outLocalAddress.getPort());
    assertEquals(inLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(inRemoteAddress.getAddress(), ss.getInetAddress());
    assertEquals(outLocalAddress.getAddress(), ss.getInetAddress());
    assertEquals(outRemoteAddress.getAddress(), ss.getInetAddress());
    in.close();
    out.close();
    ssc.close();
    assertNull(in.socket().getRemoteSocketAddress());
    assertNull(out.socket().getRemoteSocketAddress());
    assertEquals(in.socket().getLocalSocketAddress(), ss.getLocalSocketAddress());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 14 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project j2objc by google.

the class ServerSocketChannelTest method testNonBlockingAccept.

// http://code.google.com/p/android/issues/detail?id=16579
public void testNonBlockingAccept() throws Exception {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    try {
        ssc.configureBlocking(false);
        ssc.socket().bind(null);
        // Should return immediately, since we're non-blocking.
        assertNull(ssc.accept());
    } finally {
        ssc.close();
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 15 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project h2o-3 by h2oai.

the class TCPReceiverThread method run.

// The Run Method.
// Started by main() on a single thread, this code manages reading TCP requests
@SuppressWarnings("resource")
public void run() {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    ServerSocketChannel errsock = null;
    boolean saw_error = false;
    while (true) {
        try {
            // Cleanup from any prior socket failures.  Rare unless we're really sick.
            if (errsock != null) {
                // One time attempt a socket close
                final ServerSocketChannel tmp2 = errsock;
                errsock = null;
                // Could throw, but errsock cleared for next pass
                tmp2.close();
            }
            // prevent deny-of-service endless socket-creates
            if (saw_error)
                Thread.sleep(100);
            saw_error = false;
            // More common-case setup of a ServerSocket
            if (SOCK == null) {
                SOCK = ServerSocketChannel.open();
                SOCK.socket().setReceiveBufferSize(AutoBuffer.BBP_BIG._size);
                SOCK.socket().bind(H2O.SELF._key);
            }
            // Block for TCP connection and setup to read from it.
            SocketChannel sock = SOCK.accept();
            ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
            ByteChannel wrappedSocket = socketChannelFactory.serverChannel(sock);
            bb.limit(bb.capacity());
            bb.position(0);
            while (bb.hasRemaining()) {
                // read first 8 bytes
                wrappedSocket.read(bb);
            }
            bb.flip();
            // 1 - small , 2 - big
            int chanType = bb.get();
            int port = bb.getChar();
            int sentinel = (0xFF) & bb.get();
            if (sentinel != 0xef) {
                if (H2O.SELF.getSecurityManager().securityEnabled) {
                    throw new IOException("Missing EOM sentinel when opening new SSL tcp channel.");
                } else {
                    throw H2O.fail("missing eom sentinel when opening new tcp channel");
                }
            }
            // todo compare against current cloud, refuse the con if no match
            // Do H2O.Intern in corresponding case branch, we can't do H2O.intern here since it wouldn't work
            // with ExternalFrameHandling ( we don't send the same information there as with the other communication)
            InetAddress inetAddress = sock.socket().getInetAddress();
            // Pass off the TCP connection to a separate reader thread
            switch(chanType) {
                case TCP_SMALL:
                    H2ONode h2o = H2ONode.intern(inetAddress, port);
                    new UDP_TCP_ReaderThread(h2o, wrappedSocket).start();
                    break;
                case TCP_BIG:
                    new TCPReaderThread(wrappedSocket, new AutoBuffer(wrappedSocket, inetAddress), inetAddress).start();
                    break;
                case TCP_EXTERNAL:
                    new ExternalFrameHandlerThread(wrappedSocket, new AutoBuffer(wrappedSocket, null)).start();
                    break;
                default:
                    throw H2O.fail("unexpected channel type " + chanType + ", only know 1 - Small, 2 - Big and 3 - ExternalFrameHandling");
            }
        } catch (java.nio.channels.AsynchronousCloseException ex) {
            // Socket closed for shutdown
            break;
        } catch (Exception e) {
            e.printStackTrace();
            // On any error from anybody, close all sockets & re-open
            Log.err("IO error on TCP port " + H2O.H2O_PORT + ": ", e);
            saw_error = true;
            // Signal error recovery on the next loop
            errsock = SOCK;
            // Signal error recovery on the next loop
            SOCK = null;
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ByteChannel(java.nio.channels.ByteChannel) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)274 SocketChannel (java.nio.channels.SocketChannel)156 InetSocketAddress (java.net.InetSocketAddress)128 IOException (java.io.IOException)96 ByteBuffer (java.nio.ByteBuffer)53 SelectionKey (java.nio.channels.SelectionKey)41 ServerSocket (java.net.ServerSocket)37 Test (org.junit.Test)27 Socket (java.net.Socket)24 ClosedChannelException (java.nio.channels.ClosedChannelException)22 Selector (java.nio.channels.Selector)22 ClosedSelectorException (java.nio.channels.ClosedSelectorException)15 InetAddress (java.net.InetAddress)14 CancelledKeyException (java.nio.channels.CancelledKeyException)11 EOFException (java.io.EOFException)8 SocketException (java.net.SocketException)8 BindException (java.net.BindException)7 SocketAddress (java.net.SocketAddress)7 Test (org.junit.jupiter.api.Test)7 OutgoingPacket (com.twitter.heron.common.network.OutgoingPacket)6