Skip to content

Commit

Permalink
Add ability to set time between connection attempts (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alemiz112 authored Sep 23, 2024
2 parents 048ef8f + 31a8a8f commit 1e26b20
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class RakConstants {
public static final int RAKNET_DATAGRAM_HEADER_SIZE = 4;

public static final int MAXIMUM_CONNECTION_ATTEMPTS = 10;

public static final int TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS = 1000;
/**
* Time after {@link RakSessionCodec} is closed due to no activity.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.cloudburstmc.netty.channel.raknet.RakConstants.DEFAULT_UNCONNECTED_MAGIC;
import static org.cloudburstmc.netty.channel.raknet.RakConstants.MTU_SIZES;
import static org.cloudburstmc.netty.channel.raknet.RakConstants.SESSION_TIMEOUT_MS;
import static org.cloudburstmc.netty.channel.raknet.RakConstants.TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS;

/**
* The extended implementation of {@link RakChannelConfig} based on {@link DefaultRakSessionConfig} used by client.
Expand All @@ -41,6 +42,7 @@ public class DefaultRakClientConfig extends DefaultRakSessionConfig {
private volatile Integer[] mtuSizes = MTU_SIZES;
private volatile boolean ipDontFragment = false;
private volatile int clientInternalAddresses = 10;
private volatile int timeBetweenSendConnectionAttemptsMS = TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS;

public DefaultRakClientConfig(Channel channel) {
super(channel);
Expand All @@ -51,7 +53,7 @@ public Map<ChannelOption<?>, Object> getOptions() {
return this.getOptions(
super.getOptions(),
RakChannelOption.RAK_UNCONNECTED_MAGIC, RakChannelOption.RAK_CONNECT_TIMEOUT, RakChannelOption.RAK_REMOTE_GUID, RakChannelOption.RAK_SESSION_TIMEOUT, RakChannelOption.RAK_COMPATIBILITY_MODE,
RakChannelOption.RAK_MTU_SIZES, RakChannelOption.RAK_IP_DONT_FRAGMENT, RakChannelOption.RAK_CLIENT_INTERNAL_ADDRESSES);
RakChannelOption.RAK_MTU_SIZES, RakChannelOption.RAK_IP_DONT_FRAGMENT, RakChannelOption.RAK_CLIENT_INTERNAL_ADDRESSES, RakChannelOption.RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS);
}

@SuppressWarnings("unchecked")
Expand All @@ -73,6 +75,8 @@ public <T> T getOption(ChannelOption<T> option) {
return (T) Boolean.valueOf(this.ipDontFragment);
} else if (option == RakChannelOption.RAK_CLIENT_INTERNAL_ADDRESSES) {
return (T) Integer.valueOf(this.clientInternalAddresses);
} else if (option == RakChannelOption.RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS) {
return (T) Integer.valueOf(this.timeBetweenSendConnectionAttemptsMS);
}
return super.getOption(option);
}
Expand Down Expand Up @@ -105,6 +109,9 @@ public <T> boolean setOption(ChannelOption<T> option, T value) {
} else if (option == RakChannelOption.RAK_CLIENT_INTERNAL_ADDRESSES) {
this.setClientInternalAddresses((Integer) value);
return true;
} else if (option == RakChannelOption.RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS) {
this.setTimeBetweenSendConnectionAttemptsMS((Integer) value);
return true;
}
return super.setOption(option, value);
}
Expand Down Expand Up @@ -181,4 +188,12 @@ public int getClientInternalAddresses() {
public void setClientInternalAddresses(int clientInternalAddresses) {
this.clientInternalAddresses = clientInternalAddresses;
}

public int getTimeBetweenSendConnectionAttemptsMS() {
return this.timeBetweenSendConnectionAttemptsMS;
}

public void setTimeBetweenSendConnectionAttemptsMS(int timeBetweenSendConnectionAttemptsMS) {
this.timeBetweenSendConnectionAttemptsMS = timeBetweenSendConnectionAttemptsMS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public class RakChannelOption<T> extends ChannelOption<T> {
public static final ChannelOption<Integer> RAK_CLIENT_INTERNAL_ADDRESSES =
valueOf(RakChannelOption.class, "RAK_CLIENT_INTERNAL_ADDRESSES");


/**
* The time between the client sending connection attempts in milliseconds.
*/
public static final ChannelOption<Integer> RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS =
valueOf(RakChannelOption.class, "RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS");

@SuppressWarnings("deprecation")
protected RakChannelOption() {
super(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
long timeout = this.rakChannel.config().getOption(RakChannelOption.RAK_CONNECT_TIMEOUT);
this.timeoutFuture = channel.eventLoop().schedule(this::onTimeout, timeout, TimeUnit.MILLISECONDS);
this.retryFuture = channel.eventLoop().scheduleAtFixedRate(() -> this.onRetryAttempt(channel), 0, 1, TimeUnit.SECONDS);
this.retryFuture = channel.eventLoop().scheduleAtFixedRate(() -> this.onRetryAttempt(channel), 0,
this.rakChannel.config().getOption(RakChannelOption.RAK_TIME_BETWEEN_SEND_CONNECTION_ATTEMPTS_MS), TimeUnit.MILLISECONDS);
this.successPromise.addListener(future -> safeCancel(this.timeoutFuture, channel));
this.successPromise.addListener(future -> safeCancel(this.retryFuture, channel));

Expand Down

0 comments on commit 1e26b20

Please sign in to comment.