Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DotNetSelectorImpl.java « ch « nio « sun « openjdk - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 897cf9a1f4a88b0c6c2686164498e664dbdb4656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

// Parts Copyright (C) 2002-2007 Jeroen Frijters

package sun.nio.ch;

import cli.System.Net.Sockets.Socket;
import cli.System.Net.Sockets.SocketException;
import cli.System.Net.Sockets.AddressFamily;
import cli.System.Net.Sockets.SocketType;
import cli.System.Net.Sockets.ProtocolType;
import cli.System.Net.Sockets.SelectMode;
import cli.System.Collections.ArrayList;
import java.io.IOException;
import java.nio.channels.ClosedSelectorException;
import java.nio.channels.Pipe;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.AbstractSelector;
import java.nio.channels.spi.SelectorProvider;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

final class DotNetSelectorImpl extends SelectorImpl
{
    private ArrayList channelArray = new ArrayList();
    private long updateCount = 0;

    //Pipe used as a wakeup object.
    private final Pipe wakeupPipe;

    // File descriptors corresponding to source and sink
    private final Socket wakeupSourceFd, wakeupSinkFd;

    // Lock for interrupt triggering and clearing
    private final Object interruptLock = new Object();
    private volatile boolean interruptTriggered = false;

    // class for fdMap entries
    private final static class MapEntry
    {
        SelectionKeyImpl ski;
        long updateCount = 0;
        long clearedCount = 0;
        MapEntry(SelectionKeyImpl ski)
        {
            this.ski = ski;
        }
    }
    private final HashMap<Socket, MapEntry> fdMap = new HashMap<Socket, MapEntry>();

    DotNetSelectorImpl(SelectorProvider sp) throws IOException
    {
        super(sp);
        wakeupPipe = Pipe.open();
        wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFD().getSocket();

        // Disable the Nagle algorithm so that the wakeup is more immediate
        SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
        (sink.sc).socket().setTcpNoDelay(true);
        wakeupSinkFd = ((SelChImpl)sink).getFD().getSocket();
    }

