mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2025-01-05 20:21:59 -05:00
Add local address binding to Client
This commit is contained in:
parent
54f761ca13
commit
30588f7be0
2 changed files with 29 additions and 2 deletions
|
@ -8,15 +8,23 @@ import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||||
public class Client {
|
public class Client {
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
|
private String bindAddress;
|
||||||
|
private int bindPort;
|
||||||
private PacketProtocol protocol;
|
private PacketProtocol protocol;
|
||||||
private Session session;
|
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.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
this.bindAddress = bindAddress;
|
||||||
|
this.bindPort = bindPort;
|
||||||
this.protocol = protocol;
|
this.protocol = protocol;
|
||||||
this.session = factory.createClientSession(this);
|
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.
|
* Gets the host the client is connecting to.
|
||||||
|
@ -35,7 +43,25 @@ public class Client {
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return this.port;
|
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.
|
* Gets the packet protocol of the client.
|
||||||
*
|
*
|
||||||
|
|
|
@ -142,6 +142,7 @@ public class TcpClientSession extends TcpSession {
|
||||||
try {
|
try {
|
||||||
InetSocketAddress remoteAddress = resolveAddress();
|
InetSocketAddress remoteAddress = resolveAddress();
|
||||||
bootstrap.remoteAddress(remoteAddress);
|
bootstrap.remoteAddress(remoteAddress);
|
||||||
|
bootstrap.localAddress(client.getBindAddress(), client.getBindPort());
|
||||||
|
|
||||||
ChannelFuture future = bootstrap.connect().sync();
|
ChannelFuture future = bootstrap.connect().sync();
|
||||||
if(future.isSuccess()) {
|
if(future.isSuccess()) {
|
||||||
|
|
Loading…
Reference in a new issue