Skip to content

Commit

Permalink
added resends counter
Browse files Browse the repository at this point in the history
  • Loading branch information
UltimateHikari committed Nov 4, 2021
1 parent a3b6966 commit 80a7ffa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/main/java/me/hikari/snakeclient/ctl/CommWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void run() {
}
var msg = NetUtils.tryDeserializeGameMessage(packet);
handleMsg(msg, packet);
updateLastRecv((InetSocketAddress) packet.getSocketAddress());
updateLastRecv(NetUtils.packet2addr(packet));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -140,7 +140,7 @@ private void handleJoin(SnakesProto.GameMessage msg, DatagramPacket packet) thro
var errMsg = SnakesProto.GameMessage.ErrorMsg.newBuilder()
.setErrorMessage(e.getMessage()).build();
var finalErrMsg = SnakesProto.GameMessage.newBuilder().setError(errMsg).buildPartial();
sendMessage(finalErrMsg, (InetSocketAddress) packet.getSocketAddress());
sendMessage(finalErrMsg, NetUtils.packet2addr(packet));
}
sendAckVerbose(msg, packet, receiverID);
}
Expand Down Expand Up @@ -197,6 +197,17 @@ public void send(DatagramPacket p) throws IOException {
socket.send(p);
}

@Override
public void noteFailed(DatagramPacket packet) throws IOException {
var addr = NetUtils.packet2addr(packet);
if(master != null && addr.equals(master.getAddr())){
manager.masterFailed();
}
if(deputy != null && addr.equals(deputy.getAddr())){
manager.deputyFailed();
}
}

public void sendMessage(SnakesProto.GameMessage msg, InetSocketAddress addr) throws IOException {
SnakesProto.GameMessage finalMsg = msg.toBuilder().setMsgSeq(msg_seq).build();
var buf = NetUtils.serializeGameMessageBuf(finalMsg.toByteArray(), config);
Expand Down Expand Up @@ -251,11 +262,10 @@ private void updateLastSent(InetSocketAddress addr){
}

private void updateMaster(DatagramPacket packet) {
updateMaster(new InetSocketAddress(packet.getAddress().getHostAddress(), packet.getPort()));
updateMaster(NetUtils.packet2addr(packet));
}
private void updateMasterState(DatagramPacket packet) {
var potentialMaster = new InetSocketAddress(packet.getAddress().getHostAddress(), packet.getPort());
if(potentialMaster.equals(master.getAddr())){
if(NetUtils.packet2addr(packet).equals(master.getAddr())){
master.setLastStateTime(System.currentTimeMillis());
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/me/hikari/snakeclient/ctl/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.hikari.snakes.SnakesProto;

import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

class NetUtils {
Expand All @@ -27,4 +28,8 @@ public static SnakesProto.GameMessage tryDeserializeGameMessage(
public static Peer getPeer(DatagramPacket packet) {
return new Peer(packet.getAddress().getHostAddress(), packet.getPort());
}

public static InetSocketAddress packet2addr(DatagramPacket packet){
return new InetSocketAddress(packet.getAddress().getHostAddress(), packet.getPort());
}
}
14 changes: 8 additions & 6 deletions src/main/java/me/hikari/snakeclient/ctl/ResendWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

@Log4j2
class ResendWorker implements Runnable, Resender {
private static final Long MAX_RESENDS = 3L;
private final Object buffersLock = new Object();
public final static Integer RESEND_TIMEOUT_MS = 20;
private final Sender communicator;
Expand Down Expand Up @@ -53,15 +54,16 @@ public void handleAck(SnakesProto.GameMessage msg, Consumer<Integer> join) {
}

public void resendAll() {
var time = System.currentTimeMillis();
datagrams.forEach((k, v) -> {
if (time - v > RESEND_TIMEOUT_MS) {
try {
try {
if (v < MAX_RESENDS) {
resend(k);
} catch (IOException e) {
e.printStackTrace();

} else {
communicator.noteFailed(k);
}
// TODO add remove if too many resends
} catch (IOException e) {
log.error(e.getStackTrace());
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/me/hikari/snakeclient/ctl/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.SocketAddress;

/**
* CommWorker as seen from Resender perspective
Expand All @@ -11,4 +12,6 @@ interface Sender {
void bindResender(Resender resendWorker);

void send(DatagramPacket p) throws IOException;

void noteFailed(DatagramPacket packet) throws IOException;
}

0 comments on commit 80a7ffa

Please sign in to comment.