mirror of
https://github.com/GeyserMC/MCProtocolLib.git
synced 2024-12-04 21:01:02 -05:00
Merge pull request #25 from VADemon/custom-local-interface
Add local address binding to Client
This commit is contained in:
commit
f37d2eaf3a
3 changed files with 43 additions and 2 deletions
5
pom.xml
5
pom.xml
|
@ -59,6 +59,11 @@
|
|||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.spotbugs</groupId>
|
||||
<artifactId>spotbugs-annotations</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.github.steveice10.packetlib;
|
||||
|
||||
import com.github.steveice10.packetlib.packet.PacketProtocol;
|
||||
import javax.annotation.Nullable;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* A client that may connect to a server.
|
||||
|
@ -8,15 +10,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) {
|
||||
this(host, port, null, 0, protocol, factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the host the client is connecting to.
|
||||
|
@ -35,7 +45,32 @@ public class Client {
|
|||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the the local address the client is connected from/will be binding to.
|
||||
*
|
||||
* @return Client's local IP address, or null if default and not connected.
|
||||
*/
|
||||
@Nullable
|
||||
public String getBindAddress() {
|
||||
final Session session = this.getSession();
|
||||
return session.isConnected()
|
||||
? ((InetSocketAddress) session.getLocalAddress()).getAddress().getHostAddress()
|
||||
: this.bindAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the the local port the client is connected from/will be binding to.
|
||||
*
|
||||
* @return Client's local port, or 0 if default and not connected.
|
||||
*/
|
||||
public int getBindPort() {
|
||||
final Session session = this.getSession();
|
||||
return session.isConnected()
|
||||
? ((InetSocketAddress) session.getLocalAddress()).getPort()
|
||||
: this.bindPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the packet protocol of the client.
|
||||
*
|
||||
|
|
|
@ -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()) {
|
||||
|
|
Loading…
Reference in a new issue