| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.SuppressLint; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.system.ErrnoException; |
| 26 |
import android.system.Os; |
| 27 |
|
| 28 |
import java.io.Closeable; |
| 29 |
import java.io.FileDescriptor; |
| 30 |
import java.io.IOException; |
| 31 |
import java.io.InputStream; |
| 32 |
import java.io.OutputStream; |
| 33 |
import java.net.SocketOptions; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class LocalSocket implements Closeable { |
| 41 |
|
| 42 |
@UnsupportedAppUsage |
| 43 |
private final LocalSocketImpl impl; |
| 44 |
|
| 45 |
private volatile boolean implCreated; |
| 46 |
private LocalSocketAddress localAddress; |
| 47 |
private boolean isBound; |
| 48 |
private boolean isConnected; |
| 49 |
private final int sockType; |
| 50 |
|
| 51 |
|
| 52 |
static final int SOCKET_UNKNOWN = 0; |
| 53 |
|
| 54 |
public static final int SOCKET_DGRAM = 1; |
| 55 |
|
| 56 |
public static final int SOCKET_STREAM = 2; |
| 57 |
|
| 58 |
public static final int SOCKET_SEQPACKET = 3; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public LocalSocket() { |
| 64 |
this(SOCKET_STREAM); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public LocalSocket(int sockType) { |
| 74 |
this(new LocalSocketImpl(), sockType); |
| 75 |
} |
| 76 |
|
| 77 |
private LocalSocket(LocalSocketImpl impl, int sockType) { |
| 78 |
this.impl = impl; |
| 79 |
this.sockType = sockType; |
| 80 |
this.isConnected = false; |
| 81 |
this.isBound = false; |
| 82 |
} |
| 83 |
|
| 84 |
private void checkConnected() { |
| 85 |
try { |
| 86 |
Os.getpeername(impl.getFileDescriptor()); |
| 87 |
} catch (ErrnoException e) { |
| 88 |
throw new IllegalArgumentException("Not a connected socket", e); |
| 89 |
} |
| 90 |
isConnected = true; |
| 91 |
isBound = true; |
| 92 |
implCreated = true; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@SystemApi(client = MODULE_LIBRARIES) |
| 106 |
public LocalSocket(@NonNull @SuppressLint("UseParcelFileDescriptor") FileDescriptor fd) { |
| 107 |
this(new LocalSocketImpl(fd), SOCKET_UNKNOWN); |
| 108 |
checkConnected(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static LocalSocket createLocalSocketForAccept(LocalSocketImpl impl) { |
| 115 |
LocalSocket socket = new LocalSocket(impl, SOCKET_UNKNOWN); |
| 116 |
socket.checkConnected(); |
| 117 |
return socket; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
@Override |
| 122 |
public String toString() { |
| 123 |
return super.toString() + " impl:" + impl; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private void implCreateIfNeeded() throws IOException { |
| 134 |
if (!implCreated) { |
| 135 |
synchronized (this) { |
| 136 |
if (!implCreated) { |
| 137 |
try { |
| 138 |
impl.create(sockType); |
| 139 |
} finally { |
| 140 |
implCreated = true; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void connect(LocalSocketAddress endpoint) throws IOException { |
| 156 |
synchronized (this) { |
| 157 |
if (isConnected) { |
| 158 |
throw new IOException("already connected"); |
| 159 |
} |
| 160 |
|
| 161 |
implCreateIfNeeded(); |
| 162 |
impl.connect(endpoint, 0); |
| 163 |
isConnected = true; |
| 164 |
isBound = true; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public void bind(LocalSocketAddress bindpoint) throws IOException { |
| 176 |
implCreateIfNeeded(); |
| 177 |
|
| 178 |
synchronized (this) { |
| 179 |
if (isBound) { |
| 180 |
throw new IOException("already bound"); |
| 181 |
} |
| 182 |
|
| 183 |
localAddress = bindpoint; |
| 184 |
impl.bind(localAddress); |
| 185 |
isBound = true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public LocalSocketAddress getLocalSocketAddress() { |
| 195 |
return localAddress; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public InputStream getInputStream() throws IOException { |
| 205 |
implCreateIfNeeded(); |
| 206 |
return impl.getInputStream(); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public OutputStream getOutputStream() throws IOException { |
| 216 |
implCreateIfNeeded(); |
| 217 |
return impl.getOutputStream(); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Override |
| 226 |
public void close() throws IOException { |
| 227 |
implCreateIfNeeded(); |
| 228 |
impl.close(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public void shutdownInput() throws IOException { |
| 237 |
implCreateIfNeeded(); |
| 238 |
impl.shutdownInput(); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public void shutdownOutput() throws IOException { |
| 247 |
implCreateIfNeeded(); |
| 248 |
impl.shutdownOutput(); |
| 249 |
} |
| 250 |
|
| 251 |
public void setReceiveBufferSize(int size) throws IOException { |
| 252 |
impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size)); |
| 253 |
} |
| 254 |
|
| 255 |
public int getReceiveBufferSize() throws IOException { |
| 256 |
return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue(); |
| 257 |
} |
| 258 |
|
| 259 |
public void setSoTimeout(int n) throws IOException { |
| 260 |
impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(n)); |
| 261 |
} |
| 262 |
|
| 263 |
public int getSoTimeout() throws IOException { |
| 264 |
return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue(); |
| 265 |
} |
| 266 |
|
| 267 |
public void setSendBufferSize(int n) throws IOException { |
| 268 |
impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(n)); |
| 269 |
} |
| 270 |
|
| 271 |
public int getSendBufferSize() throws IOException { |
| 272 |
return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue(); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
public LocalSocketAddress getRemoteSocketAddress() { |
| 277 |
throw new UnsupportedOperationException(); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
public synchronized boolean isConnected() { |
| 282 |
return isConnected; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
public boolean isClosed() { |
| 287 |
throw new UnsupportedOperationException(); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
public synchronized boolean isBound() { |
| 292 |
return isBound; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
public boolean isOutputShutdown() { |
| 297 |
throw new UnsupportedOperationException(); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
public boolean isInputShutdown() { |
| 302 |
throw new UnsupportedOperationException(); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
public void connect(LocalSocketAddress endpoint, int timeout) |
| 307 |
throws IOException { |
| 308 |
throw new UnsupportedOperationException(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public void setFileDescriptorsForSend(FileDescriptor[] fds) { |
| 320 |
impl.setFileDescriptorsForSend(fds); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public FileDescriptor[] getAncillaryFileDescriptors() throws IOException { |
| 334 |
return impl.getAncillaryFileDescriptors(); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public Credentials getPeerCredentials() throws IOException { |
| 345 |
return impl.getPeerCredentials(); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public FileDescriptor getFileDescriptor() { |
| 354 |
return impl.getFileDescriptor(); |
| 355 |
} |
| 356 |
} |