    protected int doSelect(long timeout) throws IOException
    {
        if (channelArray == null)
            throw new ClosedSelectorException();
        processDeregisterQueue();
        if (interruptTriggered)
        {
            resetWakeupSocket();
            return 0;
        }

        ArrayList read = new ArrayList();
        ArrayList write = new ArrayList();
        ArrayList error = new ArrayList();
        for (int i = 0; i < channelArray.get_Count(); i++)
        {
            SelectionKeyImpl ski = (SelectionKeyImpl)channelArray.get_Item(i);
            int ops = ski.interestOps();
            if (ski.channel() instanceof SocketChannelImpl)
            {
                // TODO there's a race condition here...
                if (((SocketChannelImpl)ski.channel()).isConnected())
                {
                    ops &= SelectionKey.OP_READ | SelectionKey.OP_WRITE;
                }
                else
                {
                    ops &= SelectionKey.OP_CONNECT;
                }
            }
            if ((ops & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0)
            {
                read.Add(ski.getSocket());
            }
            if ((ops & (SelectionKey.OP_WRITE | SelectionKey.OP_CONNECT)) != 0)
            {
                write.Add(ski.getSocket());
            }
            if ((ops & SelectionKey.OP_CONNECT) != 0)
            {
                error.Add(ski.getSocket());
            }
        }
        read.Add(wakeupSourceFd);
        try
        {
            begin();
            int microSeconds = 1000 * (int)Math.min(Integer.MAX_VALUE / 1000, timeout);
            try
            {
                if (false) throw new SocketException();
                // FXBUG docs say that -1 is infinite timeout, but that doesn't appear to work
                Socket.Select(read, write, error, timeout < 0 ? Integer.MAX_VALUE : microSeconds);
            }
            catch (SocketException _)
            {
                read.Clear();
                write.Clear();
                error.Clear();
            }
        }
        finally
        {
            end();
        }
        processDeregisterQueue();
        int updated = updateSelectedKeys(read, write, error);
        // Done with poll(). Set wakeupSocket to nonsignaled  for the next run.
        resetWakeupSocket();
        return updated;
    }

    private int updateSelectedKeys(ArrayList read, ArrayList write, ArrayList error)
    {
        updateCount++;
        int keys = processFDSet(updateCount, read, PollArrayWrapper.POLLIN);
        keys += processFDSet(updateCount, write, PollArrayWrapper.POLLCONN | PollArrayWrapper.POLLOUT);
        keys += processFDSet(updateCount, error, PollArrayWrapper.POLLIN | PollArrayWrapper.POLLCONN | PollArrayWrapper.POLLOUT);
        return keys;
    }

    private int processFDSet(long updateCount, ArrayList sockets, int rOps)
    {
        int numKeysUpdated = 0;
        for (int i = 0; i < sockets.get_Count(); i++)
        {
            Socket desc = (Socket)sockets.get_Item(i);
            if (desc == wakeupSourceFd)
            {
                synchronized (interruptLock)
                {
                    interruptTriggered = true;
                }
                continue;
            }
            MapEntry me = fdMap.get(desc);
            // If me is null, the key was deregistered in the previous
            // processDeregisterQueue.
            if (me == null)
                continue;
            SelectionKeyImpl sk = me.ski;
            if (selectedKeys.contains(sk))
            { // Key in selected set
                if (me.clearedCount != updateCount)
                {
                    if (sk.channel.translateAndSetReadyOps(rOps, sk) &&
                        (me.updateCount != updateCount))
                    {
                        me.updateCount = updateCount;
                        numKeysUpdated++;
                    }
                }
                else
                { // The readyOps have been set; now add
                    if (sk.channel.translateAndUpdateReadyOps(rOps, sk) &&
                        (me.updateCount != updateCount))
                    {
                        me.updateCount = updateCount;
                        numKeysUpdated++;
                    }
                }
                me.clearedCount = updateCount;
            }
            else
            { // Key is not in selected set yet
                if (me.clearedCount != updateCount)
                {
                    sk.channel.translateAndSetReadyOps(rOps, sk);
                    if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0)
                    {
                        selectedKeys.add(sk);
                        me.updateCount = updateCount;
                        numKeysUpdated++;
                    }
                }
                else
                { // The readyOps have been set; now add
                    sk.channel.translateAndUpdateReadyOps(rOps, sk);
                    if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0)
                    {
                        selectedKeys.add(sk);
                        me.updateCount = updateCount;
                        numKeysUpdated++;
                    }
                }
                me.clearedCount = updateCount;
            }
        }
        return numKeysUpdated;
    }

    protected void implClose() throws IOException
    {
        if (channelArray != null)
        {
            // prevent further wakeup
            synchronized (interruptLock) {
                interruptTriggered = true;
            }
            wakeupPipe.sink().close();
            wakeupPipe.source().close();
            for (int i = 0; i < channelArray.get_Count(); i++)
            { // Deregister channels
                SelectionKeyImpl ski = (SelectionKeyImpl)channelArray.get_Item(i);
                deregister(ski);
                SelectableChannel selch = ski.channel();
                if (!selch.isOpen() && !selch.isRegistered())
                    ((SelChImpl)selch).kill();
            }
            selectedKeys = null;
            channelArray = null;
        }
    }

    protected void implRegister(SelectionKeyImpl ski)
    {
        channelArray.Add(ski);
        fdMap.put(ski.getSocket(), new MapEntry(ski));
        keys.add(ski);
    }

    protected void implDereg(SelectionKeyImpl ski) throws IOException
    {
        channelArray.Remove(ski);
        fdMap.remove(ski.getSocket());
        keys.remove(ski);
        selectedKeys.remove(ski);
        deregister(ski);
        SelectableChannel selch = ski.channel();
        if (!selch.isOpen() && !selch.isRegistered())
        {
            ((SelChImpl)selch).kill();
        }
    }

    public Selector wakeup()
    {
        synchronized (interruptLock)
        {
            if (!interruptTriggered)
            {
                setWakeupSocket();
                interruptTriggered = true;
            }
        }
        return this;
    }

    // Sets Windows wakeup socket to a signaled state.
    private void setWakeupSocket() {
        wakeupSinkFd.Send(new byte[1]);
    }

    // Sets Windows wakeup socket to a non-signaled state.
    private void resetWakeupSocket() {
        synchronized (interruptLock)
        {
            if (interruptTriggered == false)
                return;
            resetWakeupSocket0(wakeupSourceFd);
            interruptTriggered = false;
        }
    }

    private static void resetWakeupSocket0(Socket wakeupSourceFd)
    {
        while (wakeupSourceFd.get_Available() > 0)
        {
            wakeupSourceFd.Receive(new byte[1]);
        }
    }
}