The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
Jake Wharton | 89d62c1 | 2017-09-20 12:23:14 -0400 | [diff] [blame] | 19 | import java.io.Closeable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import java.io.FileDescriptor; |
Jake Wharton | 89d62c1 | 2017-09-20 12:23:14 -0400 | [diff] [blame] | 21 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
Elliott Hughes | 0adcf15 | 2014-12-08 20:47:11 -0800 | [diff] [blame] | 24 | * Non-standard class for creating an inbound UNIX-domain socket |
| 25 | * in the Linux abstract namespace. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | */ |
Jake Wharton | 89d62c1 | 2017-09-20 12:23:14 -0400 | [diff] [blame] | 27 | public class LocalServerSocket implements Closeable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | private final LocalSocketImpl impl; |
| 29 | private final LocalSocketAddress localAddress; |
| 30 | |
| 31 | /** 50 seems a bit much, but it's what was here */ |
| 32 | private static final int LISTEN_BACKLOG = 50; |
| 33 | |
| 34 | /** |
Elliott Hughes | 0adcf15 | 2014-12-08 20:47:11 -0800 | [diff] [blame] | 35 | * Creates a new server socket listening at specified name. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | * On the Android platform, the name is created in the Linux |
| 37 | * abstract namespace (instead of on the filesystem). |
| 38 | * |
| 39 | * @param name address for socket |
| 40 | * @throws IOException |
| 41 | */ |
| 42 | public LocalServerSocket(String name) throws IOException |
| 43 | { |
| 44 | impl = new LocalSocketImpl(); |
| 45 | |
Mike Lockwood | e7d309a | 2013-07-16 11:36:22 -0700 | [diff] [blame] | 46 | impl.create(LocalSocket.SOCKET_STREAM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | localAddress = new LocalSocketAddress(name); |
| 49 | impl.bind(localAddress); |
| 50 | |
| 51 | impl.listen(LISTEN_BACKLOG); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create a LocalServerSocket from a file descriptor that's already |
| 56 | * been created and bound. listen() will be called immediately on it. |
| 57 | * Used for cases where file descriptors are passed in via environment |
| 58 | * variables |
| 59 | * |
| 60 | * @param fd bound file descriptor |
| 61 | * @throws IOException |
| 62 | */ |
| 63 | public LocalServerSocket(FileDescriptor fd) throws IOException |
| 64 | { |
| 65 | impl = new LocalSocketImpl(fd); |
| 66 | impl.listen(LISTEN_BACKLOG); |
| 67 | localAddress = impl.getSockAddress(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Obtains the socket's local address |
| 72 | * |
| 73 | * @return local address |
| 74 | */ |
| 75 | public LocalSocketAddress getLocalSocketAddress() |
| 76 | { |
| 77 | return localAddress; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Accepts a new connection to the socket. Blocks until a new |
| 82 | * connection arrives. |
| 83 | * |
| 84 | * @return a socket representing the new connection. |
| 85 | * @throws IOException |
| 86 | */ |
| 87 | public LocalSocket accept() throws IOException |
| 88 | { |
| 89 | LocalSocketImpl acceptedImpl = new LocalSocketImpl(); |
| 90 | |
Neil Fuller | b08c7bc | 2017-01-04 10:07:25 +0000 | [diff] [blame] | 91 | impl.accept(acceptedImpl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
Neil Fuller | 7fd7246 | 2017-01-06 11:29:15 +0000 | [diff] [blame] | 93 | return LocalSocket.createLocalSocketForAccept(acceptedImpl); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns file descriptor or null if not yet open/already closed |
| 98 | * |
| 99 | * @return fd or null |
| 100 | */ |
| 101 | public FileDescriptor getFileDescriptor() { |
| 102 | return impl.getFileDescriptor(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Closes server socket. |
| 107 | * |
| 108 | * @throws IOException |
| 109 | */ |
Jake Wharton | 89d62c1 | 2017-09-20 12:23:14 -0400 | [diff] [blame] | 110 | @Override public void close() throws IOException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | { |
| 112 | impl.close(); |
| 113 | } |
| 114 | } |