# HG changeset patch
# User asaha
# Date 1300988627 25200
# Node ID bfc1a4516e20e13c84b6597d7bfcbd2fbc3e0c4d
# Parent  16fbb8d0b834c3e0a95a4d51e22ec77b81192bb3
6213702: (so) non-blocking sockets with TCP urgent disabled get still selected for read ops (win)
Reviewed-by: alanb

diff --git a/src/windows/classes/sun/nio/ch/WindowsSelectorImpl.java b/src/windows/classes/sun/nio/ch/WindowsSelectorImpl.java
--- openjdk/jdk/src/windows/classes/sun/nio/ch/WindowsSelectorImpl.java
+++ openjdk/jdk/src/windows/classes/sun/nio/ch/WindowsSelectorImpl.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2011, 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
@@ -308,14 +308,17 @@ final class WindowsSelectorImpl extends 
         private int processSelectedKeys(long updateCount) {
             int numKeysUpdated = 0;
             numKeysUpdated += processFDSet(updateCount, readFds,
-                                           PollArrayWrapper.POLLIN);
+                                           PollArrayWrapper.POLLIN,
+                                           false);
             numKeysUpdated += processFDSet(updateCount, writeFds,
                                            PollArrayWrapper.POLLCONN |
-                                           PollArrayWrapper.POLLOUT);
+                                           PollArrayWrapper.POLLOUT,
+                                           false);
             numKeysUpdated += processFDSet(updateCount, exceptFds,
                                            PollArrayWrapper.POLLIN |
                                            PollArrayWrapper.POLLCONN |
-                                           PollArrayWrapper.POLLOUT);
+                                           PollArrayWrapper.POLLOUT,
+                                           true);
             return numKeysUpdated;
         }
 
@@ -327,7 +330,8 @@ final class WindowsSelectorImpl extends 
          *
          * me.updateCount <= me.clearedCount <= updateCount
          */
-        private int processFDSet(long updateCount, int[] fds, int rOps) {
+        private int processFDSet(long updateCount, int[] fds, int rOps,
+                                 boolean isExceptFds) {
             int numKeysUpdated = 0;
             for (int i = 1; i <= fds[0]; i++) {
                 int desc = fds[i];
@@ -343,6 +347,17 @@ final class WindowsSelectorImpl extends 
                 if (me == null)
                     continue;
                 SelectionKeyImpl sk = me.ski;
+
+                // The descriptor may be in the exceptfds set because there is
+                // OOB data queued to the socket. If there is OOB data then it
+                // is discarded and the key is not added to the selected set.
+                if (isExceptFds &&
+                    (sk.channel() instanceof SocketChannelImpl) &&
+                    discardUrgentData(desc))
+                {
+                    continue;
+                }
+
                 if (selectedKeys.contains(sk)) { // Key in selected set
                     if (me.clearedCount != updateCount) {
                         if (sk.channel.translateAndSetReadyOps(rOps, sk) &&
@@ -448,6 +463,8 @@ final class WindowsSelectorImpl extends 
     }
 
     private native void resetWakeupSocket0(int wakeupSourceFd);
+
+    private native boolean discardUrgentData(int fd);
 
     // We increment this counter on each call to updateSelectedKeys()
     // each entry in  SubSelector.fdsMap has a memorized value of
diff --git a/src/windows/native/sun/nio/ch/WindowsSelectorImpl.c b/src/windows/native/sun/nio/ch/WindowsSelectorImpl.c
--- openjdk/jdk/src/windows/native/sun/nio/ch/WindowsSelectorImpl.c
+++ openjdk/jdk/src/windows/native/sun/nio/ch/WindowsSelectorImpl.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2011, 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
@@ -211,3 +211,20 @@ Java_sun_nio_ch_WindowsSelectorImpl_rese
         recv(scinFd, bytes, WAKEUP_SOCKET_BUF_SIZE, 0);
     }
 }
+
+JNIEXPORT jboolean JNICALL
+Java_sun_nio_ch_WindowsSelectorImpl_discardUrgentData(JNIEnv* env, jobject this,
+                                                      jint s)
+{
+    char data[8];
+    jboolean discarded = JNI_FALSE;
+    int n;
+    do {
+        n = recv(s, data, sizeof(data), MSG_OOB);
+        if (n > 0) {
+            discarded = JNI_TRUE;
+        }
+    } while (n > 0);
+    return discarded;
+}
+
