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);
}
use of java.nio.channels.SocketChannel in project http-kit by http-kit.
the class HttpClient method doWrite.
private void doWrite(SelectionKey key) {
Request req = (Request) key.attachment();
SocketChannel ch = (SocketChannel) key.channel();
try {
if (req instanceof HttpsRequest) {
HttpsRequest httpsReq = (HttpsRequest) req;
if (httpsReq.handshaken) {
// will flip to OP_READ
httpsReq.writeWrappedRequest();
} else {
buffer.clear();
if (httpsReq.doHandshake(buffer) < 0) {
// will be a No status exception
req.finish();
}
}
} else {
ByteBuffer[] buffers = req.request;
ch.write(buffers);
if (!buffers[buffers.length - 1].hasRemaining()) {
key.interestOps(OP_READ);
}
}
} catch (IOException e) {
if (!cleanAndRetryIfBroken(key, req)) {
req.finish(e);
}
} catch (Exception e) {
// rarely happen
req.finish(e);
}
}
Aggregations