use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioConnection method closeConnection.
protected void closeConnection(SelectionKey key) {
if (key != null) {
SocketChannel channel = (SocketChannel) key.channel();
key.cancel();
try {
if (channel != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Closing socket " + channel.socket());
}
channel.close();
}
} catch (IOException ignore) {
}
}
}
use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioConnection method logTrace.
protected void logTrace(Exception e, SelectionKey key, int loc) {
if (s_logger.isTraceEnabled()) {
Socket socket = null;
if (key != null) {
SocketChannel ch = (SocketChannel) key.channel();
if (ch != null) {
socket = ch.socket();
}
}
s_logger.trace("Location " + loc + ": Socket " + socket + " closed on read. Probably -1 returned.");
}
}
use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioConnection method connect.
protected void connect(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
try {
socketChannel.finishConnect();
key.interestOps(SelectionKey.OP_READ);
Socket socket = socketChannel.socket();
if (!socket.getKeepAlive()) {
socket.setKeepAlive(true);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Connected to " + socket);
}
Link link = new Link((InetSocketAddress) socket.getRemoteSocketAddress(), this);
link.setKey(key);
key.attach(link);
Task task = _factory.create(Task.Type.CONNECT, link, null);
_executor.execute(task);
} catch (IOException e) {
logTrace(e, key, 2);
terminate(key);
}
}
use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.
the class NioConnection method logDebug.
protected void logDebug(Exception e, SelectionKey key, int loc) {
if (s_logger.isDebugEnabled()) {
Socket socket = null;
if (key != null) {
SocketChannel ch = (SocketChannel) key.channel();
if (ch != null) {
socket = ch.socket();
}
}
s_logger.debug("Location " + loc + ": Socket " + socket + " closed on read. Probably -1 returned: " + e.getMessage());
}
}
use of java.nio.channels.SocketChannel in project jeromq by zeromq.
the class TcpListener method acceptEvent.
@Override
public void acceptEvent() {
SocketChannel fd = null;
try {
fd = accept();
Utils.tuneTcpSocket(fd);
Utils.tuneTcpKeepalives(fd, options.tcpKeepAlive, options.tcpKeepAliveCnt, options.tcpKeepAliveIdle, options.tcpKeepAliveIntvl);
} catch (IOException e) {
// If connection was reset by the peer in the meantime, just ignore it.
// TODO: Handle specific errors like ENFILE/EMFILE etc.
socket.eventAcceptFailed(endpoint, ZError.exccode(e));
return;
}
// Create the engine object for this connection.
StreamEngine engine = null;
try {
engine = new StreamEngine(fd, options, endpoint);
} catch (ZError.InstantiationException e) {
socket.eventAcceptFailed(endpoint, ZError.EINVAL);
return;
}
// Choose I/O thread to run connecter in. Given that we are already
// running in an I/O thread, there must be at least one available.
IOThread ioThread = chooseIoThread(options.affinity);
// Create and launch a session object.
SessionBase session = SessionBase.create(ioThread, false, socket, options, new Address(fd.socket().getRemoteSocketAddress()));
session.incSeqnum();
launchChild(session);
sendAttach(session, engine, false);
socket.eventAccepted(endpoint, fd);
}
Aggregations