hprose.net.Acceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hprose-java Show documentation
Show all versions of hprose-java Show documentation
Hprose is a High Performance Remote Object Service Engine.
It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system.
Hprose supports many programming languages, for example:
* AAuto Quicker
* ActionScript
* ASP
* C++
* Dart
* Delphi/Free Pascal
* dotNET(C#, Visual Basic...)
* Golang
* Java
* JavaScript
* Node.js
* Objective-C
* Perl
* PHP
* Python
* Ruby
* ...
Through Hprose, You can conveniently and efficiently intercommunicate between those programming languages.
This project is the implementation of Hprose for Java.
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* Acceptor.java *
* *
* hprose Acceptor class for Java. *
* *
* LastModified: Sep 19, 2016 *
* Author: Ma Bingyao *
* *
\**********************************************************/
package hprose.net;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public final class Acceptor extends Thread {
private final Selector selector;
private final ServerSocketChannel serverChannel;
private final ReactorGroup reactor;
private final ConnectionHandler handler;
public Acceptor(String host, int port, ConnectionHandler handler, int reactorThreads) throws IOException {
selector = Selector.open();
serverChannel = ServerSocketChannel.open();
ServerSocket serverSocket = serverChannel.socket();
InetSocketAddress address = (host == null) ?
new InetSocketAddress(port) :
new InetSocketAddress(host, port);
serverSocket.bind(address);
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
reactor = new ReactorGroup(reactorThreads);
this.handler = handler;
}
@Override
public void run() {
reactor.start();
try {
while (!isInterrupted()) {
try {
process();
}
catch (IOException e) {
handler.onError(null, e);
}
}
}
catch (ClosedSelectorException e) {}
reactor.close();
}
private void process() throws IOException {
int n = selector.select();
if (n == 0) return;
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
it.remove();
if (key.isAcceptable()) {
accept(key);
}
}
}
private void accept(SelectionKey key) throws IOException {
final SocketChannel channel = ((ServerSocketChannel) key.channel()).accept();
if (channel != null) {
channel.configureBlocking(false);
channel.socket().setReuseAddress(true);
channel.socket().setKeepAlive(true);
reactor.register(new Connection(channel, handler, null));
}
}
public void close() {
try {
selector.close();
}
catch (IOException e) {}
try {
serverChannel.close();
}
catch (IOException e) {}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy