Add method to expose the locally opened port for UdpDataSource.

PiperOrigin-RevId: 341707809
This commit is contained in:
claincly 2020-11-10 23:15:37 +00:00 committed by kim-vde
parent b1eef00b80
commit 6f7c97a729

View File

@ -45,6 +45,8 @@ public final class UdpDataSource extends BaseDataSource {
/** The default socket timeout, in milliseconds. */
public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 8 * 1000;
public static final int UDP_PORT_UNSET = -1;
private final int socketTimeoutMillis;
private final byte[] packetBuffer;
private final DatagramPacket packet;
@ -169,4 +171,15 @@ public final class UdpDataSource extends BaseDataSource {
transferEnded();
}
}
/**
* Returns the local port number opened for the UDP connection, or {@link #UDP_PORT_UNSET} if no
* connection is open
*/
public int getLocalPort() {
if (socket == null) {
return UDP_PORT_UNSET;
}
return socket.getLocalPort();
}
}