OSDN Git Service

2002-04-16 David S. Miller <davem@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / net / PlainDatagramSocketImpl.java
1 // PlainDatagramSocketImpl.java - Implementation of DatagramSocketImpl.
2
3 /* Copyright (C) 1999  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.net;
12 import java.io.IOException;
13
14 /**
15  * @author Warren Levy <warrenl@cygnus.com>
16  * @date May 3, 1999.
17  */
18
19 /**
20  * Written using on-line Java Platform 1.2 API Specification, as well
21  * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
22  * Status:  Believed complete and correct.
23  */
24
25 class PlainDatagramSocketImpl extends DatagramSocketImpl
26 {
27   // These fields are mirrored for use in native code to avoid cpp conflicts
28   // when the #defines in system header files are the same as the public fields.
29   static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
30                    _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
31                    _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
32                    _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
33                    _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
34                    _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
35                    _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
36                    _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
37
38   int fnum = -1;
39
40   // FIXME: Is this necessary?  Could it help w/ DatagramSocket.getLocalAddress?
41   // InetAddress address;
42   
43   // localAddress cache  
44   InetAddress localAddress;
45
46   // 'timeout' is set/read by setOption/getOption.
47   int timeout = 0;
48
49   // FIXME: Probably should have bind (and create?) calls from DatagramSocket
50   // constuctor.  If so, then same change should be made to the corresponding
51   // Socket (non-datagram) classes.  This allows the implementation more
52   // complete control over how the socket is set up and used (e.g. connect,
53   // setting options, etc.).
54   public PlainDatagramSocketImpl()
55   {
56   }
57
58   protected native void bind(int lport, InetAddress laddr)
59         throws SocketException;
60   protected native void create() throws SocketException;
61   protected native int peek(InetAddress i) throws IOException;
62   protected native void setTimeToLive(int ttl) throws IOException;
63   protected native int getTimeToLive() throws IOException;
64   protected native void send(DatagramPacket p) throws IOException;
65   protected native void receive(DatagramPacket p) throws IOException;
66   public native void setOption(int optID, Object value) throws SocketException;
67   public native Object getOption(int optID) throws SocketException;
68   private native void mcastGrp(InetAddress inetaddr, boolean join)
69         throws IOException;
70   protected native void close();
71
72   // Deprecated in JDK 1.2.
73   protected byte getTTL() throws IOException
74   {
75     return (byte) getTimeToLive();
76   }
77
78   // Deprecated in JDK 1.2.
79   protected void setTTL(byte ttl) throws IOException
80   {
81     setTimeToLive(((int) ttl) & 0xFF);
82   }
83
84   protected void join(InetAddress inetaddr) throws IOException
85   {
86     mcastGrp(inetaddr, true);
87   }
88
89   protected void leave(InetAddress inetaddr) throws IOException
90   {
91     mcastGrp(inetaddr, false);
92   }
93
94   protected void finalize() throws Throwable
95   {
96     synchronized (this)
97       {
98         if (fnum != -1)
99           close();
100       }
101     super.finalize();
102   }
103 }