1 /*
2  * Copyright (C) 2011 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.system;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 
23 import libcore.util.Objects;
24 
25 /**
26  * Corresponds to C's {@code struct linger} from
27  * <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html">&lt;sys/socket.h&gt;</a>
28  *
29  * When enabled, a {@link Os.close(java.io.FileDescriptor) or
30  * {@link Os.shutdown(java.io.FileDescriptor, int)} will
31  * not return until all queued messages for the socket have been successfully sent or the
32  * linger timeout has been reached. Otherwise, the call returns immediately and the closing is
33  * done in the background.
34  *
35  * See <a href="https://man7.org/linux/man-pages/man7/socket.7.html">socket(7)</a>
36  * for linger struct description.
37  *
38  * @see Os#getsockoptLinger(java.io.FileDescriptor, int, int).
39  * @see OsConstants#SO_LINGER
40  *
41  * @hide
42  */
43 @SystemApi(client = MODULE_LIBRARIES)
44 public final class StructLinger {
45     /**
46      * Whether or not linger is enabled. Non-zero is on.
47      *
48      * @hide
49      */
50     public final int l_onoff;
51 
52     /**
53      * Linger time in seconds.
54      *
55      * @hide
56      */
57     @SystemApi(client = MODULE_LIBRARIES)
58     public final int l_linger;
59 
60     /**
61      * Constructs linger structure.
62      *
63      * @param l_onoff  whether or not linger is enabled, non-zero is on
64      * @param l_linger linger time, in seconds
65      *
66      * @hide
67      */
68     @SuppressWarnings("NewApi") // False positive lint limitation, see b/177434707.
69     @SystemApi(client = MODULE_LIBRARIES)
StructLinger(int l_onoff, int l_linger)70     public StructLinger(int l_onoff, int l_linger) {
71         this.l_onoff = l_onoff;
72         this.l_linger = l_linger;
73     }
74 
75     /**
76      * Returns whether linger is on or not.
77      *
78      * @return {@code true} if linger is enabled, and {@code false} otherwise
79      *
80      * @hide
81      */
82     @SuppressWarnings("NewApi") // False positive lint limitation, see b/177434707.
83     @SystemApi(client = MODULE_LIBRARIES)
isOn()84     public boolean isOn() {
85         return l_onoff != 0;
86     }
87 
88     /**
89      * @hide
90      */
91     @Override
toString()92     public String toString() {
93         return Objects.toString(this);
94     }
95 }
96