Search in sources :

Example 56 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class HttpServer method accept.

void accept(SelectionKey key) {
    ServerSocketChannel ch = (ServerSocketChannel) key.channel();
    SocketChannel s;
    try {
        while ((s = ch.accept()) != null) {
            s.configureBlocking(false);
            HttpAtta atta = new HttpAtta(maxBody, maxLine, proxyProtocolOption);
            SelectionKey k = s.register(selector, OP_READ, atta);
            atta.channel = new AsyncChannel(k, this);
        }
    } catch (Exception e) {
        // eg: too many open files. do not quit
        HttpUtils.printError("accept incoming request", e);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) LineTooLargeException(org.httpkit.LineTooLargeException) ProtocolException(org.httpkit.ProtocolException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) RequestTooLargeException(org.httpkit.RequestTooLargeException)

Example 57 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class ConcurrencyBench method finishConnect.

private static void finishConnect(SelectionKey key) {
    SocketChannel ch = (SocketChannel) key.channel();
    try {
        if (ch.finishConnect()) {
            ++connected;
            key.interestOps(SelectionKey.OP_WRITE);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) IOException(java.io.IOException)

Example 58 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class MakeupIdelConnection method main.

public static void main(String[] args) throws IOException {
    final Selector selector = Selector.open();
    InetSocketAddress[] locals = { new InetSocketAddress("127.0.0.1", 4348) };
    // InetSocketAddress remote = new InetSocketAddress("192.168.1.114",
    // 9090);
    long start = System.currentTimeMillis();
    int connected = 0;
    int currentConnectionPerIP = 0;
    while (true) {
        if (System.currentTimeMillis() - start > 1000 * 60 * 10) {
            break;
        }
        for (int i = 0; i < connectionPerIP / STEPS && currentConnectionPerIP < connectionPerIP; ++i, ++currentConnectionPerIP) {
            for (InetSocketAddress addr : locals) {
                SocketChannel ch = SocketChannel.open();
                ch.configureBlocking(false);
                Socket s = ch.socket();
                s.setReuseAddress(true);
                // s.bind(addr);
                ch.register(selector, SelectionKey.OP_CONNECT);
                ch.connect(addr);
            }
        }
        // 10s
        int select = selector.select(1000 * 10);
        if (select > 0) {
            System.out.println("select return: " + select + " events ; current connection per ip: " + currentConnectionPerIP);
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> it = selectedKeys.iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                if (key.isConnectable()) {
                    SocketChannel ch = (SocketChannel) key.channel();
                    if (ch.finishConnect()) {
                        ++connected;
                        if (connected % (connectionPerIP * locals.length / 10) == 0) {
                            System.out.println("connected: " + connected);
                        }
                        key.interestOps(SelectionKey.OP_READ);
                    }
                }
            }
            selectedKeys.clear();
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) Selector(java.nio.channels.Selector)

Example 59 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class PerformanceBench method connect.

private static void connect(InetSocketAddress addr, Selector selector) throws IOException, SocketException, ClosedChannelException {
    SocketChannel ch = SocketChannel.open();
    ch.socket().setReuseAddress(true);
    ch.configureBlocking(false);
    ch.register(selector, SelectionKey.OP_CONNECT, SelectAttachment.next());
    ch.connect(addr);
}
Also used : SocketChannel(java.nio.channels.SocketChannel)

Example 60 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class PerformanceBench method doRead.

private static void doRead(Selector selector, SelectionKey key) throws IOException, SocketException, ClosedChannelException {
    SocketChannel ch = (SocketChannel) key.channel();
    SelectAttachment att = (SelectAttachment) key.attachment();
    readBuffer.clear();
    while (true) {
        int read = ch.read(readBuffer);
        if (read == -1) {
            ch.close();
            decAndPrint();
            D("remote closed, connecton new, remaining: " + remaining);
            connect(addr, selector);
            break;
        } else if (read == 0) {
            D("read zero");
            break;
        } else {
            totalByteReceive += read;
            if (att.response_length == -1) {
                readBuffer.flip();
                String line = readLine(readBuffer);
                while (line.length() > 0) {
                    line = line.toLowerCase();
                    if (line.startsWith(CL)) {
                        String length = line.substring(CL.length());
                        att.response_length = Integer.valueOf(length);
                        att.response_cnt = att.response_length;
                    }
                    line = readLine(readBuffer);
                }
                att.response_cnt -= readBuffer.remaining();
            } else {
                att.response_cnt -= read;
            }
            if (att.response_cnt == 0) {
                D("read all");
                decAndPrint();
                key.attach(SelectAttachment.next());
                key.interestOps(SelectionKey.OP_WRITE);
            }
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel)

Aggregations

SocketChannel (java.nio.channels.SocketChannel)662 IOException (java.io.IOException)298 ServerSocketChannel (java.nio.channels.ServerSocketChannel)285 InetSocketAddress (java.net.InetSocketAddress)202 ByteBuffer (java.nio.ByteBuffer)163 SelectionKey (java.nio.channels.SelectionKey)105 Socket (java.net.Socket)87 Test (org.junit.Test)81 ClosedChannelException (java.nio.channels.ClosedChannelException)50 Selector (java.nio.channels.Selector)42 SocketAddress (java.net.SocketAddress)35 ServerSocket (java.net.ServerSocket)31 ClosedSelectorException (java.nio.channels.ClosedSelectorException)28 CancelledKeyException (java.nio.channels.CancelledKeyException)27 ConnectException (java.net.ConnectException)26 ArrayList (java.util.ArrayList)26 SocketTimeoutException (java.net.SocketTimeoutException)23 SelectableChannel (java.nio.channels.SelectableChannel)21 SocketException (java.net.SocketException)20 HashMap (java.util.HashMap)20