@Override public int available() throws IOException { return IoBridge.available(fd); }
@Override protected void bind(InetAddress address, int port) throws IOException { IoBridge.bind(fd, address, port); this.address = address; if (port != 0) { this.localport = port; } else { this.localport = IoBridge.getSocketLocalPort(fd); } }
return booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP)); case IoBridge.JAVA_IP_MULTICAST_TTL: return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_BROADCAST)); case SocketOptions.SO_KEEPALIVE: return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE)); case SocketOptions.SO_LINGER: StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER); return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE)); case SocketOptions.SO_RCVBUF: return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF); case SocketOptions.SO_REUSEADDR: return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR)); case SocketOptions.SO_SNDBUF: return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_SNDBUF); return (int) Libcore.os.getsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO).toMillis(); case SocketOptions.TCP_NODELAY: return booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_TCP, TCP_NODELAY)); default: throw new SocketException("Unknown socket option: " + option);
private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException { // TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106) FileDescriptor fd = IoBridge.socket(true); boolean reached = false; try { if (source != null) { IoBridge.bind(fd, source, 0); } IoBridge.connect(fd, destination, 7, timeout); reached = true; } catch (IOException e) { if (e.getCause() instanceof ErrnoException) { // "Connection refused" means the IP address was reachable. reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED); } } IoBridge.closeSocket(fd); return reached; }
/** * Connects socket 'fd' to 'inetAddress' on 'port', with a the given 'timeoutMs'. * Use timeoutMs == 0 for a blocking connect with no timeout. */ public static boolean connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException { try { return connectErrno(fd, inetAddress, port, timeoutMs); } catch (ErrnoException errnoException) { throw new ConnectException(connectDetail(inetAddress, port, timeoutMs, errnoException), errnoException); } catch (SocketException ex) { throw ex; // We don't want to doubly wrap these. } catch (SocketTimeoutException ex) { throw ex; // We don't want to doubly wrap these. } catch (IOException ex) { throw new SocketException(ex); } }
/** * Closes the socket. After this method is invoked, subsequent * read/write operations will fail. */ public void close() throws IOException { guard.close(); IoBridge.closeSocket(fd); }
/** * Returns the local address to which this socket is bound, * or {@code null} if this socket is closed. */ public InetAddress getLocalAddress() { try { return IoBridge.getSocketLocalAddress(impl.fd); } catch (SocketException ex) { return null; } }
/** * Connects socket 'fd' to 'inetAddress' on 'port', with no timeout. The lack of a timeout * means this method won't throw SocketTimeoutException. */ public static boolean connect(FileDescriptor fd, InetAddress inetAddress, int port) throws SocketException { try { return IoBridge.connect(fd, inetAddress, port, 0); } catch (SocketTimeoutException ex) { throw new AssertionError(ex); // Can't happen for a connect without a timeout. } }
Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_LOOP, booleanToInt((Boolean) value)); Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, booleanToInt((Boolean) value)); return; case IoBridge.JAVA_IP_MULTICAST_TTL: return; case SocketOptions.SO_BROADCAST: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_BROADCAST, booleanToInt((Boolean) value)); return; case SocketOptions.SO_KEEPALIVE: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE, booleanToInt((Boolean) value)); return; case SocketOptions.SO_LINGER: StructLinger linger = new StructLinger(booleanToInt(on), seconds); Libcore.os.setsockoptLinger(fd, SOL_SOCKET, SO_LINGER, linger); return; case SocketOptions.SO_OOBINLINE: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE, booleanToInt((Boolean) value)); return; case SocketOptions.SO_RCVBUF: return; case SocketOptions.SO_REUSEADDR: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR, booleanToInt((Boolean) value)); return; case SocketOptions.SO_SNDBUF: return;
String detail = connectDetail(inetAddress, port, timeoutMs, cause); if (cause.errno == ETIMEDOUT) { throw new SocketTimeoutException(detail, cause);
remainingTimeoutMs = (int) (finishTimeMs - System.currentTimeMillis()); if (remainingTimeoutMs <= 0) { throw new SocketTimeoutException(connectDetail(inetAddress, port, timeoutMs, null)); } while (!IoBridge.isConnected(fd, inetAddress, port, timeoutMs, remainingTimeoutMs)); IoUtils.setBlocking(fd, true); // 4. set the socket back to blocking. return true; // Or we'd have thrown.
newImpl.localport = IoBridge.getSocketLocalPort(newImpl.fd);
/** * Connects socket 'fd' to 'inetAddress' on 'port', with a the given 'timeoutMs'. * Use timeoutMs == 0 for a blocking connect with no timeout. */ public static boolean connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException { try { return connectErrno(fd, inetAddress, port, timeoutMs); } catch (ErrnoException errnoException) { throw new ConnectException(connectDetail(inetAddress, port, timeoutMs, errnoException), errnoException); } catch (SocketException ex) { throw ex; // We don't want to doubly wrap these. } catch (SocketTimeoutException ex) { throw ex; // We don't want to doubly wrap these. } catch (IOException ex) { throw new SocketException(ex); } }
@Override protected synchronized void implCloseSelectableChannel() throws IOException { connected = false; if (socket != null && !socket.isClosed()) { socket.close(); } else { IoBridge.closeSocket(fd); } }
@Override public InetAddress getLocalAddress() { try { return IoBridge.getSocketLocalAddress(channelImpl.fd); } catch (SocketException ex) { return null; } }
@Override public void connect(InetAddress inetAddr, int port) throws SocketException { IoBridge.connect(fd, inetAddr, port); // Throws on failure. try { connectedAddress = InetAddress.getByAddress(inetAddr.getAddress()); } catch (UnknownHostException e) { // this is never expected to happen as we should not have gotten // here if the address is not resolvable throw new SocketException("Host is unresolved: " + inetAddr.getHostName()); } connectedPort = port; isNativeConnected = true; }
Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_LOOP, booleanToInt((Boolean) value)); Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, booleanToInt((Boolean) value)); return; case IoBridge.JAVA_IP_MULTICAST_TTL: return; case SocketOptions.SO_BROADCAST: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_BROADCAST, booleanToInt((Boolean) value)); return; case SocketOptions.SO_KEEPALIVE: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE, booleanToInt((Boolean) value)); return; case SocketOptions.SO_LINGER: StructLinger linger = new StructLinger(booleanToInt(on), seconds); Libcore.os.setsockoptLinger(fd, SOL_SOCKET, SO_LINGER, linger); return; case SocketOptions.SO_OOBINLINE: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE, booleanToInt((Boolean) value)); return; case SocketOptions.SO_RCVBUF: return; case SocketOptions.SO_REUSEADDR: Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR, booleanToInt((Boolean) value)); return; case SocketOptions.SO_SNDBUF: return;
String detail = connectDetail(inetAddress, port, timeoutMs, cause); if (cause.errno == ETIMEDOUT) { throw new SocketTimeoutException(detail, cause);
@Override public void bind(int port, InetAddress address) throws SocketException { IoBridge.bind(fd, address, port); if (port != 0) { localPort = port; } else { localPort = IoBridge.getSocketLocalPort(fd); } try { setOption(SocketOptions.SO_BROADCAST, Boolean.TRUE); } catch (IOException ignored) { } }
/** * Connects socket 'fd' to 'inetAddress' on 'port', with a the given 'timeoutMs'. * Use timeoutMs == 0 for a blocking connect with no timeout. */ public static boolean connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException { try { return connectErrno(fd, inetAddress, port, timeoutMs); } catch (ErrnoException errnoException) { throw new ConnectException(connectDetail(inetAddress, port, timeoutMs, errnoException), errnoException); } catch (SocketException ex) { throw ex; // We don't want to doubly wrap these. } catch (SocketTimeoutException ex) { throw ex; // We don't want to doubly wrap these. } catch (IOException ex) { throw new SocketException(ex); } }