Skip to content

Commit

Permalink
Switch to sending multiple packets at once (through API)
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Feb 28, 2020
1 parent 741dc36 commit 88e96ef
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 58 deletions.
10 changes: 5 additions & 5 deletions Client/api/src/net/ME1312/SubData/Client/DataClient.java
Expand Up @@ -96,19 +96,19 @@ public final void closed(Callback<NamedContainer<DisconnectReason, DataClient>>.
/**
* Send a message to the Server
*
* @param message Message to send
* @see net.ME1312.SubData.Client.Protocol.ForwardOnly Packets must <b><u>NOT</u></b> e tagged as Forward-Only
* @param messages Messages to send
* @see net.ME1312.SubData.Client.Protocol.ForwardOnly Messages must <b><u>NOT</u></b> be tagged as Forward-Only
*/
public abstract void sendMessage(MessageOut message);
public abstract void sendMessage(MessageOut... messages);

/**
* Forward a message to another Client
*
* @param id Client ID
* @param message Message to send
* @param messages Messages to send
* @see net.ME1312.SubData.Client.Protocol.Forwardable Messages must be tagged as Forwardable
*/
public abstract <ForwardableMessageOut extends MessageOut & Forwardable> void forwardMessage(UUID id, ForwardableMessageOut message);
public abstract <ForwardableMessageOut extends MessageOut & Forwardable> void forwardMessage(UUID id, ForwardableMessageOut... messages);

/**
* Get the Client that connects the Server to us
Expand Down
4 changes: 2 additions & 2 deletions Client/api/src/net/ME1312/SubData/Client/DataSender.java
Expand Up @@ -21,9 +21,9 @@ public interface DataSender {
/**
* Send a message to the Sender
*
* @param message Message to send
* @param messages Messages to send
*/
void sendMessage(MessageOut message);
void sendMessage(MessageOut... messages);

/**
* Get the Client that connects this Sender to us
Expand Down
Expand Up @@ -34,18 +34,18 @@ public void ping(Callback<PingResponse> response) {
}

@Override
public void sendPacket(PacketOut packet) {
public void sendPacket(PacketOut... packets) {
try {
SubDataClient.class.getMethod("forwardPacket", UUID.class, PacketOut.class).invoke(client, id, packet);
SubDataClient.class.getMethod("forwardPacket", UUID.class, PacketOut[].class).invoke(client, id, packets);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) throw (RuntimeException) e.getCause();
} catch (Throwable e) {}
}

@Override
public void sendMessage(MessageOut message) {
public void sendMessage(MessageOut... messages) {
try {
SubDataClient.class.getMethod("forwardMessage", UUID.class, MessageOut.class).invoke(client, id, message);
SubDataClient.class.getMethod("forwardMessage", UUID.class, MessageOut[].class).invoke(client, id, messages);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) throw (RuntimeException) e.getCause();
} catch (Throwable e) {}
Expand Down
Expand Up @@ -15,6 +15,7 @@ public final class InitPacketDeclaration implements InitialProtocol.Packet, Pack
public void receive(SubDataSender sender) throws Throwable {
if (Util.reflect(SubDataClient.class.getDeclaredField("state"), sender.getConnection()) == ConnectionState.PRE_INITIALIZATION) {
Util.reflect(SubDataClient.class.getDeclaredField("state"), sender.getConnection(), ConnectionState.INITIALIZATION);
sender.sendPacket(this);
}
}

Expand Down
77 changes: 49 additions & 28 deletions Client/src/net/ME1312/SubData/Client/SubDataClient.java
Expand Up @@ -63,7 +63,6 @@ public class SubDataClient extends DataClient implements SubDataSender {
};

log.info("Connected to " + socket.getRemoteSocketAddress());
sendPacket(new InitPacketDeclaration());
read();
}

Expand Down Expand Up @@ -358,27 +357,35 @@ public void close() throws IOException {
/**
* Send a packet to Client
*
* @param packet Packet to send
* @see ForwardOnly Packets must <b><u>NOT</u></b> e tagged as Forward-Only
* @param packets Packets to send
* @see ForwardOnly Packets must <b><u>NOT</u></b> be tagged as Forward-Only
*/
public void sendPacket(PacketOut packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (packet instanceof ForwardOnly) throw new IllegalPacketException("Packet is Forward-Only");
if (isClosed() || (state == CLOSING && !(packet instanceof PacketDisconnect || packet instanceof PacketDisconnectUnderstood))) {
if (next == null) sendPacketLater(packet, CLOSED);
else next.sendPacket(packet);
} else if (state.asInt() < POST_INITIALIZATION.asInt() && !(packet instanceof InitialProtocol.Packet)) {
sendPacketLater(packet, (packet instanceof InitialPacket)?POST_INITIALIZATION:READY);
} else if (state == POST_INITIALIZATION && !(packet instanceof InitialPacket)) {
sendPacketLater(packet, READY);
} else {
public void sendPacket(PacketOut... packets) {
List<PacketOut> list = new ArrayList<>();

for (PacketOut packet : packets) {
if (Util.isNull(packet)) throw new NullPointerException();
if (packet instanceof ForwardOnly) throw new IllegalPacketException("Packet is Forward-Only");
if (isClosed() || (state == CLOSING && !(packet instanceof PacketDisconnect || packet instanceof PacketDisconnectUnderstood))) {
if (next == null) sendPacketLater(packet, CLOSED);
else next.sendPacket(packet);
} else if (state.asInt() < POST_INITIALIZATION.asInt() && !(packet instanceof InitialProtocol.Packet)) {
sendPacketLater(packet, (packet instanceof InitialPacket)?POST_INITIALIZATION:READY);
} else if (state == POST_INITIALIZATION && !(packet instanceof InitialPacket)) {
sendPacketLater(packet, READY);
} else {
list.add(packet);
}
}

if (list.size() > 0) {
boolean init = false;

if (queue == null) {
queue = new LinkedList<>();
init = true;
}
queue.add(packet);
queue.addAll(list);

if (init) write();
}
Expand All @@ -393,25 +400,39 @@ private void sendPacketLater(PacketOut packet, ConnectionState state) {
* Forward a packet to another Client
*
* @param id Client ID
* @param packet Packet to send
* @param packets Packets to send
* @see net.ME1312.SubData.Client.Protocol.Forwardable Packets must be tagged as Forwardable
*/
public <ForwardablePacketOut extends PacketOut & Forwardable> void forwardPacket(UUID id, ForwardablePacketOut packet) {
if (Util.isNull(id, packet)) throw new NullPointerException();
if (!(packet instanceof Forwardable)) throw new IllegalPacketException("Packet is not Forwardable");
sendPacket(new PacketForwardPacket(id, packet));
public <ForwardablePacketOut extends PacketOut & Forwardable> void forwardPacket(UUID id, ForwardablePacketOut... packets) {
List<PacketOut> list = new ArrayList<>();
for (PacketOut packet : packets) {
if (Util.isNull(id, packet)) throw new NullPointerException();
if (!(packet instanceof Forwardable)) throw new IllegalPacketException("Packet is not Forwardable");
sendPacket(new PacketForwardPacket(id, packet));
}
sendPacket(list.toArray(new PacketOut[0]));
}

public void sendMessage(MessageOut message) {
if (Util.isNull(message)) throw new NullPointerException();
if (message instanceof ForwardOnly) throw new IllegalMessageException("Message is Forward-Only");
sendPacket(new PacketSendMessage(message));
public void sendMessage(MessageOut... messages) {
List<PacketOut> list = new ArrayList<>();
for (MessageOut message : messages) {
if (Util.isNull(message)) throw new NullPointerException();
if (message instanceof ForwardOnly) throw new IllegalMessageException("Message is Forward-Only");
list.add(new PacketSendMessage(message));
}
sendPacket(list.toArray(new PacketOut[0]));
}

public <ForwardableMessageOut extends MessageOut & Forwardable> void forwardMessage(UUID id, ForwardableMessageOut message) {
if (Util.isNull(id, message)) throw new NullPointerException();
if (!(message instanceof Forwardable)) throw new IllegalMessageException("Message is not Forwardable");
forwardPacket(id, new PacketSendMessage(message));
public <ForwardableMessageOut extends MessageOut & Forwardable> void forwardMessage(UUID id, ForwardableMessageOut... messages) {
if (Util.isNull(id)) throw new NullPointerException();

List<PacketOut> list = new ArrayList<>();
for (MessageOut message : messages) {
if (Util.isNull(message)) throw new NullPointerException();
if (!(message instanceof Forwardable)) throw new IllegalMessageException("Message is not Forwardable");
list.add(new PacketForwardPacket(id, new PacketSendMessage(message)));
}
sendPacket(list.toArray(new PacketOut[0]));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions Client/src/net/ME1312/SubData/Client/SubDataSender.java
Expand Up @@ -9,9 +9,9 @@ public interface SubDataSender extends DataSender {
/**
* Send a packet to the Sender
*
* @param packet Packet to send
* @param packets Packets to send
*/
void sendPacket(PacketOut packet);
void sendPacket(PacketOut... packets);

/**
* Get the Client that connects this Sender to us
Expand Down
4 changes: 2 additions & 2 deletions Server/api/src/net/ME1312/SubData/Server/DataClient.java
Expand Up @@ -71,9 +71,9 @@ public final void closed(Callback<NamedContainer<DisconnectReason, DataClient>>.
/**
* Send a message to the Client
*
* @param message Message to send
* @param messages Messages to send
*/
public abstract void sendMessage(MessageOut message);
public abstract void sendMessage(MessageOut... messages);

/**
* Get the Server this Client belongs to
Expand Down
42 changes: 27 additions & 15 deletions Server/src/net/ME1312/SubData/Server/SubDataClient.java
Expand Up @@ -355,25 +355,33 @@ public void close() throws IOException {
/**
* Send a packet to the Client
*
* @param packet Packet to send
* @param packets Packets to send
*/
public void sendPacket(PacketOut packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (isClosed() || (state == CLOSING && !(packet instanceof PacketDisconnect || packet instanceof PacketDisconnectUnderstood))) {
if (next == null) sendPacketLater(packet, CLOSED);
else next.sendPacket(packet);
} else if (state.asInt() < POST_INITIALIZATION.asInt() && !(packet instanceof InitialProtocol.Packet)) {
sendPacketLater(packet, (packet instanceof InitialPacket)?POST_INITIALIZATION:READY);
} else if (state == POST_INITIALIZATION && !(packet instanceof InitialPacket)) {
sendPacketLater(packet, READY);
} else {
public void sendPacket(PacketOut... packets) {
List<PacketOut> list = new ArrayList<>();

for (PacketOut packet : packets) {
if (Util.isNull(packet)) throw new NullPointerException();
if (isClosed() || (state == CLOSING && !(packet instanceof PacketDisconnect || packet instanceof PacketDisconnectUnderstood))) {
if (next == null) sendPacketLater(packet, CLOSED);
else next.sendPacket(packet);
} else if (state.asInt() < POST_INITIALIZATION.asInt() && !(packet instanceof InitialProtocol.Packet)) {
sendPacketLater(packet, (packet instanceof InitialPacket)?POST_INITIALIZATION:READY);
} else if (state == POST_INITIALIZATION && !(packet instanceof InitialPacket)) {
sendPacketLater(packet, READY);
} else {
list.add(packet);
}
}

if (list.size() > 0) {
boolean init = false;

if (queue == null) {
queue = new LinkedList<>();
init = true;
}
queue.add(packet);
queue.addAll(list);

if (init) write();
}
Expand All @@ -384,9 +392,13 @@ private void sendPacketLater(PacketOut packet, ConnectionState state) {
this.statequeue.put(state, prequeue);
}

public void sendMessage(MessageOut message) {
if (Util.isNull(message)) throw new NullPointerException();
sendPacket(new PacketSendMessage(message));
public void sendMessage(MessageOut... messages) {
List<PacketOut> list = new ArrayList<>();
for (MessageOut message : messages) {
if (Util.isNull(message)) throw new NullPointerException();
list.add(new PacketSendMessage(message));
}
sendPacket(list.toArray(new PacketOut[0]));
}

@Override
Expand Down

0 comments on commit 88e96ef

Please sign in to comment.