Skip to content

Commit

Permalink
Merge pull request #5 from Thiies/dev-netty
Browse files Browse the repository at this point in the history
Using lengthdecoder + changed netty packet handling - 1.1.0-SNAPSHOT
  • Loading branch information
HttpMarco authored Apr 6, 2024
2 parents b295e14 + 2762537 commit 68c3f8a
Show file tree
Hide file tree
Showing 25 changed files with 410 additions and 389 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "dev.httpmarco"
version = "1.0.37-SNAPSHOT"
version = "1.1.3-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
public class JsonUtils {

private static final Gson JSON = new GsonBuilder()
.disableHtmlEscaping()
.setExclusionStrategies(new JsonByteExclusionStrategy())
.create();
private static final Gson PRETTY_JSON = new GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
.setExclusionStrategies(new JsonByteExclusionStrategy())
.create();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.httpmarco.osgan.networking;

import dev.httpmarco.osgan.networking.codec.PacketToMessageCodec;
import dev.httpmarco.osgan.networking.codec.PacketDecoder;
import dev.httpmarco.osgan.networking.codec.PacketEncoder;
import io.netty5.channel.Channel;
import io.netty5.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty5.handler.codec.LengthFieldPrepender;
import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
Expand All @@ -16,6 +19,10 @@ public final class ChannelInitializer extends io.netty5.channel.ChannelInitializ

@Override
protected void initChannel(@NotNull Channel channel) {
channel.pipeline().addLast(new PacketToMessageCodec(), communicationComponentHandler);
channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, Integer.BYTES, 0, Integer.BYTES))
.addLast(new PacketDecoder())
.addLast(new LengthFieldPrepender(Integer.BYTES))
.addLast(new PacketEncoder())
.addLast(communicationComponentHandler);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.httpmarco.osgan.networking;

import dev.httpmarco.osgan.files.json.JsonUtils;
import dev.httpmarco.osgan.networking.packet.ForwardPacket;
import io.netty5.buffer.Buffer;
import io.netty5.channel.Channel;
import io.netty5.util.concurrent.Future;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.concurrent.CompletableFuture;

@Getter
@Accessors(fluent = true)
@RequiredArgsConstructor
Expand All @@ -20,11 +23,18 @@ public <P extends Packet> void sendPacket(P object) {
this.sendPacket(this.channel, object);
}

public <P extends Packet> void sendPacket(Channel channel, P object) {
channel.writeAndFlush(object);
public void sendPacket(Channel channel, Packet object) {
this.writeAndFlush(channel, object);
}

public void redirectPacket(String id, Packet object) {
this.sendPacket(new ForwardPacket(id, object));
}

public <P extends Packet> void redirectPacket(String id, P object) {
this.sendPacket(new ForwardPacket(id, object.getClass().getName(), JsonUtils.toJson(object)));
@SneakyThrows
private void writeAndFlush(Channel channel, Packet packet) {
packet.getBuffer().getOrigin().readerOffset(0);

channel.writeAndFlush(packet);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package dev.httpmarco.osgan.networking;

public interface Packet {
import dev.httpmarco.osgan.files.annotations.ConfigExclude;
import dev.httpmarco.osgan.networking.codec.CodecBuffer;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public abstract class Packet {
@ConfigExclude
private final CodecBuffer buffer;

public Packet() {
this.buffer = CodecBuffer.allocate();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
@Accessors(fluent = true)
public final class ClientMetadata extends Metadata {

private @Nullable String id;
private final @Nullable String id;
// if connection is not present, time for reconnect scheduling
private final long reconnectSchedule;
// time for wait a successful connection response
private final int connectionTimeout;

public ClientMetadata(String id, String hostname, int port, long reconnectSchedule, int connectionTimeout) {
public ClientMetadata(@Nullable String id, String hostname, int port, long reconnectSchedule, int connectionTimeout) {
super(hostname, port);
this.id = id;
this.reconnectSchedule = reconnectSchedule;
this.connectionTimeout = connectionTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public NettyClient(ClientMetadata metadata) {
})
.onInactive(it -> {
if ((metadata.hasReconnection())) {
this.reconnectQueue.start();
System.out.println("Starting reconnect queue...");
this.reconnectQueue.start();
}

this.transmit = null;
Expand All @@ -52,7 +52,8 @@ public NettyClient(ClientMetadata metadata) {
.build()))
.option(ChannelOption.AUTO_READ, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.IP_TOS, 24)
//.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, metadata().connectionTimeout());

if (Epoll.isTcpFastOpenClientSideAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public final class ReconnectQueue extends Thread {
@Override
@SneakyThrows
public void run() {
while ((Thread.currentThread().isAlive())) {
Thread.sleep(RECONNECT_TIMEOUT);

while (this.isAlive()) {
if (!this.nettyClient.isConnected()) {
this.nettyClient.connect();
} else {
interrupt();
}
//TODO

Thread.sleep(RECONNECT_TIMEOUT);
}
}
}

This file was deleted.

Loading

0 comments on commit 68c3f8a

Please sign in to comment.