@Override protected void doClose() throws Exception { try { super.doClose(); } finally { DomainSocketAddress local = this.local; if (local != null) { // Delete the socket file if possible. File socketFile = new File(local.path()); boolean success = socketFile.delete(); if (!success && logger.isDebugEnabled()) { logger.debug("Failed to delete a domain socket file: {}", local.path()); } } } }
public static boolean connect(int fd, SocketAddress socketAddress) throws IOException { int res; if (socketAddress instanceof InetSocketAddress) { InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; NativeInetAddress address = toNativeInetAddress(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()); } 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; } throw newIOException("connect", res); } return true; }
@Override public ManagedChannel forDescriptor(ApiServiceDescriptor apiServiceDescriptor) { SocketAddress address = SocketAddressFactory.createFrom(apiServiceDescriptor.getUrl()); return NettyChannelBuilder.forAddress(address) .channelType(address instanceof DomainSocketAddress ? EpollDomainSocketChannel.class : EpollSocketChannel.class) .eventLoopGroup(new EpollEventLoopGroup()) .usePlaintext(true) // Set the message size to max value here. The actual size is governed by the // buffer size in the layers above. .maxInboundMessageSize(Integer.MAX_VALUE) .build(); }
/** * Parse a {@link SocketAddress} from the given string. */ public static SocketAddress createFrom(String value) { if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) { // Unix Domain Socket address. // Create the underlying file for the Unix Domain Socket. String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length()); File file = new File(filePath); if (!file.isAbsolute()) { throw new IllegalArgumentException("File path must be absolute: " + filePath); } try { if (file.createNewFile()) { // If this application created the file, delete it when the application exits. file.deleteOnExit(); } } catch (IOException ex) { throw new RuntimeException(ex); } // Create the SocketAddress referencing the file. return new DomainSocketAddress(file); } else { // Standard TCP/IP address. HostAndPort hostAndPort = HostAndPort.fromString(value); checkArgument(hostAndPort.hasPort(), "Address must be a unix:// path or be in the form host:port. Got: %s", value); return new InetSocketAddress(hostAndPort.getHostText(), hostAndPort.getPort()); } }
@Test public void testDomainSocket() throws Exception { File tmpFile = tmpFolder.newFile(); SocketAddress socketAddress = SocketAddressFactory.createFrom( "unix://" + tmpFile.getAbsolutePath()); assertThat(socketAddress, Matchers.instanceOf(DomainSocketAddress.class)); assertEquals(tmpFile.getAbsolutePath(), ((DomainSocketAddress) socketAddress).path()); }
@Override protected void doBind(SocketAddress localAddress) throws Exception { int fd = fd().intValue(); Native.bind(fd, localAddress); Native.listen(fd, config.getBacklog()); local = (DomainSocketAddress) localAddress; }
@Override protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { if (super.doConnect(remoteAddress, localAddress)) { local = (DomainSocketAddress) localAddress; remote = (DomainSocketAddress) remoteAddress; return true; } return false; }
public static DomainSocketAddress newSocketAddress() { try { File file = File.createTempFile("netty", "dsocket"); file.delete(); return new DomainSocketAddress(file); } catch (IOException e) { throw new IllegalStateException(e); } }
@Test public void socketToProto_unix() throws Exception { String path = "/some/path"; DomainSocketAddress socketAddress = new DomainSocketAddress(path); assertEquals( Peer .newBuilder() .setPeerType(Peer.PeerType.PEER_UNIX) .setPeer(ByteString.copyFrom(path.getBytes(US_ASCII))) .build(), BinaryLog.socketToProto(socketAddress) ); }
/** * Parse a {@link SocketAddress} from the given string. */ public static SocketAddress parseSocketAddress(String value) { if (value.startsWith(UNIX_DOMAIN_SOCKET_PREFIX)) { // Unix Domain Socket address. // Create the underlying file for the Unix Domain Socket. String filePath = value.substring(UNIX_DOMAIN_SOCKET_PREFIX.length()); File file = new File(filePath); if (!file.isAbsolute()) { throw new IllegalArgumentException("File path must be absolute: " + filePath); } try { if (file.createNewFile()) { // If this application created the file, delete it when the application exits. file.deleteOnExit(); } } catch (IOException ex) { throw new RuntimeException(ex); } // Create the SocketAddress referencing the file. return new DomainSocketAddress(file); } else { // Standard TCP/IP address. String[] parts = value.split(":", 2); if (parts.length < 2) { throw new IllegalArgumentException( "Address must be a unix:// path or be in the form host:port. Got: " + value); } String host = parts[0]; int port = Integer.parseInt(parts[1]); return new InetSocketAddress(host, port); } }
ChannelFuture connect() { return bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")); }
@Override protected DomainSocketAddress localAddress0() { return local; }
@Override public DomainSocketAddress remoteAddress() { return (DomainSocketAddress) super.remoteAddress(); }
@Override public DomainSocketAddress localAddress() { return (DomainSocketAddress) super.localAddress(); }
@Override protected DomainSocketAddress remoteAddress0() { return remote; }
@Override protected void doBind(SocketAddress localAddress) throws Exception { Native.bind(fd().intValue(), localAddress); local = (DomainSocketAddress) localAddress; }
@Override public SocketAddress newSocketAddress(String socketPath) { checkForEpollLibrary(); return new DomainSocketAddress(socketPath); }
@Override public SocketAddress newSocketAddress(String socketPath) { checkForKqueueLibrary(); return new DomainSocketAddress(socketPath); }
@Override public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException { return (DuplexChannel) bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")).sync().channel(); }