Local address binding: return bind addresses from Session if connected

This commit is contained in:
VADemon 2020-12-23 12:58:52 +01:00
parent 83201135ec
commit 1df02557d1

View file

@ -1,6 +1,8 @@
package com.github.steveice10.packetlib; package com.github.steveice10.packetlib;
import com.github.steveice10.packetlib.packet.PacketProtocol; import com.github.steveice10.packetlib.packet.PacketProtocol;
import com.sun.istack.internal.Nullable;
import java.net.InetSocketAddress;
/** /**
* A client that may connect to a server. * A client that may connect to a server.
@ -45,21 +47,28 @@ public class Client {
} }
/** /**
* Gets the the local address the client is connecting from. * Gets the the local address the client is connected from/will be binding to.
* *
* @return Client's local IP address or null if default. * @return Client's local IP address, or null if default and not connected.
*/ */
@Nullable
public String getBindAddress() { public String getBindAddress() {
return this.bindAddress; final Session session = this.getSession();
return session.isConnected()
? ((InetSocketAddress) session.getLocalAddress()).getAddress().getHostAddress()
: this.bindAddress;
} }
/** /**
* Gets the the local port the client is connecting from. * Gets the the local port the client is connected from/will be binding to.
* *
* @return Client's local port or 0 if default. * @return Client's local port, or 0 if default and not connected.
*/ */
public int getBindPort() { public int getBindPort() {
return this.bindPort; final Session session = this.getSession();
return session.isConnected()
? ((InetSocketAddress) session.getLocalAddress()).getPort()
: this.bindPort;
} }
/** /**