-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,311 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ plugins { | |
} | ||
|
||
group = "fr.islandswars" | ||
version = "0.2.8" | ||
version = "0.3" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/fr/islandswars/commons/service/rabbitmq/packet/IslandsMQ.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package fr.islandswars.commons.service.rabbitmq.packet; | ||
|
||
import com.rabbitmq.client.BuiltinExchangeType; | ||
import fr.islandswars.commons.service.rabbitmq.RabbitMQConnection; | ||
import fr.islandswars.commons.utils.LogUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.UUID; | ||
|
||
/** | ||
* File <b>IslandsMQ</b> located on fr.islandswars.commons.service.rabbitmq.packet | ||
* IslandsMQ is a part of commons. | ||
* <p> | ||
* Copyright (c) 2017 - 2024 Islands Wars. | ||
* <p> | ||
* commons is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>. | ||
* <p> | ||
* | ||
* @author Jangliu, {@literal <[email protected]>} | ||
* Created the 02/06/2024 at 16:47 | ||
* @since 0.3 | ||
*/ | ||
public class IslandsMQ { | ||
|
||
private final String EXCHANGE_NAME = "islands"; | ||
private final String EXCHANGE_FIRST = "server"; | ||
private final String EXCHANGE_ALL = "all"; | ||
private final BuiltinExchangeType EXCHANGE_TYPE = BuiltinExchangeType.TOPIC; | ||
private final RabbitMQConnection connection; | ||
|
||
public IslandsMQ(RabbitMQConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public void gameServerListen(String serverType, UUID id) { | ||
var channel = connection.getConnection(); | ||
try { | ||
var queue = EXCHANGE_FIRST + "." + serverType + "." + id; | ||
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE); | ||
channel.queueDeclare(queue, false, true, false, null); | ||
channel.queueBind(queue, EXCHANGE_NAME, queue); //listen to specific message | ||
channel.queueBind(queue, EXCHANGE_NAME, EXCHANGE_FIRST + "." + serverType + "." + EXCHANGE_ALL); //listen to all same server's type message | ||
channel.queueBind(queue, EXCHANGE_NAME, EXCHANGE_FIRST + "." + EXCHANGE_ALL); //listen to all servers message | ||
|
||
channel.basicConsume(queue, true, (tag, delivery) -> { | ||
String receivedMessage = new String(delivery.getBody(), StandardCharsets.UTF_8); | ||
}, consumerTag -> { | ||
}); | ||
} catch (IOException e) { | ||
LogUtils.error(e); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/fr/islandswars/commons/service/rabbitmq/packet/Packet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fr.islandswars.commons.service.rabbitmq.packet; | ||
|
||
import fr.islandswars.commons.network.NetInput; | ||
import fr.islandswars.commons.network.NetOutput; | ||
|
||
/** | ||
* File <b>Packet</b> located on fr.islandswars.commons.service.rabbitmq.packet | ||
* Packet is a part of commons. | ||
* <p> | ||
* Copyright (c) 2017 - 2024 Islands Wars. | ||
* <p> | ||
* commons is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>. | ||
* <p> | ||
* | ||
* @author Jangliu, {@literal <[email protected]>} | ||
* Created the 02/06/2024 at 16:55 | ||
* @since 0.3 | ||
*/ | ||
public abstract class Packet { | ||
|
||
private final int id; | ||
|
||
public Packet(int id) { | ||
this.id = id; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public abstract void decode(NetInput input) throws Exception; | ||
|
||
public abstract void encode(NetOutput output) throws Exception; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/fr/islandswars/commons/service/rabbitmq/packet/PacketEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package fr.islandswars.commons.service.rabbitmq.packet; | ||
|
||
/** | ||
* File <b>PacketEvent</b> located on fr.islandswars.commons.service.rabbitmq.packet | ||
* PacketEvent is a part of commons. | ||
* <p> | ||
* Copyright (c) 2017 - 2024 Islands Wars. | ||
* <p> | ||
* commons is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>. | ||
* <p> | ||
* | ||
* @author Jangliu, {@literal <[email protected]>} | ||
* Created the 02/06/2024 at 20:31 | ||
* @since 0.3 | ||
*/ | ||
public interface PacketEvent<T extends Packet> { | ||
|
||
void receivePacket(T packet); | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/fr/islandswars/commons/service/rabbitmq/packet/PacketManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package fr.islandswars.commons.service.rabbitmq.packet; | ||
|
||
import fr.islandswars.commons.network.nio.ByteBufferPool; | ||
import fr.islandswars.commons.network.nio.InputByteBuffer; | ||
import fr.islandswars.commons.utils.LogUtils; | ||
import fr.islandswars.commons.utils.ReflectionUtil; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* File <b>PacketManager</b> located on fr.islandswars.commons.service.rabbitmq.packet | ||
* PacketManager is a part of commons. | ||
* <p> | ||
* Copyright (c) 2017 - 2024 Islands Wars. | ||
* <p> | ||
* commons is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <a href="http://www.gnu.org/licenses/">GNU license</a>. | ||
* <p> | ||
* | ||
* @author Jangliu, {@literal <[email protected]>} | ||
* Created the 02/06/2024 at 17:01 | ||
* @since 0.3 | ||
*/ | ||
public class PacketManager { | ||
|
||
private final Map<Integer, List<PacketEvent<? extends Packet>>> handlers; | ||
private final ByteBufferPool pool; | ||
private final PacketType.Bound bound; | ||
|
||
public PacketManager(PacketType.Bound bound, int size, boolean direct) { | ||
this.pool = new ByteBufferPool(size, direct); | ||
this.handlers = new ConcurrentHashMap<>(); | ||
this.bound = bound; | ||
} | ||
|
||
public <T extends Packet> void addListener(PacketType<T> type, PacketEvent<T> event) { | ||
var id = type.getId(); | ||
if (type.getBound().equals(bound)) { | ||
LogUtils.error(new IllegalArgumentException("Cannot listen to packet with the same Bound " + bound)); | ||
return; | ||
} | ||
handlers.compute(id, (k, v) -> { | ||
if (v == null) | ||
v = new CopyOnWriteArrayList<>(); | ||
v.add(event); | ||
return v; | ||
}); | ||
} | ||
|
||
public byte[] encode(Packet packet) throws Exception { | ||
var output = pool.allocateNetOutput(); | ||
output.writeInt(packet.getId()); | ||
packet.encode(output); | ||
var result = output.getBuffer(); | ||
pool.free(output); | ||
return result; | ||
} | ||
|
||
public <T extends Packet> void decode(byte[] delivery) throws Exception { | ||
var input = pool.allocateNetInput(); | ||
((InputByteBuffer) input).getByteBuffer().put(delivery); | ||
((InputByteBuffer) input).getByteBuffer().flip(); | ||
var packetId = input.readInt(); | ||
PacketType<T> packetType = PacketType.getPacketType(packetId); | ||
if (packetType != null) { | ||
T packet = ReflectionUtil.getConstructorAccessor(packetType.getPacketClass()).newInstance(); | ||
packet.decode(input); | ||
pool.free(input); | ||
if (handlers.containsKey(packetId)) { | ||
handlers.get(packetId).forEach(event -> { | ||
@SuppressWarnings("unchecked") | ||
PacketEvent<T> typedEvent = (PacketEvent<T>) event; | ||
typedEvent.receivePacket(packet); | ||
}); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.