Refactored to send connection packet on main thread instead of through an extra handler attached to the pipeline

This commit is contained in:
Collin Smith 2020-06-24 16:29:49 -07:00
parent b74e870e9b
commit d328eff4d2
2 changed files with 29 additions and 45 deletions

View File

@ -5,8 +5,6 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
@ -56,31 +54,12 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
endpoint = new ReliableEndpoint(ch, TestClient.this);
ch.pipeline()
.addLast(new EndpointedChannelHandler<>(DatagramPacket.class, endpoint))
.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
init();
ctx.pipeline().remove(this);
}
void init() {
InetSocketAddress remoteAddress = (InetSocketAddress) endpoint.channel().remoteAddress();
Gdx.app.log(TAG, "Sending Connection packet to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
FlatBufferBuilder builder = new FlatBufferBuilder();
Connection.startConnection(builder);
int dataOffset = Connection.endConnection(builder);
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
Netty.finishNettyBuffer(builder, offset);
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
}
})
;
}
});
ChannelFuture f = b.connect("localhost", TestServer.PORT);
ChannelFuture f = b.connect("localhost", TestServer.PORT).sync();
sendPacket();
f.channel().closeFuture().sync();
} catch (Throwable t) {
Gdx.app.error(TAG, t.getMessage(), t);
@ -89,6 +68,19 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
}
}
private void sendPacket() {
InetSocketAddress remoteAddress = (InetSocketAddress) endpoint.channel().remoteAddress();
Gdx.app.log(TAG, "Sending Connection packet to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
FlatBufferBuilder builder = new FlatBufferBuilder();
Connection.startConnection(builder);
int dataOffset = Connection.endConnection(builder);
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
Netty.finishNettyBuffer(builder, offset);
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
}
@Override
public void render() {
endpoint.update(Gdx.graphics.getDeltaTime());

View File

@ -5,8 +5,6 @@ import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
@ -58,31 +56,12 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
endpoint = new TcpEndpoint(ch, TestClient.this);
ch.pipeline()
.addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint))
.addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
init();
ctx.pipeline().remove(this);
}
void init() {
InetSocketAddress remoteAddress = (InetSocketAddress) endpoint.channel().remoteAddress();
Gdx.app.log(TAG, "Sending Connection packet to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
FlatBufferBuilder builder = new FlatBufferBuilder();
Connection.startConnection(builder);
int dataOffset = Connection.endConnection(builder);
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
Netty.finishNettyBuffer(builder, offset);
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
}
})
;
}
});
ChannelFuture f = b.connect("localhost", TestServer.PORT).sync();
sendPacket();
f.channel().closeFuture().sync();
} catch (Throwable t) {
Gdx.app.error(TAG, t.getMessage(), t);
@ -92,6 +71,19 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
}
}
private void sendPacket() {
InetSocketAddress remoteAddress = (InetSocketAddress) endpoint.channel().remoteAddress();
Gdx.app.log(TAG, "Sending Connection packet to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
FlatBufferBuilder builder = new FlatBufferBuilder();
Connection.startConnection(builder);
int dataOffset = Connection.endConnection(builder);
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
Netty.finishNettyBuffer(builder, offset);
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
}
@Override
public void render() {
endpoint.update(Gdx.graphics.getDeltaTime());