Skip to content

Commit

Permalink
Add option to ignore certain default packets
Browse files Browse the repository at this point in the history
Useful, e.g in 1:n connections were close packets are invalid
  • Loading branch information
GermanCoding committed Aug 13, 2021
1 parent 33a7eda commit 9d581ee
Showing 1 changed file with 38 additions and 1 deletion.
Expand Up @@ -31,6 +31,9 @@
public class DefaultPacketListener implements PacketListener {

private PacketHandler handler;
private boolean ignoreClosePackets;
private boolean ignoreHandshakePackets;
private boolean ignoreKeepAlivePackets;

public DefaultPacketListener(PacketHandler handler) {
this.handler = handler;
Expand All @@ -53,7 +56,6 @@ public void onPacketReceived(PacketHandler handler, Packet packet) {
;
}


@Override
public void onConnectionClosed(PacketHandler handler, String message, boolean expected) {
;
Expand All @@ -67,6 +69,10 @@ public void onUnknownPacketReceived(PacketHandler handler, short id) {
// Handlers

private void handleHandshakePacket(HandshakePacket packet) {
if (ignoreHandshakePackets()) {
return;
}

if (packet.getHandshakeID() == PacketHandler.HANDSHAKE_ID_REQUEST) {
handler.sendHandshake(PacketHandler.HANDSHAKE_ID_RESPONSE);
}
Expand All @@ -77,11 +83,18 @@ private void handleHandshakePacket(HandshakePacket packet) {
}

private void handleClosePacket(ClosePacket packet) {
if (ignoreClosePackets()) {
return;
}

if (!handler.isClosed())
handler.onConnectionClosed(packet.getCloseMessage(), true);
}

private void handleKeepAlivePacket(KeepAlivePacket packet) {
if (ignoreKeepAlivePackets()) {
return;
}
if (!packet.isResponse()) {
KeepAlivePacket keepAliveResponse = new KeepAlivePacket();
keepAliveResponse.setResponse(true);
Expand All @@ -94,4 +107,28 @@ public void onConnectionFailed(PacketHandler handler, Throwable exception) {
;
}

public boolean ignoreClosePackets() {
return ignoreClosePackets;
}

public void setIgnoreClosePackets(boolean ignoreClosePackets) {
this.ignoreClosePackets = ignoreClosePackets;
}

public boolean ignoreHandshakePackets() {
return ignoreHandshakePackets;
}

public void setIgnoreHandshakePackets(boolean ignoreHandshakePackets) {
this.ignoreHandshakePackets = ignoreHandshakePackets;
}

public boolean ignoreKeepAlivePackets() {
return ignoreKeepAlivePackets;
}

public void setIgnoreKeepAlivePackets(boolean ignoreKeepAlivePackets) {
this.ignoreKeepAlivePackets = ignoreKeepAlivePackets;
}

}

0 comments on commit 9d581ee

Please sign in to comment.