本文整理汇总了Java中java.nio.channels.SelectableChannel类的典型用法代码示例。如果您正苦于以下问题:Java SelectableChannel类的具体用法?Java SelectableChannel怎么用?Java SelectableChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SelectableChannel类属于java.nio.channels包,在下文中一共展示了SelectableChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: acceptCallback
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
public void acceptCallback(SelectableChannel channel) {
// accept the connection
assert channel == serverSocket;
SocketChannel client;
try {
client = serverSocket.accept();
} catch (IOException e) {
throw new RuntimeException(e);
}
assert client != null;
// wrap it in a message connection and register with event loop
NIOMessageConnection connection = new NIOMessageConnection(client);
connection.setBigEndian();
this.eventLoop.registerRead(client, new ClientConnectionHandler(connection));
this.numConnections.incrementAndGet();
}
示例2: acceptCallback
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void acceptCallback(SelectableChannel channel) {
// accept the connection
assert channel == serverSocket;
SocketChannel client;
try {
client = serverSocket.accept();
} catch (IOException e) {
throw new RuntimeException(e);
}
assert client != null;
// wrap it in a message connection and register with event loop
ProtoConnection connection = new ProtoConnection(new NonBlockingConnection(client));
eventLoop.registerRead(client, new EventCallbackWrapper(connection));
// SelectionKey clientKey = connection.register(selector);
// clientKey.attach(connection);
// eventQueue.add(new Event(connection, null));
}
示例3: addInterest
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void addInterest(SelectableChannel channel, int operation, Handler callback) {
// TODO: Support multiple handlers?
SelectionKey key = channel.keyFor(selector);
if (key != null) {
assert (key.interestOps() & operation) == 0;
if (key.attachment() == null) {
key.attach(callback);
} else {
assert callback == key.attachment();
}
key.interestOps(key.interestOps() | operation);
// TODO: This fixes a synchronization issue where one thread changes the interest set
// of a thread while another thread is blocked in select(), because the Selector
// documentation states that it waits for events registered "as of the moment that the
// selection operation began. Is there a better fix?
selector.wakeup();
} else {
register(channel, operation, callback);
}
}
示例4: timeoutExceptionString
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private static String timeoutExceptionString(SelectableChannel channel,
long timeout, int ops) {
String waitingFor;
switch(ops) {
case SelectionKey.OP_READ :
waitingFor = "read"; break;
case SelectionKey.OP_WRITE :
waitingFor = "write"; break;
case SelectionKey.OP_CONNECT :
waitingFor = "connect"; break;
default :
waitingFor = "" + ops;
}
return timeout + " millis timeout while " +
"waiting for channel to be ready for " +
waitingFor + ". ch : " + channel;
}
示例5: registerNewSelector
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
/**
* In the case we are using the java select() method, this method is used to
* trash the buggy selector and create a new one, registering all the
* sockets on it.
*/
@Override
protected void registerNewSelector() throws IOException {
synchronized (selector) {
Set<SelectionKey> keys = selector.keys();
// Open a new selector
Selector newSelector = Selector.open();
// Loop on all the registered keys, and register them on the new selector
for (SelectionKey key : keys) {
SelectableChannel ch = key.channel();
// Don't forget to attache the session, and back !
NioSession session = (NioSession) key.attachment();
SelectionKey newKey = ch.register(newSelector, key.interestOps(), session);
session.setSelectionKey(newKey);
}
// Now we can close the old selector and switch it
selector.close();
selector = newSelector;
}
}
示例6: handleIOException
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void handleIOException( SelectionKey key, WebSocket conn, IOException ex ) {
// onWebsocketError( conn, ex );// conn may be null here
if( conn != null ) {
conn.closeConnection( CloseFrame.ABNORMAL_CLOSE, ex.getMessage() );
} else if( key != null ) {
SelectableChannel channel = key.channel();
if( channel != null && channel.isOpen() ) { // this could be the case if the IOException ex is a SSLException
try {
channel.close();
} catch ( IOException e ) {
// there is nothing that must be done here
}
if( WebSocketImpl.DEBUG )
System.out.println( "Connection closed because of" + ex );
}
}
}
示例7: handleIOException
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void handleIOException(SelectionKey key, WebSocket conn, IOException ex) {
// onWebsocketError( conn, ex );// conn may be null here
if (conn != null) {
conn.closeConnection(CloseFrame.ABNORMAL_CLOSE, ex.getMessage());
} else if (key != null) {
SelectableChannel channel = key.channel();
if (channel != null && channel.isOpen()) { // this could be the case if the IOException ex is a SSLException
try {
channel.close();
} catch (IOException e) {
// there is nothing that must be done here
}
if (WebSocketImpl.DEBUG) {
System.out.println("Connection closed because of" + ex);
}
}
}
}
示例8: readCallback
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void readCallback(SelectableChannel channel) {
try {
read(this);
} catch (RuntimeException ex) {
if (ex.getCause() instanceof IOException) {
// Ignore this
if (LOG.isDebugEnabled()) LOG.warn("Client connection closed unexpectedly", ex);
} else {
throw ex;
}
}
}
示例9: registerRead
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void registerRead(SelectableChannel channel, Handler handler) {
// Disallow both being registered for read events and connection events at the same time.
// On Linux, when a connect fails, the socket is ready for both events, which causes
// errors when reads are attempted on the closed socket.
assert channel.keyFor(selector) == null ||
(channel.keyFor(selector).interestOps() & SelectionKey.OP_CONNECT) == 0;
addInterest(channel, SelectionKey.OP_READ, handler);
}
示例10: get
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
/**
* Takes one selector from end of LRU list of free selectors.
* If there are no selectors awailable, it creates a new selector.
* Also invokes trimIdleSelectors().
*
* @param channel
* @return
* @throws IOException
*/
private synchronized SelectorInfo get(SelectableChannel channel)
throws IOException {
SelectorInfo selInfo = null;
SelectorProvider provider = channel.provider();
// pick the list : rarely there is more than one provider in use.
ProviderInfo pList = providerList;
while (pList != null && pList.provider != provider) {
pList = pList.next;
}
if (pList == null) {
//LOG.info("Creating new ProviderInfo : " + provider.toString());
pList = new ProviderInfo();
pList.provider = provider;
pList.queue = new LinkedList<SelectorInfo>();
pList.next = providerList;
providerList = pList;
}
LinkedList<SelectorInfo> queue = pList.queue;
if (queue.isEmpty()) {
Selector selector = provider.openSelector();
selInfo = new SelectorInfo();
selInfo.selector = selector;
selInfo.queue = queue;
} else {
selInfo = queue.removeLast();
}
trimIdleSelectors(Time.now());
return selInfo;
}
示例11: readCallback
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void readCallback(SelectableChannel channel) {
boolean isOpen = connection.readAllAvailable();
if (!isOpen) {
// TODO: Fail any subsequent RPCs
throw new UnsupportedOperationException("Connection closed: not handled (for now).");
}
while (true) {
RpcResponse.Builder builder = RpcResponse.newBuilder();
boolean success = connection.readBufferedMessage(builder);
if (!success) {
// TODO: Cache the builder object to reduce garbage?
break;
}
// Set the appropriate flags on the RPC object
// TODO: Handle bad sequence number by ignoring/logging?
RpcResponse response = builder.build();
ProtoRpcController rpc = null;
synchronized (this) {
rpc = pendingRpcs.remove(response.getSequenceNumber());
assert response.getStatus() == Protocol.Status.OK;
assert rpc != null :
"No ProtoRpcController for Sequence# " + response.getSequenceNumber();
}
rpc.finishRpcSuccess(response.getResponse());
}
}
示例12: acceptCallback
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void acceptCallback(SelectableChannel channel) {
// accept the connection
assert client == null;
try {
client = ((ServerSocketChannel) channel).accept();
} catch (IOException e) {
throw new RuntimeException(e);
}
assert client != null;
}
示例13: registerWrite
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
@Override
public void registerWrite(SelectableChannel channel, Handler handler) {
if (writeHandler != null) {
throw new IllegalStateException("Each channel can only call registerWrite() once");
}
writeHandler = handler;
}
示例14: implClose
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
protected void implClose() throws IOException {
synchronized (closeLock) {
if (channelArray != null) {
if (pollWrapper != null) {
// prevent further wakeup
synchronized (interruptLock) {
interruptTriggered = true;
}
wakeupPipe.sink().close();
wakeupPipe.source().close();
for(int i = 1; i < totalChannels; i++) { // Deregister channels
if (i % MAX_SELECTABLE_FDS != 0) { // skip wakeupEvent
deregister(channelArray[i]);
SelectableChannel selch = channelArray[i].channel();
if (!selch.isOpen() && !selch.isRegistered())
((SelChImpl)selch).kill();
}
}
pollWrapper.free();
pollWrapper = null;
selectedKeys = null;
channelArray = null;
// Make all remaining helper threads exit
for (SelectThread t: threads)
t.makeZombie();
startLock.startThreads();
}
}
}
}
示例15: unregisterChannel
import java.nio.channels.SelectableChannel; //导入依赖的package包/类
private void unregisterChannel(SelectableChannel channel) {
SelectionKey key = channel.keyFor(selector);
if (key != null) {
// not registered with this selector => null returned
key.cancel();
}
}