mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-07 00:39:13 +07:00
Refactored to send connection packet on main thread instead of through an extra handler attached to the pipeline
This commit is contained in:
@ -5,8 +5,6 @@ import io.netty.bootstrap.Bootstrap;
|
|||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufUtil;
|
import io.netty.buffer.ByteBufUtil;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
@ -56,31 +54,12 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
|||||||
endpoint = new ReliableEndpoint(ch, TestClient.this);
|
endpoint = new ReliableEndpoint(ch, TestClient.this);
|
||||||
ch.pipeline()
|
ch.pipeline()
|
||||||
.addLast(new EndpointedChannelHandler<>(DatagramPacket.class, endpoint))
|
.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();
|
f.channel().closeFuture().sync();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
Gdx.app.error(TAG, t.getMessage(), 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
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
endpoint.update(Gdx.graphics.getDeltaTime());
|
endpoint.update(Gdx.graphics.getDeltaTime());
|
||||||
|
@ -5,8 +5,6 @@ import io.netty.bootstrap.Bootstrap;
|
|||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufUtil;
|
import io.netty.buffer.ByteBufUtil;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelOption;
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.EventLoopGroup;
|
||||||
@ -58,31 +56,12 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
|||||||
endpoint = new TcpEndpoint(ch, TestClient.this);
|
endpoint = new TcpEndpoint(ch, TestClient.this);
|
||||||
ch.pipeline()
|
ch.pipeline()
|
||||||
.addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint))
|
.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();
|
ChannelFuture f = b.connect("localhost", TestServer.PORT).sync();
|
||||||
|
sendPacket();
|
||||||
f.channel().closeFuture().sync();
|
f.channel().closeFuture().sync();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
Gdx.app.error(TAG, t.getMessage(), 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
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
endpoint.update(Gdx.graphics.getDeltaTime());
|
endpoint.update(Gdx.graphics.getDeltaTime());
|
||||||
|
Reference in New Issue
Block a user