Skip to content

Commit

Permalink
Bumped version to 2.2.0
Browse files Browse the repository at this point in the history
Added a way to specify how to cut buffers into Packets
Added a way to send multiple packets at once
Added a PacketHandler that will handle only packets that verify a certain condition
  • Loading branch information
adamaq01 committed May 17, 2019
1 parent 330c4bb commit b7ddb99
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {

```Gradle
dependencies {
compile 'com.github.Adamaq01:ozao-net:2.1.0'
compile 'com.github.Adamaq01:ozao-net:2.2.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ publishing {
maven(MavenPublication) {
groupId = 'fr.adamaq01'
artifactId = 'ozao-net'
version = '2.1.0'
version = '2.2.0'

from components.java
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/fr/adamaq01/ozao/net/BaseProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.protocol.Protocol;

import java.util.Collection;
import java.util.List;

public class BaseProtocol extends Protocol {

public BaseProtocol() {
Expand All @@ -19,6 +22,11 @@ public boolean verify(Packet packet) {
return true;
}

@Override
public Collection<Buffer> cut(Buffer buffer) {
return List.of(buffer);
}

@Override
public Packet decode(Buffer buffer) {
return Packet.create(buffer);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/adamaq01/ozao/net/OzaoNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

// TODO Protocol decode returns a list of Packets
public class OzaoNet {

public static final Logger LOGGER = LogManager.getLogger("OzaoNet");
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/fr/adamaq01/ozao/net/OzaoProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.protocol.Protocol;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -35,9 +37,10 @@ public OzaoProtocol(Function<Buffer, Buffer> compressFunction, Function<Buffer,

@Override
public boolean verify(Buffer buffer) {
byte compression = buffer.getByte();
byte magic = buffer.getByte();
byte compression = buffer.readerIndex(5).getByte();
buffer.readerIndex(0);
return compression == 0 || compression == 1;
return magic == 42 && (compression == 0 || compression == 1);
}

@Override
Expand All @@ -46,19 +49,34 @@ public boolean verify(Packet packet) {
return true;
}

@Override
public Collection<Buffer> cut(Buffer buffer) {
ArrayList<Buffer> buffers = new ArrayList<>();
int read = 0;
while (read < buffer.length()) {
int length = buffer.readerIndex(1).getInt();
buffers.add(buffer.sliceCopy(read, length + 6));
read += length + 6;
}
return buffers;
}

@Override
public Packet decode(Buffer buffer) {
boolean compression = buffer.getBoolean();
Buffer packetPayload = compression ? decompressFunction.apply(buffer.sliceCopy(1)) : buffer.sliceCopy(1);
boolean compression = buffer.readerIndex(5).getBoolean();
Buffer packetPayload = compression ? decompressFunction.apply(buffer.sliceCopy(6)) : buffer.sliceCopy(6);
return Packet.create(packetPayload).put("compression", compression);
}

@Override
public Buffer encode(Packet packet) {
Buffer buffer = Buffer.create();
boolean compression = packet.get("compression");
Buffer buffer = Buffer.create();
Buffer payload = compression ? compressFunction.apply(packet.getPayload()) : packet.getPayload();
buffer.putByte((byte) 42);
buffer.putInt(payload.length());
buffer.putBoolean(compression);
buffer.putBuffer(compression ? compressFunction.apply(packet.getPayload()) : packet.getPayload());
buffer.putBuffer(payload);
return buffer;
}
}
27 changes: 26 additions & 1 deletion src/main/java/fr/adamaq01/ozao/net/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.adamaq01.ozao.net.OzaoException;
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.packet.PacketContainer;
import fr.adamaq01.ozao.net.protocol.Protocol;

import java.net.InetSocketAddress;
Expand All @@ -13,11 +14,13 @@ public abstract class Client {
protected Protocol protocol;
protected int timeout;
protected List<ClientHandler> handlers;
protected List<ClientPacketHandler> packetHandlers;

protected Client(Protocol protocol) {
this.protocol = protocol;
this.timeout = 30;
this.handlers = new ArrayList<>();
this.packetHandlers = new ArrayList<>();
}

public Client connect(String address, int port) {
Expand Down Expand Up @@ -48,11 +51,31 @@ public Client addHandler(ClientHandler handler) {
return this;
}

public Client addPacketHandler(ClientPacketHandler packetHandler) {
this.packetHandlers.add(packetHandler);

return this;
}

public List<ClientHandler> getHandlers() {
return handlers;
}

public Client sendPacket(Packet packet) {
public List<ClientPacketHandler> getPacketHandlers() {
return packetHandlers;
}

public final Client sendPackets(PacketContainer packetContainer) {
if (!packetContainer.verify(this.protocol)) {
this.handlers.forEach(handler -> handler.onException(this, new OzaoException("Tried to send one or more packets that does not suit the protocol requirements ! These packets will not be sent !")));
packetContainer.filter(this.protocol);
}
this.sendPackets0(packetContainer);

return this;
}

public final Client sendPacket(Packet packet) {
if (!this.protocol.verify(packet))
this.handlers.forEach(handler -> handler.onException(this, new OzaoException("Tried to send a packet that does not suit the protocol requirements !")));
else
Expand All @@ -62,4 +85,6 @@ public Client sendPacket(Packet packet) {
}

protected abstract void sendPacket0(Packet packet);

protected abstract void sendPackets0(PacketContainer packetContainer);
}
20 changes: 20 additions & 0 deletions src/main/java/fr/adamaq01/ozao/net/client/ClientPacketHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.adamaq01.ozao.net.client;

import fr.adamaq01.ozao.net.packet.Packet;

import java.util.function.Function;

public abstract class ClientPacketHandler {

protected Function<Packet, Boolean> verifyConsumer;

public ClientPacketHandler(Function<Packet, Boolean> verifyConsumer) {
this.verifyConsumer = verifyConsumer;
}

public abstract void onPacketReceive(Client client, Packet packet);

public boolean verify(Packet packet) {
return verifyConsumer.apply(packet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
Buffer buffer = Buffer.create(ReferenceCountUtil.retain(msg));
if (!this.client.getProtocol().verify(buffer))
throw new OzaoException("Received a packet that does not suit the protocol requirements !");
Packet packet = this.client.getProtocol().decode(buffer);
this.client.getHandlers().forEach(handler -> handler.onPacketReceive(client, packet));
this.client.getProtocol().cut(buffer).stream().filter(data -> {
if (!this.client.getProtocol().verify(data)) {
exceptionCaught(ctx, new OzaoException("Received a packet that does not suit the protocol requirements !"));
return false;
}
return true;
}).map(data -> this.client.getProtocol().decode(data)).forEachOrdered(packet -> {
this.client.getHandlers().forEach(handler -> handler.onPacketReceive(client, packet));
this.client.getPacketHandlers().stream().filter(packetHandler -> packetHandler.verify(packet)).forEach(handler -> handler.onPacketReceive(client, packet));
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.adamaq01.ozao.net.client.Client;
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.packet.PacketContainer;
import fr.adamaq01.ozao.net.protocol.Protocol;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -66,4 +67,10 @@ public Client disconnect() {
protected void sendPacket0(Packet packet) {
channel.writeAndFlush(protocol.encode(packet).getData());
}

@Override
protected void sendPackets0(PacketContainer packetContainer) {
packetContainer.forEach(packet -> channel.write(protocol.encode(packet).getData()));
channel.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throw
throw new OzaoException("Received a packet that does not suit the protocol requirements !");
Packet packet = this.client.getProtocol().decode(buffer);
this.client.getHandlers().forEach(handler -> handler.onPacketReceive(client, packet));
this.client.getPacketHandlers().stream().filter(packetHandler -> packetHandler.verify(packet)).forEach(handler -> handler.onPacketReceive(client, packet));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.adamaq01.ozao.net.client.Client;
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.packet.PacketContainer;
import fr.adamaq01.ozao.net.protocol.Protocol;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -66,4 +67,10 @@ public Client disconnect() {
protected void sendPacket0(Packet packet) {
channel.writeAndFlush(protocol.encode(packet).getData());
}

@Override
protected void sendPackets0(PacketContainer packetContainer) {
packetContainer.forEach(packet -> channel.write(protocol.encode(packet).getData()));
channel.flush();
}
}
40 changes: 40 additions & 0 deletions src/main/java/fr/adamaq01/ozao/net/packet/PacketContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fr.adamaq01.ozao.net.packet;

import fr.adamaq01.ozao.net.protocol.Protocol;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.Consumer;

public class PacketContainer {

public static PacketContainer create(Packet... packets) {
return new PacketContainer(packets);
}

private Collection<Packet> packets;

private PacketContainer(Packet... packets) {
this.packets = Arrays.asList(packets);
}

public boolean verify(Protocol protocol) {
return packets.stream().allMatch(packet -> protocol.verify(packet));
}

public PacketContainer filter(Protocol protocol) {
packets.removeIf(packet -> !protocol.verify(packet));

return this;
}

public PacketContainer put(Packet... packets) {
Arrays.stream(packets).forEachOrdered(packet -> this.packets.add(packet));

return this;
}

public void forEach(Consumer<Packet> packetConsumer) {
this.packets.forEach(packetConsumer);
}
}
3 changes: 3 additions & 0 deletions src/main/java/fr/adamaq01/ozao/net/protocol/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.adamaq01.ozao.net.Buffer;
import fr.adamaq01.ozao.net.packet.Packet;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -24,6 +25,8 @@ public String getIdentifier() {

public abstract boolean verify(Packet packet);

public abstract Collection<Buffer> cut(Buffer buffer);

public abstract Packet decode(Buffer buffer);

public abstract Buffer encode(Packet packet);
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/fr/adamaq01/ozao/net/server/Connection.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package fr.adamaq01.ozao.net.server;

import fr.adamaq01.ozao.net.OzaoException;
import fr.adamaq01.ozao.net.client.Client;
import fr.adamaq01.ozao.net.packet.Packet;
import fr.adamaq01.ozao.net.packet.PacketContainer;
import fr.adamaq01.ozao.net.protocol.Protocol;

import java.net.InetSocketAddress;
Expand All @@ -14,7 +16,17 @@ protected Connection(Server server) {
this.server = server;
}

public Connection sendPacket(Packet packet) {
public final Connection sendPackets(PacketContainer packetContainer) {
if (!packetContainer.verify(this.server.protocol)) {
this.server.handlers.forEach(handler -> handler.onException(server, this, new OzaoException("Tried to send one or more packets that does not suit the protocol requirements ! These packets will not be sent !")));
packetContainer.filter(this.server.protocol);
}
this.sendPackets0(packetContainer);

return this;
}

public final Connection sendPacket(Packet packet) {
if (!this.server.protocol.verify(packet))
this.server.handlers.forEach(handler -> handler.onException(server, this, new OzaoException("Tried to send a packet that does not suit the protocol requirements !")));
else
Expand All @@ -25,5 +37,7 @@ public Connection sendPacket(Packet packet) {

protected abstract void sendPacket0(Packet packet);

protected abstract void sendPackets0(PacketContainer packetContainer);

public abstract InetSocketAddress getAddress();
}
12 changes: 12 additions & 0 deletions src/main/java/fr/adamaq01/ozao/net/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ public abstract class Server {
protected int timeout;
protected List<Connection> connections;
protected List<ServerHandler> handlers;
protected List<ServerPacketHandler> packetHandlers;

protected Server(Protocol protocol) {
this.protocol = protocol;
this.timeout = 30;
this.connections = new ArrayList<>();
this.handlers = new ArrayList<>();
this.packetHandlers = new ArrayList<>();
}

public abstract Server bind(int port);
Expand Down Expand Up @@ -55,10 +57,20 @@ public Server addHandler(ServerHandler handler) {
return this;
}

public Server addPacketHandler(ServerPacketHandler packetHandler) {
this.packetHandlers.add(packetHandler);

return this;
}

public List<ServerHandler> getHandlers() {
return handlers;
}

public List<ServerPacketHandler> getPacketHandlers() {
return packetHandlers;
}

public Server broadcastPacket(Packet packet) {
if (!this.protocol.verify(packet))
this.handlers.forEach(handler -> handler.onException(this, null, new OzaoException("Tried to broadcast a packet that does not suit the protocol requirements !")));
Expand Down
Loading

0 comments on commit b7ddb99

Please sign in to comment.