forked from wupeixuan/JDKSourceCode1.8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatagramSocketImpl.java
More file actions
314 lines (284 loc) · 10.8 KB
/
DatagramSocketImpl.java
File metadata and controls
314 lines (284 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.net;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InterruptedIOException;
/**
* Abstract datagram and multicast socket implementation base class.
* @author Pavani Diwanji
* @since JDK1.1
*/
public abstract class DatagramSocketImpl implements SocketOptions {
/**
* The local port number.
*/
protected int localPort;
/**
* The file descriptor object.
*/
protected FileDescriptor fd;
int dataAvailable() {
// default impl returns zero, which disables the calling
// functionality
return 0;
}
/**
* The DatagramSocket or MulticastSocket
* that owns this impl
*/
DatagramSocket socket;
void setDatagramSocket(DatagramSocket socket) {
this.socket = socket;
}
DatagramSocket getDatagramSocket() {
return socket;
}
/**
* Creates a datagram socket.
* @exception SocketException if there is an error in the
* underlying protocol, such as a TCP error.
*/
protected abstract void create() throws SocketException;
/**
* Binds a datagram socket to a local port and address.
* @param lport the local port
* @param laddr the local address
* @exception SocketException if there is an error in the
* underlying protocol, such as a TCP error.
*/
protected abstract void bind(int lport, InetAddress laddr) throws SocketException;
/**
* Sends a datagram packet. The packet contains the data and the
* destination address to send the packet to.
* @param p the packet to be sent.
* @exception IOException if an I/O exception occurs while sending the
* datagram packet.
* @exception PortUnreachableException may be thrown if the socket is connected
* to a currently unreachable destination. Note, there is no guarantee that
* the exception will be thrown.
*/
protected abstract void send(DatagramPacket p) throws IOException;
/**
* Connects a datagram socket to a remote destination. This associates the remote
* address with the local socket so that datagrams may only be sent to this destination
* and received from this destination. This may be overridden to call a native
* system connect.
*
* <p>If the remote destination to which the socket is connected does not
* exist, or is otherwise unreachable, and if an ICMP destination unreachable
* packet has been received for that address, then a subsequent call to
* send or receive may throw a PortUnreachableException.
* Note, there is no guarantee that the exception will be thrown.
* @param address the remote InetAddress to connect to
* @param port the remote port number
* @exception SocketException may be thrown if the socket cannot be
* connected to the remote destination
* @since 1.4
*/
protected void connect(InetAddress address, int port) throws SocketException {}
/**
* Disconnects a datagram socket from its remote destination.
* @since 1.4
*/
protected void disconnect() {}
/**
* Peek at the packet to see who it is from. Updates the specified {@code InetAddress}
* to the address which the packet came from.
* @param i an InetAddress object
* @return the port number which the packet came from.
* @exception IOException if an I/O exception occurs
* @exception PortUnreachableException may be thrown if the socket is connected
* to a currently unreachable destination. Note, there is no guarantee that the
* exception will be thrown.
*/
protected abstract int peek(InetAddress i) throws IOException;
/**
* Peek at the packet to see who it is from. The data is copied into the specified
* {@code DatagramPacket}. The data is returned,
* but not consumed, so that a subsequent peekData/receive operation
* will see the same data.
* @param p the Packet Received.
* @return the port number which the packet came from.
* @exception IOException if an I/O exception occurs
* @exception PortUnreachableException may be thrown if the socket is connected
* to a currently unreachable destination. Note, there is no guarantee that the
* exception will be thrown.
* @since 1.4
*/
protected abstract int peekData(DatagramPacket p) throws IOException;
/**
* Receive the datagram packet.
* @param p the Packet Received.
* @exception IOException if an I/O exception occurs
* while receiving the datagram packet.
* @exception PortUnreachableException may be thrown if the socket is connected
* to a currently unreachable destination. Note, there is no guarantee that the
* exception will be thrown.
*/
protected abstract void receive(DatagramPacket p) throws IOException;
/**
* Set the TTL (time-to-live) option.
* @param ttl a byte specifying the TTL value
*
* @deprecated use setTimeToLive instead.
* @exception IOException if an I/O exception occurs while setting
* the time-to-live option.
* @see #getTTL()
*/
@Deprecated
protected abstract void setTTL(byte ttl) throws IOException;
/**
* Retrieve the TTL (time-to-live) option.
*
* @exception IOException if an I/O exception occurs
* while retrieving the time-to-live option
* @deprecated use getTimeToLive instead.
* @return a byte representing the TTL value
* @see #setTTL(byte)
*/
@Deprecated
protected abstract byte getTTL() throws IOException;
/**
* Set the TTL (time-to-live) option.
* @param ttl an {@code int} specifying the time-to-live value
* @exception IOException if an I/O exception occurs
* while setting the time-to-live option.
* @see #getTimeToLive()
*/
protected abstract void setTimeToLive(int ttl) throws IOException;
/**
* Retrieve the TTL (time-to-live) option.
* @exception IOException if an I/O exception occurs
* while retrieving the time-to-live option
* @return an {@code int} representing the time-to-live value
* @see #setTimeToLive(int)
*/
protected abstract int getTimeToLive() throws IOException;
/**
* Join the multicast group.
* @param inetaddr multicast address to join.
* @exception IOException if an I/O exception occurs
* while joining the multicast group.
*/
protected abstract void join(InetAddress inetaddr) throws IOException;
/**
* Leave the multicast group.
* @param inetaddr multicast address to leave.
* @exception IOException if an I/O exception occurs
* while leaving the multicast group.
*/
protected abstract void leave(InetAddress inetaddr) throws IOException;
/**
* Join the multicast group.
* @param mcastaddr address to join.
* @param netIf specifies the local interface to receive multicast
* datagram packets
* @throws IOException if an I/O exception occurs while joining
* the multicast group
* @since 1.4
*/
protected abstract void joinGroup(SocketAddress mcastaddr,
NetworkInterface netIf)
throws IOException;
/**
* Leave the multicast group.
* @param mcastaddr address to leave.
* @param netIf specified the local interface to leave the group at
* @throws IOException if an I/O exception occurs while leaving
* the multicast group
* @since 1.4
*/
protected abstract void leaveGroup(SocketAddress mcastaddr,
NetworkInterface netIf)
throws IOException;
/**
* Close the socket.
*/
protected abstract void close();
/**
* Gets the local port.
* @return an {@code int} representing the local port value
*/
protected int getLocalPort() {
return localPort;
}
<T> void setOption(SocketOption<T> name, T value) throws IOException {
if (name == StandardSocketOptions.SO_SNDBUF) {
setOption(SocketOptions.SO_SNDBUF, value);
} else if (name == StandardSocketOptions.SO_RCVBUF) {
setOption(SocketOptions.SO_RCVBUF, value);
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
setOption(SocketOptions.SO_REUSEADDR, value);
} else if (name == StandardSocketOptions.IP_TOS) {
setOption(SocketOptions.IP_TOS, value);
} else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
(getDatagramSocket() instanceof MulticastSocket)) {
setOption(SocketOptions.IP_MULTICAST_IF2, value);
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
(getDatagramSocket() instanceof MulticastSocket)) {
if (! (value instanceof Integer)) {
throw new IllegalArgumentException("not an integer");
}
setTimeToLive((Integer)value);
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
(getDatagramSocket() instanceof MulticastSocket)) {
setOption(SocketOptions.IP_MULTICAST_LOOP, value);
} else {
throw new UnsupportedOperationException("unsupported option");
}
}
<T> T getOption(SocketOption<T> name) throws IOException {
if (name == StandardSocketOptions.SO_SNDBUF) {
return (T) getOption(SocketOptions.SO_SNDBUF);
} else if (name == StandardSocketOptions.SO_RCVBUF) {
return (T) getOption(SocketOptions.SO_RCVBUF);
} else if (name == StandardSocketOptions.SO_REUSEADDR) {
return (T) getOption(SocketOptions.SO_REUSEADDR);
} else if (name == StandardSocketOptions.IP_TOS) {
return (T) getOption(SocketOptions.IP_TOS);
} else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
(getDatagramSocket() instanceof MulticastSocket)) {
return (T) getOption(SocketOptions.IP_MULTICAST_IF2);
} else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
(getDatagramSocket() instanceof MulticastSocket)) {
Integer ttl = getTimeToLive();
return (T)ttl;
} else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
(getDatagramSocket() instanceof MulticastSocket)) {
return (T) getOption(SocketOptions.IP_MULTICAST_LOOP);
} else {
throw new UnsupportedOperationException("unsupported option");
}
}
/**
* Gets the datagram socket file descriptor.
* @return a {@code FileDescriptor} object representing the datagram socket
* file descriptor
*/
protected FileDescriptor getFileDescriptor() {
return fd;
}
}