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);
}
}
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();
}
}
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();
}
}
}
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);
}
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);
}
}
}
}
Aggregations