blob: d1f49d2082f5bd7a805a001eb4fcbd5ed23abd1f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.net;
18
Jake Wharton89d62c12017-09-20 12:23:14 -040019import java.io.Closeable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import java.io.FileDescriptor;
Jake Wharton89d62c12017-09-20 12:23:14 -040021import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23/**
Elliott Hughes0adcf152014-12-08 20:47:11 -080024 * Non-standard class for creating an inbound UNIX-domain socket
25 * in the Linux abstract namespace.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 */
Jake Wharton89d62c12017-09-20 12:23:14 -040027public class LocalServerSocket implements Closeable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028 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 Hughes0adcf152014-12-08 20:47:11 -080035 * Creates a new server socket listening at specified name.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 * 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 Lockwoode7d309a2013-07-16 11:36:22 -070046 impl.create(LocalSocket.SOCKET_STREAM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
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 Fullerb08c7bc2017-01-04 10:07:25 +000091 impl.accept(acceptedImpl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Neil Fuller7fd72462017-01-06 11:29:15 +000093 return LocalSocket.createLocalSocketForAccept(acceptedImpl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
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 Wharton89d62c12017-09-20 12:23:14 -0400110 @Override public void close() throws IOException
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 {
112 impl.close();
113 }
114}