Add local address binding to Client

This commit is contained in:
VADemon 2020-12-21 15:58:02 +01:00
parent 54f761ca13
commit 30588f7be0
2 changed files with 29 additions and 2 deletions

View file

@ -8,15 +8,23 @@ import com.github.steveice10.packetlib.packet.PacketProtocol;
public class Client {
private String host;
private int port;
private String bindAddress;
private int bindPort;
private PacketProtocol protocol;
private Session session;
public Client(String host, int port, PacketProtocol protocol, SessionFactory factory) {
public Client(String host, int port, String bindAddress, int bindPort, PacketProtocol protocol, SessionFactory factory) {
this.host = host;
this.port = port;
this.bindAddress = bindAddress;
this.bindPort = bindPort;
this.protocol = protocol;
this.session = factory.createClientSession(this);
}
public Client(String host, int port, PacketProtocol protocol, SessionFactory factory) {
new Client(host, port, null, 0, protocol, factory);
}
/**
* Gets the host the client is connecting to.
@ -35,7 +43,25 @@ public class Client {
public int getPort() {
return this.port;
}
/**
* Gets the the local address the client is connecting from.
*
* @return Client's local IP address or null if default.
*/
public String getBindAddress() {
return this.bindAddress;
}
/**
* Gets the the local port the client is connecting from.
*
* @return Client's local port or 0 if default.
*/
public int getBindPort() {
return this.bindPort;
}
/**
* Gets the packet protocol of the client.
*

View file

@ -142,6 +142,7 @@ public class TcpClientSession extends TcpSession {
try {
InetSocketAddress remoteAddress = resolveAddress();
bootstrap.remoteAddress(remoteAddress);
bootstrap.localAddress(client.getBindAddress(), client.getBindPort());
ChannelFuture future = bootstrap.connect().sync();
if(future.isSuccess()) {