public final boolean connect(SocketAddress socketAddress) throws IOException { int res; if (socketAddress instanceof InetSocketAddress) { InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; NativeInetAddress address = NativeInetAddress.newInstance(inetSocketAddress.getAddress()); res = connect(fd, address.address, address.scopeId, inetSocketAddress.getPort()); } else if (socketAddress instanceof DomainSocketAddress) { DomainSocketAddress unixDomainSocketAddress = (DomainSocketAddress) socketAddress; res = connectDomainSocket(fd, unixDomainSocketAddress.path().getBytes(CharsetUtil.UTF_8)); } else { throw new Error("Unexpected SocketAddress implementation " + socketAddress); } if (res < 0) { if (res == ERRNO_EINPROGRESS_NEGATIVE) { // connect not complete yet need to wait for EPOLLOUT event return false; } throwConnectException("connect", CONNECT_REFUSED_EXCEPTION, res); } return true; }
public final int getReceiveBufferSize() throws IOException { return getReceiveBufferSize(fd); }
public final int getSendBufferSize() throws IOException { return getSendBufferSize(fd); }
public static void initialize() { if (INITIALIZED.compareAndSet(false, true)) { initialize(NetUtil.isIpV4StackPreferred()); } }
@Test public void testKeepAlive() throws Exception { assertFalse(socket.isKeepAlive()); socket.setKeepAlive(true); assertTrue(socket.isKeepAlive()); }
@Test public void testTrafficClass() throws IOException { // IPTOS_THROUGHPUT final int value = 0x08; socket.setTrafficClass(value); assertEquals(value, socket.getTrafficClass()); } }
@Test public void testReceivedBufferSize() throws Exception { int size = socket.getReceiveBufferSize(); int newSize = 65535; assertTrue(size > 0); socket.setReceiveBufferSize(newSize); // Linux usually set it to double what is specified assertTrue(newSize <= socket.getReceiveBufferSize()); }
@Test public void testTcpNoDelay() throws Exception { assertFalse(socket.isTcpNoDelay()); socket.setTcpNoDelay(true); assertTrue(socket.isTcpNoDelay()); }
@Test public void testSendBufferSize() throws Exception { int size = socket.getSendBufferSize(); int newSize = 65535; assertTrue(size > 0); socket.setSendBufferSize(newSize); // Linux usually set it to double what is specified assertTrue(newSize <= socket.getSendBufferSize()); }
@Test public void testSoLinger() throws Exception { assertEquals(-1, socket.getSoLinger()); socket.setSoLinger(10); assertEquals(10, socket.getSoLinger()); }
public final int getSoLinger() throws IOException { return getSoLinger(fd); }
public final int getTrafficClass() throws IOException { return getTrafficClass(fd); }
public final void setKeepAlive(boolean keepAlive) throws IOException { setKeepAlive(fd, keepAlive ? 1 : 0); }
public final void setReceiveBufferSize(int receiveBufferSize) throws IOException { setReceiveBufferSize(fd, receiveBufferSize); }
public final boolean isTcpNoDelay() throws IOException { return isTcpNoDelay(fd) != 0; }
public final boolean isKeepAlive() throws IOException { return isKeepAlive(fd) != 0; }
private void sendCancelRequest() { try { if (serverAddress instanceof InetSocketAddress) { InetSocketAddress target = (InetSocketAddress) serverAddress; try (Socket abortSocket = new Socket(target.getAddress(), target.getPort())) { writeCancelRequest(new DataOutputStream(abortSocket.getOutputStream())); } } else if (serverAddress instanceof DomainSocketAddress) { io.netty.channel.unix.Socket unixSocket = io.netty.channel.unix.Socket.newSocketDomain(); try { unixSocket.connect(serverAddress); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { writeCancelRequest(new DataOutputStream(os)); byte[] request = os.toByteArray(); ByteBuffer buffer = ByteBuffer.allocateDirect(request.length); buffer.put(request, 0, request.length); buffer.flip(); unixSocket.write(buffer, 0, buffer.limit()); } } finally { unixSocket.close(); } } } catch (IOException ignored) { // All exceptions during a cancellation attempt are ignored... } }
public static void initialize() { if (INITIALIZED.compareAndSet(false, true)) { initialize(NetUtil.isIpV4StackPreferred()); } }
public final int getSoLinger() throws IOException { return getSoLinger(fd); }
public final int getTrafficClass() throws IOException { return getTrafficClass(fd); }