~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/net/TwoStacksPlainSocketImpl.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
package java.net;
 
26
 
 
27
import java.io.IOException;
 
28
import java.io.FileDescriptor;
 
29
import sun.net.ResourceManager;
 
30
 
 
31
/*
 
32
 * This class defines the plain SocketImpl that is used for all
 
33
 * Windows version lower than Vista. It adds support for IPv6 on
 
34
 * these platforms where available.
 
35
 *
 
36
 * For backward compatibility Windows platforms that do not have IPv6
 
37
 * support also use this implementation, and fd1 gets set to null
 
38
 * during socket creation.
 
39
 *
 
40
 * @author Chris Hegarty
 
41
 */
 
42
 
 
43
class TwoStacksPlainSocketImpl extends AbstractPlainSocketImpl
 
44
{
 
45
    /* second fd, used for ipv6 on windows only.
 
46
     * fd1 is used for listeners and for client sockets at initialization
 
47
     * until the socket is connected. Up to this point fd always refers
 
48
     * to the ipv4 socket and fd1 to the ipv6 socket. After the socket
 
49
     * becomes connected, fd always refers to the connected socket
 
50
     * (either v4 or v6) and fd1 is closed.
 
51
     *
 
52
     * For ServerSockets, fd always refers to the v4 listener and
 
53
     * fd1 the v6 listener.
 
54
     */
 
55
    FileDescriptor fd1;
 
56
 
 
57
    /*
 
58
     * Needed for ipv6 on windows because we need to know
 
59
     * if the socket is bound to ::0 or 0.0.0.0, when a caller
 
60
     * asks for it. Otherwise we don't know which socket to ask.
 
61
     */
 
62
    private InetAddress anyLocalBoundAddr = null;
 
63
 
 
64
    /* to prevent starvation when listening on two sockets, this is
 
65
     * is used to hold the id of the last socket we accepted on.
 
66
     */
 
67
    cli.System.Net.Sockets.Socket lastfd = null;
 
68
 
 
69
    public TwoStacksPlainSocketImpl() {}
 
70
 
 
71
    public TwoStacksPlainSocketImpl(FileDescriptor fd) {
 
72
        this.fd = fd;
 
73
    }
 
74
 
 
75
    /**
 
76
     * Creates a socket with a boolean that specifies whether this
 
77
     * is a stream socket (true) or an unconnected UDP socket (false).
 
78
     */
 
79
    protected synchronized void create(boolean stream) throws IOException {
 
80
        fd1 = new FileDescriptor();
 
81
        try {
 
82
            super.create(stream);
 
83
        } catch (IOException e) {
 
84
            fd1 = null;
 
85
            throw e;
 
86
        }
 
87
    }
 
88
 
 
89
     /**
 
90
     * Binds the socket to the specified address of the specified local port.
 
91
     * @param address the address
 
92
     * @param port the port
 
93
     */
 
94
    protected synchronized void bind(InetAddress address, int lport)
 
95
        throws IOException
 
96
    {
 
97
        super.bind(address, lport);
 
98
        if (address.isAnyLocalAddress()) {
 
99
            anyLocalBoundAddr = address;
 
100
        }
 
101
    }
 
102
 
 
103
    public Object getOption(int opt) throws SocketException {
 
104
        if (isClosedOrPending()) {
 
105
            throw new SocketException("Socket Closed");
 
106
        }
 
107
        if (opt == SO_BINDADDR) {
 
108
            if (fd != null && fd1 != null ) {
 
109
                /* must be unbound or else bound to anyLocal */
 
110
                return anyLocalBoundAddr;
 
111
            }
 
112
            InetAddressContainer in = new InetAddressContainer();
 
113
            socketGetOption(opt, in);
 
114
            return in.addr;
 
115
        } else
 
116
            return super.getOption(opt);
 
117
    }
 
118
 
 
119
    /**
 
120
     * Closes the socket.
 
121
     */
 
122
    protected void close() throws IOException {
 
123
        synchronized(fdLock) {
 
124
            if (fd != null || fd1 != null) {
 
125
                if (!stream) {
 
126
                    ResourceManager.afterUdpClose();
 
127
                }
 
128
                if (fdUseCount == 0) {
 
129
                    if (closePending) {
 
130
                        return;
 
131
                    }
 
132
                    closePending = true;
 
133
                    socketClose();
 
134
                    fd = null;
 
135
                    fd1 = null;
 
136
                    return;
 
137
                } else {
 
138
                    /*
 
139
                     * If a thread has acquired the fd and a close
 
140
                     * isn't pending then use a deferred close.
 
141
                     * Also decrement fdUseCount to signal the last
 
142
                     * thread that releases the fd to close it.
 
143
                     */
 
144
                    if (!closePending) {
 
145
                        closePending = true;
 
146
                        fdUseCount--;
 
147
                        socketClose();
 
148
                    }
 
149
                }
 
150
            }
 
151
        }
 
152
    }
 
153
 
 
154
    void reset() throws IOException {
 
155
        if (fd != null || fd1 != null) {
 
156
            socketClose();
 
157
        }
 
158
        fd = null;
 
159
        fd1 = null;
 
160
        super.reset();
 
161
    }
 
162
 
 
163
    /*
 
164
     * Return true if already closed or close is pending
 
165
     */
 
166
    public boolean isClosedOrPending() {
 
167
        /*
 
168
         * Lock on fdLock to ensure that we wait if a
 
169
         * close is in progress.
 
170
         */
 
171
        synchronized (fdLock) {
 
172
            if (closePending || (fd == null && fd1 == null)) {
 
173
                return true;
 
174
            } else {
 
175
                return false;
 
176
            }
 
177
        }
 
178
    }
 
179
 
 
180
    /* Native methods */
 
181
    
 
182
    void socketCreate(boolean stream) throws IOException {
 
183
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
184
        TwoStacksPlainSocketImpl_c.socketCreate(env, this, stream);
 
185
        env.ThrowPendingException();
 
186
    }
 
187
 
 
188
    void socketConnect(InetAddress address, int port, int timeout) throws IOException {
 
189
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
190
        TwoStacksPlainSocketImpl_c.socketConnect(env, this, address, port, timeout);
 
191
        env.ThrowPendingException();
 
192
    }
 
193
    
 
194
    void socketBind(InetAddress address, int localport) throws IOException {
 
195
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
196
        TwoStacksPlainSocketImpl_c.socketBind(env, this, address, localport);
 
197
        env.ThrowPendingException();
 
198
    }
 
199
    
 
200
    void socketListen(int count) throws IOException {
 
201
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
202
        TwoStacksPlainSocketImpl_c.socketListen(env, this, count);
 
203
        env.ThrowPendingException();
 
204
    }
 
205
 
 
206
    void socketAccept(SocketImpl socket) throws IOException {
 
207
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
208
        TwoStacksPlainSocketImpl_c.socketAccept(env, this, socket);
 
209
        env.ThrowPendingException();
 
210
    }
 
211
 
 
212
    int socketAvailable() throws IOException {
 
213
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
214
        int ret = TwoStacksPlainSocketImpl_c.socketAvailable(env, this);
 
215
        env.ThrowPendingException();
 
216
        return ret;
 
217
    }
 
218
 
 
219
    void socketClose0(boolean useDeferredClose) throws IOException {
 
220
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
221
        TwoStacksPlainSocketImpl_c.socketClose0(env, this, useDeferredClose);
 
222
        env.ThrowPendingException();
 
223
    }
 
224
 
 
225
    void socketShutdown(int howto) throws IOException {
 
226
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
227
        TwoStacksPlainSocketImpl_c.socketShutdown(env, this, howto);
 
228
        env.ThrowPendingException();
 
229
    }
 
230
 
 
231
    void socketSetOption(int cmd, boolean on, Object value) throws SocketException {
 
232
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
233
        TwoStacksPlainSocketImpl_c.socketSetOption(env, this, cmd, on, value);
 
234
        env.ThrowPendingException();
 
235
    }
 
236
 
 
237
    int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
 
238
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
239
        int ret = TwoStacksPlainSocketImpl_c.socketGetOption(env, this, opt, iaContainerObj);
 
240
        env.ThrowPendingException();
 
241
        return ret;
 
242
    }
 
243
 
 
244
    int socketGetOption1(int opt, Object iaContainerObj, FileDescriptor fd) throws SocketException {
 
245
        throw new UnsatisfiedLinkError();
 
246
    }
 
247
 
 
248
    void socketSendUrgentData(int data) throws IOException {
 
249
        ikvm.internal.JNI.JNIEnv env = new ikvm.internal.JNI.JNIEnv();
 
250
        TwoStacksPlainSocketImpl_c.socketSendUrgentData(env, this, data);
 
251
        env.ThrowPendingException();
 
252
    }
 
253
}