< prev index next >

src/java.base/unix/classes/java/net/PlainSocketImpl.java

Print this page


   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 java.util.Set;
  30 import java.util.HashSet;
  31 import java.util.Collections;
  32 import jdk.net.*;
  33 
  34 import static sun.net.ExtendedOptionsImpl.*;
  35 
  36 /*
  37  * On Unix systems we simply delegate to native methods.
  38  *
  39  * @author Chris Hegarty
  40  */
  41 
  42 class PlainSocketImpl extends AbstractPlainSocketImpl
  43 {
  44     static {
  45         initProto();
  46     }
  47 
  48     /**
  49      * Constructs an empty instance.
  50      */
  51     PlainSocketImpl() { }
  52 
  53     /**
  54      * Constructs an instance with the given file descriptor.
  55      */
  56     PlainSocketImpl(FileDescriptor fd) {
  57         this.fd = fd;
  58     }
  59 
  60     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
  61         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {

  62             super.setOption(name, value);





  63         } else {
  64             if (getSocket() == null || !flowSupported()) {
  65                 throw new UnsupportedOperationException("unsupported option");
  66             }
  67             if (isClosedOrPending()) {
  68                 throw new SocketException("Socket closed");
  69             }
  70             checkSetOptionPermission(name);
  71             checkValueType(value, SocketFlow.class);
  72             setFlowOption(getFileDescriptor(), (SocketFlow)value);
  73         }
  74     }
  75 
  76     @SuppressWarnings("unchecked")
  77     protected <T> T getOption(SocketOption<T> name) throws IOException {
  78         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {

  79             return super.getOption(name);





  80         }
  81         if (getSocket() == null || !flowSupported()) {
  82             throw new UnsupportedOperationException("unsupported option");
  83         }
  84         if (isClosedOrPending()) {
  85             throw new SocketException("Socket closed");
  86         }
  87         checkGetOptionPermission(name);
  88         SocketFlow flow = SocketFlow.create();
  89         getFlowOption(getFileDescriptor(), flow);
  90         return (T)flow;
  91     }
  92 
  93     protected Set<SocketOption<?>> supportedOptions() {
  94         HashSet<SocketOption<?>> options = new HashSet<>(
  95             super.supportedOptions());
  96 
  97         if (getSocket() != null && flowSupported()) {
  98             options.add(ExtendedSocketOptions.SO_FLOW_SLA);



  99         }
 100         return options;
 101     }
 102 
 103     protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
 104         try {
 105             socketSetOption0(opt, b, val);
 106         } catch (SocketException se) {
 107             if (socket == null || !socket.isConnected())
 108                 throw se;
 109         }
 110     }
 111 
 112     native void socketCreate(boolean isServer) throws IOException;
 113 
 114     native void socketConnect(InetAddress address, int port, int timeout)
 115         throws IOException;
 116 
 117     native void socketBind(InetAddress address, int port)
 118         throws IOException;
   1 /*
   2  * Copyright (c) 2007, 2016, 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 java.util.Set;
  30 import java.util.HashSet;

  31 import jdk.net.*;
  32 
  33 import static sun.net.ExtendedOptionsImpl.*;
  34 
  35 /*
  36  * On Unix systems we simply delegate to native methods.
  37  *
  38  * @author Chris Hegarty
  39  */
  40 
  41 class PlainSocketImpl extends AbstractPlainSocketImpl
  42 {
  43     static {
  44         initProto();
  45     }
  46 
  47     /**
  48      * Constructs an empty instance.
  49      */
  50     PlainSocketImpl() { }
  51 
  52     /**
  53      * Constructs an instance with the given file descriptor.
  54      */
  55     PlainSocketImpl(FileDescriptor fd) {
  56         this.fd = fd;
  57     }
  58 
  59     protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
  60         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
  61             if (!name.equals(ExtendedSocketOptions.SO_QUICKACK)) {
  62                 super.setOption(name, value);
  63             } else if (isQuickAckAvailable()) {
  64                 setQuickAckOption(getFileDescriptor(), ((Boolean)value));
  65             } else {
  66                 throw new UnsupportedOperationException("unsupported option");
  67             }
  68         } else {
  69             if (getSocket() == null || !flowSupported()) {
  70                 throw new UnsupportedOperationException("unsupported option");
  71             }
  72             if (isClosedOrPending()) {
  73                 throw new SocketException("Socket closed");
  74             }
  75             checkSetOptionPermission(name);
  76             checkValueType(value, SocketFlow.class);
  77             setFlowOption(getFileDescriptor(), (SocketFlow)value);
  78         }
  79     }
  80 
  81     @SuppressWarnings("unchecked")
  82     protected <T> T getOption(SocketOption<T> name) throws IOException {
  83         if (!name.equals(ExtendedSocketOptions.SO_FLOW_SLA)) {
  84             if (!name.equals(ExtendedSocketOptions.SO_QUICKACK)) {
  85                 return super.getOption(name);
  86             } else if (isQuickAckAvailable()) {
  87                 return (T) getQuickAckOption(getFileDescriptor());
  88             } else {
  89                 throw new UnsupportedOperationException("unsupported option");
  90             }
  91         }
  92         if (getSocket() == null || !flowSupported()) {
  93             throw new UnsupportedOperationException("unsupported option");
  94         }
  95         if (isClosedOrPending()) {
  96             throw new SocketException("Socket closed");
  97         }
  98         checkGetOptionPermission(name);
  99         SocketFlow flow = SocketFlow.create();
 100         getFlowOption(getFileDescriptor(), flow);
 101         return (T)flow;
 102     }
 103 
 104     protected Set<SocketOption<?>> supportedOptions() {
 105         HashSet<SocketOption<?>> options = new HashSet<>(
 106             super.supportedOptions());
 107 
 108         if (getSocket() != null && flowSupported()) {
 109             options.add(ExtendedSocketOptions.SO_FLOW_SLA);
 110         }
 111         if (getSocket() != null && isQuickAckAvailable()) {
 112             options.add(ExtendedSocketOptions.SO_QUICKACK);
 113         }
 114         return options;
 115     }
 116 
 117     protected void socketSetOption(int opt, boolean b, Object val) throws SocketException {
 118         try {
 119             socketSetOption0(opt, b, val);
 120         } catch (SocketException se) {
 121             if (socket == null || !socket.isConnected())
 122                 throw se;
 123         }
 124     }
 125 
 126     native void socketCreate(boolean isServer) throws IOException;
 127 
 128     native void socketConnect(InetAddress address, int port, int timeout)
 129         throws IOException;
 130 
 131     native void socketBind(InetAddress address, int port)
 132         throws IOException;
< prev index next >