1 7 8 package java.net; 9 10 import java.io.FileDescriptor ; 11 import java.io.IOException ; 12 import java.io.InterruptedIOException ; 13 import java.util.Enumeration ; 14 15 24 25 class PlainDatagramSocketImpl extends DatagramSocketImpl 26 { 27 28 private int timeout = 0; 29 private int trafficClass = 0; 30 private boolean connected = false; 31 private InetAddress connectedAddress = null; 32 private int connectedPort = -1; 33 34 35 private int multicastInterface = 0; 36 private boolean loopbackMode = true; 37 private int ttl = -1; 38 39 40 private FileDescriptor fd1; 41 private int fduse=-1; 42 43 47 private int lastfd=-1; 48 49 56 private InetAddress anyLocalBoundAddr=null; 57 58 61 static { 62 java.security.AccessController.doPrivileged( 63 new sun.security.action.LoadLibraryAction("net")); 64 init(); 65 } 66 67 70 protected synchronized void create() throws SocketException { 71 fd = new FileDescriptor (); 72 fd1 = new FileDescriptor (); 73 datagramSocketCreate(); 74 } 75 76 79 protected synchronized void bind(int lport, InetAddress laddr) 80 throws SocketException { 81 82 bind0(lport, laddr); 83 if (laddr.isAnyLocalAddress()) { 84 anyLocalBoundAddr = laddr; 85 } 86 } 87 88 protected synchronized native void bind0(int lport, InetAddress laddr) 89 throws SocketException ; 90 91 96 protected native void send(DatagramPacket p) throws IOException ; 97 98 105 protected void connect(InetAddress address, int port) throws SocketException { 106 connect0(address, port); 107 connectedAddress = address; 108 connectedPort = port; 109 connected = true; 110 } 111 112 116 protected void disconnect() { 117 disconnect0(connectedAddress.family); 118 connected = false; 119 connectedAddress = null; 120 connectedPort = -1; 121 } 122 123 127 protected synchronized native int peek(InetAddress i) throws IOException ; 128 protected synchronized native int peekData(DatagramPacket p) throws IOException ; 129 133 protected synchronized void receive(DatagramPacket p) 134 throws IOException { 135 try { 136 receive0(p); 137 } finally { 138 fduse = -1; 139 } 140 } 141 142 protected synchronized native void receive0(DatagramPacket p) 143 throws IOException ; 144 145 149 protected native void setTimeToLive(int ttl) throws IOException ; 150 151 154 protected native int getTimeToLive() throws IOException ; 155 156 160 protected native void setTTL(byte ttl) throws IOException ; 161 162 165 protected native byte getTTL() throws IOException ; 166 167 171 protected void join(InetAddress inetaddr) throws IOException { 172 join(inetaddr, null); 173 } 174 175 179 protected void leave(InetAddress inetaddr) throws IOException { 180 leave(inetaddr, null); 181 } 182 191 192 protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) 193 throws IOException { 194 if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress )) 195 throw new IllegalArgumentException ("Unsupported address type"); 196 join(((InetSocketAddress )mcastaddr).getAddress(), netIf); 197 } 198 199 private native void join(InetAddress inetaddr, NetworkInterface netIf) 200 throws IOException ; 201 202 210 protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) 211 throws IOException { 212 if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress )) 213 throw new IllegalArgumentException ("Unsupported address type"); 214 leave(((InetSocketAddress )mcastaddr).getAddress(), netIf); 215 } 216 217 private native void leave(InetAddress inetaddr, NetworkInterface netIf) 218 throws IOException ; 219 220 223 protected void close() { 224 if (fd != null || fd1 != null) { 225 datagramSocketClose(); 226 fd = null; 227 fd1 = null; 228 } 229 } 230 231 protected void finalize() { 232 close(); 233 } 234 235 239 240 public void setOption(int optID, Object o) throws SocketException { 241 if (fd == null && fd1 == null) { 242 throw new SocketException ("Socket Closed"); 243 } 244 switch (optID) { 245 249 case SO_TIMEOUT: 250 if (o == null || !(o instanceof Integer )) { 251 throw new SocketException ("bad argument for SO_TIMEOUT"); 252 } 253 int tmp = ((Integer ) o).intValue(); 254 if (tmp < 0) 255 throw new IllegalArgumentException ("timeout < 0"); 256 timeout = tmp; 257 return; 258 case IP_TOS: 259 if (o == null || !(o instanceof Integer )) { 260 throw new SocketException ("bad argument for IP_TOS"); 261 } 262 trafficClass = ((Integer )o).intValue(); 263 break; 264 case SO_REUSEADDR: 265 if (o == null || !(o instanceof Boolean )) { 266 throw new SocketException ("bad argument for SO_REUSEADDR"); 267 } 268 break; 269 case SO_BROADCAST: 270 if (o == null || !(o instanceof Boolean )) { 271 throw new SocketException ("bad argument for SO_BROADCAST"); 272 } 273 break; 274 case SO_BINDADDR: 275 throw new SocketException ("Cannot re-bind Socket"); 276 case SO_RCVBUF: 277 case SO_SNDBUF: 278 if (o == null || !(o instanceof Integer ) || 279 ((Integer )o).intValue() < 0) { 280 throw new SocketException ("bad argument for SO_SNDBUF or " + 281 "SO_RCVBUF"); 282 } 283 break; 284 case IP_MULTICAST_IF: 285 if (o == null || !(o instanceof InetAddress )) 286 throw new SocketException ("bad argument for IP_MULTICAST_IF"); 287 break; 288 case IP_MULTICAST_IF2: 289 if (o == null || !(o instanceof NetworkInterface )) 290 throw new SocketException ("bad argument for IP_MULTICAST_IF2"); 291 break; 292 case IP_MULTICAST_LOOP: 293 if (o == null || !(o instanceof Boolean )) 294 throw new SocketException ("bad argument for IP_MULTICAST_LOOP"); 295 break; 296 default: 297 throw new SocketException ("invalid option: " + optID); 298 } 299 socketSetOption(optID, o); 300 } 301 302 305 306 public Object getOption(int optID) throws SocketException { 307 if (fd == null && fd1 == null) { 308 throw new SocketException ("Socket Closed"); 309 } 310 311 Object result; 312 313 switch (optID) { 314 case SO_TIMEOUT: 315 result = new Integer (timeout); 316 break; 317 318 case IP_TOS: 319 result = socketGetOption(optID); 320 if ( ((Integer )result).intValue() == -1) { 321 result = new Integer (trafficClass); 322 } 323 break; 324 325 case SO_BINDADDR: 326 if (fd != null && fd1 != null) { 327 return anyLocalBoundAddr; 328 } 329 330 case IP_MULTICAST_IF: 331 case IP_MULTICAST_IF2: 332 case SO_RCVBUF: 333 case SO_SNDBUF: 334 case IP_MULTICAST_LOOP: 335 case SO_REUSEADDR: 336 case SO_BROADCAST: 337 result = socketGetOption(optID); 338 break; 339 340 default: 341 throw new SocketException ("invalid option: " + optID); 342 } 343 344 return result; 345 } 346 347 private native void datagramSocketCreate() throws SocketException ; 348 private native void datagramSocketClose(); 349 private native void socketSetOption(int opt, Object val) 350 throws SocketException ; 351 private native Object socketGetOption(int opt) throws SocketException ; 352 353 private native void connect0(InetAddress address, int port) throws SocketException ; 354 private native void disconnect0(int family); 355 356 359 private native static void init(); 360 361 } 362 363 | Popular Tags |