• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import java.io.Closeable;
20 import java.io.FileDescriptor;
21 import java.io.IOException;
22 
23 /**
24  * Non-standard class for creating an inbound UNIX-domain socket
25  * in the Linux abstract namespace.
26  */
27 public class LocalServerSocket implements Closeable {
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     /**
35      * Creates a new server socket listening at specified name.
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      */
LocalServerSocket(String name)42     public LocalServerSocket(String name) throws IOException
43     {
44         impl = new LocalSocketImpl();
45 
46         impl.create(LocalSocket.SOCKET_STREAM);
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. The passed-in FileDescriptor is not managed by this class
59      * and must be closed by the caller. Calling {@link #close()} on a socket
60      * created by this method has no effect.
61      *
62      * @param fd bound file descriptor
63      * @throws IOException
64      */
LocalServerSocket(FileDescriptor fd)65     public LocalServerSocket(FileDescriptor fd) throws IOException
66     {
67         impl = new LocalSocketImpl(fd);
68         impl.listen(LISTEN_BACKLOG);
69         localAddress = impl.getSockAddress();
70     }
71 
72     /**
73      * Obtains the socket's local address
74      *
75      * @return local address
76      */
getLocalSocketAddress()77     public LocalSocketAddress getLocalSocketAddress()
78     {
79         return localAddress;
80     }
81 
82     /**
83      * Accepts a new connection to the socket. Blocks until a new
84      * connection arrives.
85      *
86      * @return a socket representing the new connection.
87      * @throws IOException
88      */
accept()89     public LocalSocket accept() throws IOException
90     {
91         LocalSocketImpl acceptedImpl = new LocalSocketImpl();
92 
93         impl.accept(acceptedImpl);
94 
95         return LocalSocket.createLocalSocketForAccept(acceptedImpl);
96     }
97 
98     /**
99      * Returns file descriptor or null if not yet open/already closed
100      *
101      * @return fd or null
102      */
getFileDescriptor()103     public FileDescriptor getFileDescriptor() {
104         return impl.getFileDescriptor();
105     }
106 
107     /**
108      * Closes server socket.
109      *
110      * @throws IOException
111      */
close()112     @Override public void close() throws IOException
113     {
114         impl.close();
115     }
116 }
117