equal
deleted
inserted
replaced
62 */ |
62 */ |
63 private int lastfd=-1; |
63 private int lastfd=-1; |
64 |
64 |
65 static { |
65 static { |
66 init(); |
66 init(); |
|
67 } |
|
68 |
|
69 // true if this socket is exclusively bound |
|
70 private final boolean exclusiveBind; |
|
71 |
|
72 /* |
|
73 * Set to true if SO_REUSEADDR is set after the socket is bound to |
|
74 * indicate SO_REUSEADDR is being emulated |
|
75 */ |
|
76 private boolean reuseAddressEmulated; |
|
77 |
|
78 // emulates SO_REUSEADDR when exclusiveBind is true and socket is bound |
|
79 private boolean isReuseAddress; |
|
80 |
|
81 TwoStacksPlainDatagramSocketImpl(boolean exclBind) { |
|
82 exclusiveBind = exclBind; |
67 } |
83 } |
68 |
84 |
69 protected synchronized void create() throws SocketException { |
85 protected synchronized void create() throws SocketException { |
70 fd1 = new FileDescriptor(); |
86 fd1 = new FileDescriptor(); |
71 try { |
87 try { |
82 if (laddr.isAnyLocalAddress()) { |
98 if (laddr.isAnyLocalAddress()) { |
83 anyLocalBoundAddr = laddr; |
99 anyLocalBoundAddr = laddr; |
84 } |
100 } |
85 } |
101 } |
86 |
102 |
|
103 @Override |
|
104 protected synchronized void bind0(int lport, InetAddress laddr) |
|
105 throws SocketException |
|
106 { |
|
107 bind0(lport, laddr, exclusiveBind); |
|
108 |
|
109 } |
|
110 |
87 protected synchronized void receive(DatagramPacket p) |
111 protected synchronized void receive(DatagramPacket p) |
88 throws IOException { |
112 throws IOException { |
89 try { |
113 try { |
90 receive0(p); |
114 receive0(p); |
91 } finally { |
115 } finally { |
102 if ((fd != null && fd1 != null) && !connected) { |
126 if ((fd != null && fd1 != null) && !connected) { |
103 return anyLocalBoundAddr; |
127 return anyLocalBoundAddr; |
104 } |
128 } |
105 int family = connectedAddress == null ? -1 : connectedAddress.holder().getFamily(); |
129 int family = connectedAddress == null ? -1 : connectedAddress.holder().getFamily(); |
106 return socketLocalAddress(family); |
130 return socketLocalAddress(family); |
107 } else |
131 } else if (optID == SO_REUSEADDR && reuseAddressEmulated) { |
|
132 return isReuseAddress; |
|
133 } else { |
108 return super.getOption(optID); |
134 return super.getOption(optID); |
|
135 } |
|
136 } |
|
137 |
|
138 protected void socketSetOption(int opt, Object val) |
|
139 throws SocketException |
|
140 { |
|
141 if (opt == SO_REUSEADDR && exclusiveBind && localPort != 0) { |
|
142 // socket already bound, emulate |
|
143 reuseAddressEmulated = true; |
|
144 isReuseAddress = (Boolean)val; |
|
145 } else { |
|
146 socketNativeSetOption(opt, val); |
|
147 } |
|
148 |
109 } |
149 } |
110 |
150 |
111 protected boolean isClosed() { |
151 protected boolean isClosed() { |
112 return (fd == null && fd1 == null) ? true : false; |
152 return (fd == null && fd1 == null) ? true : false; |
113 } |
153 } |
121 } |
161 } |
122 } |
162 } |
123 |
163 |
124 /* Native methods */ |
164 /* Native methods */ |
125 |
165 |
126 protected synchronized native void bind0(int lport, InetAddress laddr) |
166 protected synchronized native void bind0(int lport, InetAddress laddr, |
|
167 boolean exclBind) |
127 throws SocketException; |
168 throws SocketException; |
128 |
169 |
129 protected native void send(DatagramPacket p) throws IOException; |
170 protected native void send(DatagramPacket p) throws IOException; |
130 |
171 |
131 protected synchronized native int peek(InetAddress i) throws IOException; |
172 protected synchronized native int peek(InetAddress i) throws IOException; |
153 |
194 |
154 protected native void datagramSocketCreate() throws SocketException; |
195 protected native void datagramSocketCreate() throws SocketException; |
155 |
196 |
156 protected native void datagramSocketClose(); |
197 protected native void datagramSocketClose(); |
157 |
198 |
158 protected native void socketSetOption(int opt, Object val) |
199 protected native void socketNativeSetOption(int opt, Object val) |
159 throws SocketException; |
200 throws SocketException; |
160 |
201 |
161 protected native Object socketGetOption(int opt) throws SocketException; |
202 protected native Object socketGetOption(int opt) throws SocketException; |
162 |
203 |
163 protected native void connect0(InetAddress address, int port) throws SocketException; |
204 protected native void connect0(InetAddress address, int port) throws SocketException; |