mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-02-02 04:13:50 +07:00
Created UnicastEndpoint interface on top of Endpoint
Allows sending a message and letting the Endpoint figure out where it should go Will only be used for client-side endpoints
This commit is contained in:
parent
143bfca048
commit
d1270b1a75
8
core/src/com/riiablo/net/UnicastEndpoint.java
Normal file
8
core/src/com/riiablo/net/UnicastEndpoint.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.riiablo.net;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public interface UnicastEndpoint<T> extends Endpoint<T> {
|
||||
void sendMessage(ByteBuffer bb);
|
||||
void sendMessage(Object qos, ByteBuffer bb);
|
||||
}
|
@ -10,14 +10,14 @@ import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import com.riiablo.net.Endpoint;
|
||||
import com.riiablo.net.PacketProcessor;
|
||||
import com.riiablo.net.UnicastEndpoint;
|
||||
import com.riiablo.net.reliable.channel.ReliableMessageChannel;
|
||||
import com.riiablo.net.reliable.channel.UnreliableMessageChannel;
|
||||
import com.riiablo.net.reliable.channel.UnreliableOrderedMessageChannel;
|
||||
import com.riiablo.util.EnumIntMap;
|
||||
|
||||
public class ReliableEndpoint implements Endpoint<DatagramPacket>, MessageChannel.PacketTransceiver {
|
||||
public class ReliableEndpoint implements UnicastEndpoint<DatagramPacket>, MessageChannel.PacketTransceiver {
|
||||
private static final String TAG = "ReliableEndpoint";
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
@ -73,15 +73,25 @@ public class ReliableEndpoint implements Endpoint<DatagramPacket>, MessageChanne
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ByteBuffer bb) {
|
||||
sendMessage((InetSocketAddress) channel.remoteAddress(), bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Object qos, ByteBuffer bb) {
|
||||
sendMessage((InetSocketAddress) channel.remoteAddress(), QoS.Reliable, bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(InetSocketAddress to, ByteBuffer bb) {
|
||||
if (DEBUG_SEND) Log.debug(TAG, "sendMessage (auto)");
|
||||
if (DEBUG_SEND) Log.debug(TAG, "sendMessage (auto) to " + to);
|
||||
sendMessage(to, QoS.Reliable, bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(InetSocketAddress to, Object qos, ByteBuffer bb) {
|
||||
if (DEBUG_SEND) Log.debug(TAG, "sendMessage");
|
||||
if (DEBUG_SEND) Log.debug(TAG, "sendMessage to " + to);
|
||||
assert qos instanceof QoS;
|
||||
if (DEBUG_QOS) Log.debug(TAG, "sending message with %s QoS (0x%02x)", qos, ((QoS) qos).ordinal());
|
||||
int channelId = defaultChannels.get((QoS) qos);
|
||||
|
@ -9,10 +9,10 @@ import java.nio.ByteBuffer;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
|
||||
import com.riiablo.net.Endpoint;
|
||||
import com.riiablo.net.PacketProcessor;
|
||||
import com.riiablo.net.UnicastEndpoint;
|
||||
|
||||
public class TcpEndpoint implements Endpoint<ByteBuf> {
|
||||
public class TcpEndpoint implements UnicastEndpoint<ByteBuf> {
|
||||
private static final String TAG = "TcpEndpoint";
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
@ -38,9 +38,19 @@ public class TcpEndpoint implements Endpoint<ByteBuf> {
|
||||
packetProcessor.processPacket(ctx, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ByteBuffer bb) {
|
||||
sendMessage((InetSocketAddress) channel.remoteAddress(), bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(Object qos, ByteBuffer bb) {
|
||||
sendMessage((InetSocketAddress) channel.remoteAddress(), qos, bb);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(InetSocketAddress to, ByteBuffer bb) {
|
||||
if (DEBUG_SEND) Gdx.app.debug(TAG, "sendMessage");
|
||||
if (DEBUG_SEND) Gdx.app.debug(TAG, "sendMessage to " + to);
|
||||
assert to == channel.remoteAddress();
|
||||
channel.writeAndFlush(Unpooled.wrappedBuffer(bb)); // releases msg
|
||||
}
|
||||
|
@ -21,9 +21,9 @@ import com.badlogic.gdx.backends.headless.HeadlessApplication;
|
||||
import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration;
|
||||
|
||||
import com.riiablo.codec.Animation;
|
||||
import com.riiablo.net.Endpoint;
|
||||
import com.riiablo.net.EndpointedChannelHandler;
|
||||
import com.riiablo.net.PacketProcessor;
|
||||
import com.riiablo.net.UnicastEndpoint;
|
||||
import com.riiablo.net.packet.netty.Connection;
|
||||
import com.riiablo.net.packet.netty.Netty;
|
||||
import com.riiablo.net.packet.netty.NettyData;
|
||||
@ -38,7 +38,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
new HeadlessApplication(new TestClient(), config);
|
||||
}
|
||||
|
||||
private Endpoint<?> endpoint;
|
||||
private UnicastEndpoint<?> endpoint;
|
||||
private EventLoopGroup group;
|
||||
|
||||
@Override
|
||||
@ -53,7 +53,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
.handler(new ChannelInitializer<DatagramChannel>() {
|
||||
@Override
|
||||
protected void initChannel(DatagramChannel ch) {
|
||||
Endpoint<DatagramPacket> endpoint = new ReliableEndpoint(ch, TestClient.this);
|
||||
UnicastEndpoint<DatagramPacket> endpoint = new ReliableEndpoint(ch, TestClient.this);
|
||||
TestClient.this.endpoint = endpoint;
|
||||
ch.pipeline()
|
||||
.addLast(new EndpointedChannelHandler<>(DatagramPacket.class, endpoint))
|
||||
@ -79,7 +79,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
int offset = Netty.createNetty(builder, 0L, NettyData.Connection, dataOffset);
|
||||
Netty.finishNettyBuffer(builder, offset);
|
||||
|
||||
endpoint.sendMessage((InetSocketAddress) endpoint.channel().remoteAddress(), QoS.Unreliable, builder.dataBuffer());
|
||||
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,9 +21,9 @@ import com.badlogic.gdx.backends.headless.HeadlessApplication;
|
||||
import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration;
|
||||
|
||||
import com.riiablo.codec.Animation;
|
||||
import com.riiablo.net.Endpoint;
|
||||
import com.riiablo.net.EndpointedChannelHandler;
|
||||
import com.riiablo.net.PacketProcessor;
|
||||
import com.riiablo.net.UnicastEndpoint;
|
||||
import com.riiablo.net.packet.netty.Connection;
|
||||
import com.riiablo.net.packet.netty.Netty;
|
||||
import com.riiablo.net.packet.netty.NettyData;
|
||||
@ -39,7 +39,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
new HeadlessApplication(new TestClient(), config);
|
||||
}
|
||||
|
||||
private Endpoint<?> endpoint;
|
||||
private UnicastEndpoint<?> endpoint;
|
||||
private EventLoopGroup group;
|
||||
|
||||
@Override
|
||||
@ -55,7 +55,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel ch) {
|
||||
Endpoint<ByteBuf> endpoint = new TcpEndpoint(ch, TestClient.this);
|
||||
UnicastEndpoint<ByteBuf> endpoint = new TcpEndpoint(ch, TestClient.this);
|
||||
TestClient.this.endpoint = endpoint;
|
||||
ch.pipeline()
|
||||
.addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint))
|
||||
@ -81,7 +81,7 @@ public class TestClient extends ApplicationAdapter implements PacketProcessor {
|
||||
int offset = Netty.createNetty(builder, 0L, NettyData.Connection, dataOffset);
|
||||
Netty.finishNettyBuffer(builder, offset);
|
||||
|
||||
endpoint.sendMessage((InetSocketAddress) endpoint.channel().remoteAddress(), QoS.Unreliable, builder.dataBuffer());
|
||||
endpoint.sendMessage(QoS.Unreliable, builder.dataBuffer());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user