game_machine 0.0.11 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +12 -1
- data/Gemfile.lock +32 -47
- data/Rakefile +0 -27
- data/bin/bundle_run.sh +1 -0
- data/bin/game_machine +29 -27
- data/config/cluster.conf +6 -5
- data/config/default.conf +164 -0
- data/config/game_machine.sql +33 -0
- data/config/game_messages.proto +87 -25
- data/config/gamecloud.conf +140 -0
- data/config/messages.proto +46 -53
- data/config/test.conf +149 -0
- data/game_machine.gemspec +10 -5
- data/games/boot.rb +3 -0
- data/games/example/data/game_data.yml +4 -4
- data/games/example/lib/aggressive_npc.rb +1 -1
- data/games/example/lib/game.rb +1 -2
- data/games/example/lib/player_register.rb +1 -1
- data/games/routes.rb +9 -0
- data/games/tutorial/boot.rb +12 -0
- data/games/tutorial/item_manager.rb +256 -0
- data/games/tutorial/object_store.rb +55 -0
- data/games/tutorial/seed.rb +52 -0
- data/games/tutorial/sql_store.rb +30 -0
- data/java/project/build.gradle +134 -0
- data/java/project/component.erb +719 -0
- data/java/{gradle.properties → project/gradle.properties} +1 -1
- data/java/project/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/java/{gradle → project/gradle}/wrapper/gradle-wrapper.properties +2 -2
- data/java/{gradlew → project/gradlew} +0 -0
- data/java/{gradlew.bat → project/gradlew.bat} +0 -0
- data/java/project/local_lib/AdminUi.jar +0 -0
- data/java/{local_lib/protostuff-compiler-1.0.7-jarjar.jar → project/local_lib/protostuff-compiler-1.0.8-jarjar.jar} +0 -0
- data/java/project/local_lib/sigar/libsigar-amd64-freebsd-6.so +0 -0
- data/java/project/local_lib/sigar/libsigar-amd64-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-amd64-solaris.so +0 -0
- data/java/project/local_lib/sigar/libsigar-ia64-hpux-11.sl +0 -0
- data/java/project/local_lib/sigar/libsigar-ia64-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-pa-hpux-11.sl +0 -0
- data/java/project/local_lib/sigar/libsigar-ppc-aix-5.so +0 -0
- data/java/project/local_lib/sigar/libsigar-ppc-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-ppc64-aix-5.so +0 -0
- data/java/project/local_lib/sigar/libsigar-ppc64-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-s390x-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-sparc-solaris.so +0 -0
- data/java/project/local_lib/sigar/libsigar-sparc64-solaris.so +0 -0
- data/java/project/local_lib/sigar/libsigar-universal-macosx.dylib +0 -0
- data/java/project/local_lib/sigar/libsigar-universal64-macosx.dylib +0 -0
- data/java/project/local_lib/sigar/libsigar-x86-freebsd-5.so +0 -0
- data/java/project/local_lib/sigar/libsigar-x86-freebsd-6.so +0 -0
- data/java/project/local_lib/sigar/libsigar-x86-linux.so +0 -0
- data/java/project/local_lib/sigar/libsigar-x86-solaris.so +0 -0
- data/java/project/local_lib/sigar/sigar-amd64-winnt.dll +0 -0
- data/java/project/local_lib/sigar/sigar-x86-winnt.dll +0 -0
- data/java/project/local_lib/sigar/sigar-x86-winnt.lib +0 -0
- data/java/project/model.erb +99 -0
- data/java/{settings.gradle → project/settings.gradle} +0 -0
- data/java/project/src/main/java/com/game_machine/authentication/DefaultAuthenticator.java +28 -0
- data/java/project/src/main/java/com/game_machine/authentication/PlayerAuthenticator.java +6 -0
- data/java/project/src/main/java/com/game_machine/authentication/PublicAuthenticator.java +20 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/ActorFactory.java +0 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/ActorUtil.java +13 -0
- data/java/project/src/main/java/com/game_machine/core/AuthorizedPlayers.java +23 -0
- data/java/project/src/main/java/com/game_machine/core/ClientMessageDecoder.java +36 -0
- data/java/project/src/main/java/com/game_machine/core/ClientMessageEncoder.java +19 -0
- data/java/project/src/main/java/com/game_machine/core/CloudClient.java +298 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/CommandProxy.java +0 -0
- data/java/project/src/main/java/com/game_machine/core/Commands.java +20 -0
- data/java/project/src/main/java/com/game_machine/core/DatastoreCommands.java +43 -0
- data/java/project/src/main/java/com/game_machine/core/DbConnectionPool.java +72 -0
- data/java/project/src/main/java/com/game_machine/core/DefaultMovementVerifier.java +56 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/EntitySerializer.java +0 -0
- data/java/project/src/main/java/com/game_machine/core/EntityTracking.java +119 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/EventStreamHandler.java +1 -1
- data/java/project/src/main/java/com/game_machine/core/GameActor.java +73 -0
- data/java/project/src/main/java/com/game_machine/core/GameMachineLoader.java +43 -0
- data/java/project/src/main/java/com/game_machine/core/GameMessageActor.java +44 -0
- data/java/project/src/main/java/com/game_machine/core/Grid.java +255 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/GridValue.java +0 -0
- data/java/project/src/main/java/com/game_machine/core/Hashring.java +66 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/IActorFactory.java +0 -0
- data/java/project/src/main/java/com/game_machine/core/LocalLinkedBuffer.java +20 -0
- data/java/project/src/main/java/com/game_machine/core/MessageGateway.java +120 -0
- data/java/project/src/main/java/com/game_machine/core/MessagePersister.java +26 -0
- data/java/project/src/main/java/com/game_machine/core/MonoProxy.java +39 -0
- data/java/project/src/main/java/com/game_machine/core/MovementVerifier.java +7 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/NetMessage.java +10 -6
- data/java/project/src/main/java/com/game_machine/core/PersistentMessage.java +9 -0
- data/java/project/src/main/java/com/game_machine/core/PlayerCommands.java +31 -0
- data/java/project/src/main/java/com/game_machine/core/TcpServer.java +100 -0
- data/java/project/src/main/java/com/game_machine/core/TcpServerHandler.java +54 -0
- data/java/project/src/main/java/com/game_machine/core/TcpServerInitializer.java +32 -0
- data/java/project/src/main/java/com/game_machine/core/UdpClient.java +86 -0
- data/java/{src → project/src}/main/java/com/game_machine/core/UdpServer.java +18 -27
- data/java/{src → project/src}/main/java/com/game_machine/core/UdpServerHandler.java +23 -26
- data/java/project/src/main/java/com/game_machine/core/Vector3.java +159 -0
- data/java/project/src/main/java/com/game_machine/orm/models/PlayerItem.java +118 -0
- data/java/project/src/main/java/com/game_machine/orm/models/TestObject.java +110 -0
- data/java/project/src/main/java/com/game_machine/tutorial/LootGenerator.java +26 -0
- data/java/{src → project/src}/main/resources/game_machine.java.stg +3 -1
- data/java/project/src/main/resources/logback.properties +13 -0
- data/java/project/src/main/resources/logback.xml +76 -0
- data/java/{src → project/src}/main/resources/protostuff.properties +0 -0
- data/java/src/main/java/game/MyGameActor.java +26 -0
- data/lib/game_machine.rb +17 -16
- data/lib/game_machine/actor.rb +1 -1
- data/lib/game_machine/actor/base.rb +8 -31
- data/lib/game_machine/actor/builder.rb +5 -6
- data/lib/game_machine/actor/game_actor.rb +55 -0
- data/lib/game_machine/actor/reloadable.rb +6 -1
- data/lib/game_machine/akka.rb +26 -32
- data/lib/game_machine/app_config.rb +39 -26
- data/lib/game_machine/application.rb +56 -62
- data/lib/game_machine/client_manager.rb +14 -8
- data/lib/game_machine/cloud_updater.rb +51 -0
- data/lib/game_machine/cluster_monitor.rb +3 -3
- data/lib/game_machine/commands.rb +1 -1
- data/lib/game_machine/commands/misc_commands.rb +4 -8
- data/lib/game_machine/commands/player_commands.rb +8 -0
- data/lib/game_machine/console.rb +1 -0
- data/lib/game_machine/console/build.rb +57 -24
- data/lib/game_machine/console/bundle.rb +95 -0
- data/lib/game_machine/console/deploy.rb +30 -0
- data/lib/game_machine/console/install.rb +70 -36
- data/lib/game_machine/console/server.rb +2 -69
- data/lib/game_machine/data_store.rb +111 -15
- data/lib/game_machine/data_stores/couchbase.rb +8 -3
- data/lib/game_machine/data_stores/gamecloud.rb +93 -0
- data/lib/game_machine/data_stores/jdbc.rb +98 -0
- data/lib/game_machine/default_handlers.rb +2 -0
- data/lib/game_machine/default_handlers/team_handler.rb +51 -0
- data/lib/game_machine/default_handlers/zone_manager.rb +30 -0
- data/lib/game_machine/endpoints.rb +0 -4
- data/lib/game_machine/endpoints/udp_incoming.rb +13 -5
- data/lib/game_machine/endpoints/udp_outgoing.rb +15 -9
- data/lib/game_machine/game_systems.rb +0 -2
- data/lib/game_machine/game_systems/agents/controller.rb +2 -2
- data/lib/game_machine/game_systems/entity_tracking.rb +0 -3
- data/lib/game_machine/game_systems/region_manager.rb +3 -2
- data/lib/game_machine/game_systems/region_service.rb +2 -2
- data/lib/game_machine/game_systems/remote_echo.rb +10 -0
- data/lib/game_machine/game_systems/team_manager.rb +2 -11
- data/lib/game_machine/grid.rb +5 -18
- data/lib/game_machine/handlers/authentication.rb +1 -9
- data/lib/game_machine/handlers/game.rb +27 -2
- data/lib/game_machine/handlers/player_authentication.rb +87 -0
- data/lib/game_machine/handlers/request.rb +9 -11
- data/lib/game_machine/hocon_config.rb +81 -0
- data/lib/game_machine/java_lib.rb +14 -1
- data/lib/game_machine/logger.rb +10 -23
- data/lib/game_machine/models.rb +1 -0
- data/lib/game_machine/mono_server.rb +6 -1
- data/lib/game_machine/object_db.rb +12 -6
- data/lib/game_machine/protobuf.rb +1 -1
- data/lib/game_machine/protobuf/game_messages.rb +13 -3
- data/lib/game_machine/protobuf/generate.rb +107 -5
- data/lib/game_machine/restart_watcher.rb +1 -1
- data/lib/game_machine/routes.rb +23 -0
- data/lib/game_machine/scheduler.rb +1 -1
- data/lib/game_machine/securerandom.rb +2 -0
- data/lib/game_machine/system_stats.rb +28 -7
- data/lib/game_machine/version.rb +1 -1
- data/lib/game_machine/wavefront_ext.rb +47 -0
- data/lib/game_machine/write_behind_cache.rb +24 -9
- data/mono/server/Makefile +1 -1
- data/mono/server/Newtonsoft.Json.dll +0 -0
- data/mono/server/build.bat +1 -1
- data/mono/server/callable.cs +9 -0
- data/mono/server/echo.cs +17 -0
- data/mono/server/message_router.cs +16 -23
- data/mono/server/messages.cs +1792 -417
- data/mono/server/protobuf-net.dll +0 -0
- data/mono/server/server.cs +120 -0
- data/mono/server/server.exe +0 -0
- data/pathfinding/astar.cpp +149 -0
- data/pathfinding/build.sh +6 -0
- data/pathfinding/build.txt +16 -0
- data/pathfinding/crowd.cpp +194 -0
- data/pathfinding/include/astar.h +49 -0
- data/pathfinding/include/common.h +5 -0
- data/pathfinding/include/crowd.h +43 -0
- data/pathfinding/include/micropather.h +511 -0
- data/pathfinding/include/navmesh.h +114 -0
- data/pathfinding/include/pathfinder.h +24 -0
- data/pathfinding/main.cpp +108 -17
- data/pathfinding/micropather.cpp +1062 -0
- data/pathfinding/navmesh.cpp +408 -0
- data/pathfinding/overrides/DetourCrowd.cpp +1446 -0
- data/pathfinding/overrides/DetourNavMeshQuery.cpp +3551 -0
- data/pathfinding/overrides/DetourNavMeshQuery.h +538 -0
- data/pathfinding/pathfinder.cpp +117 -0
- data/pathfinding/{bin → premake}/premake4 +0 -0
- data/pathfinding/premake/premake4.exe +0 -0
- data/pathfinding/premake4.lua +12 -3
- data/spec/actor/actor_spec.rb +0 -7
- data/spec/client_manager_spec.rb +1 -1
- data/spec/couchproxy_spec.rb +38 -0
- data/spec/entity_persistence_spec.rb +129 -0
- data/spec/game_systems/team_manager_spec.rb +2 -2
- data/spec/hashring_spec.rb +17 -39
- data/spec/java_grid_spec.rb +0 -2
- data/spec/misc_spec.rb +111 -0
- data/spec/mono_spec.rb +50 -3
- data/spec/reliable_message_spec.rb +38 -0
- data/spec/spec_helper.rb +4 -4
- data/spec/spec_helper_minimal.rb +10 -0
- data/web/app.rb +108 -86
- data/web/config/trinidad.yml +1 -0
- data/web/views/add_player.erb +25 -0
- data/web/views/index.erb +0 -0
- data/web/views/layout.erb +48 -0
- data/web/views/login.erb +25 -0
- data/web/views/players.erb +24 -0
- metadata +209 -94
- data/config/config.example.yml +0 -100
- data/config/regions.example.yml +0 -9
- data/games/example/lib/authentication_handler.rb +0 -69
- data/games/models.rb +0 -3
- data/games/models/clan_member.rb +0 -8
- data/games/models/clan_profile.rb +0 -9
- data/games/models/player.rb +0 -7
- data/games/plugins.rb +0 -1
- data/games/plugins/team_handler.rb +0 -49
- data/games/preload.rb +0 -13
- data/java/.gitignore +0 -1
- data/java/build.gradle +0 -95
- data/java/component.erb +0 -396
- data/java/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/java/src/main/java/com/game_machine/core/GameMachineLoader.java +0 -25
- data/java/src/main/java/com/game_machine/core/Grid.java +0 -195
- data/java/src/main/resources/logback.xml +0 -14
- data/java/src/main/resources/logging.properties +0 -3
- data/lib/game_machine/actor/mono_actor.rb +0 -89
- data/lib/game_machine/auth_handlers/base.rb +0 -21
- data/lib/game_machine/auth_handlers/public.rb +0 -34
- data/lib/game_machine/endpoints/mono_gateway.rb +0 -87
- data/lib/game_machine/endpoints/tcp.rb +0 -51
- data/lib/game_machine/endpoints/tcp_handler.rb +0 -75
- data/lib/game_machine/endpoints/udp.rb +0 -88
- data/lib/game_machine/game_loader.rb +0 -46
- data/lib/game_machine/game_systems/region_settings.rb +0 -13
- data/lib/game_machine/hashring.rb +0 -48
- data/lib/game_machine/settings.rb +0 -11
- data/mono/server/actor.cs +0 -37
- data/mono/server/iactor.cs +0 -11
- data/mono/server/message_util.cs +0 -29
- data/mono/server/proxy_client.cs +0 -73
- data/mono/server/proxy_server.cs +0 -30
- data/mono/server/test_actor.cs +0 -33
- data/pathfinding/include/pathfind.h +0 -167
- data/pathfinding/pathfind.cpp +0 -174
- data/pathfinding/pathfinder.cs +0 -66
- data/script/server.sh +0 -109
- data/script/watch.sh +0 -11
- data/spec/commands/navigation_commands_spec.rb +0 -51
- data/spec/game_systems/entity_tracking_spec.rb +0 -64
- data/spec/navigation/detour_navmesh_spec.rb +0 -34
- data/spec/navigation/detour_path_spec.rb +0 -25
- data/spec/udp_server_spec.rb +0 -10
- data/web/controllers/auth_controller.rb +0 -19
- data/web/controllers/base_controller.rb +0 -16
- data/web/controllers/index_controller.rb +0 -7
- data/web/controllers/log_controller.rb +0 -47
- data/web/controllers/messages_controller.rb +0 -59
- data/web/controllers/player_register_controller.rb +0 -15
- data/web/views/game_messages.haml +0 -45
- data/web/views/index.haml +0 -6
- data/web/views/layout.haml +0 -41
- data/web/views/logs.haml +0 -32
- data/web/views/player_register.haml +0 -22
- data/web/views/player_registered.haml +0 -2
- data/web/views/register_layout.haml +0 -22
- data/web/views/restart.haml +0 -35
@@ -0,0 +1,26 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
|
3
|
+
import akka.actor.UntypedActor;
|
4
|
+
import akka.event.Logging;
|
5
|
+
import akka.event.LoggingAdapter;
|
6
|
+
|
7
|
+
|
8
|
+
public class MessagePersister extends UntypedActor {
|
9
|
+
|
10
|
+
public static String name = "message_persister";
|
11
|
+
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
12
|
+
|
13
|
+
@Override
|
14
|
+
public void onReceive(Object message) throws Exception {
|
15
|
+
|
16
|
+
if (message instanceof PersistentMessage) {
|
17
|
+
PersistentMessage persistentMessage = (PersistentMessage)message;
|
18
|
+
String action = persistentMessage.getPersistAction();
|
19
|
+
if (action.equals("save")) {
|
20
|
+
persistentMessage.dbSave(persistentMessage.getPersistPlayerId());
|
21
|
+
} else if (action.equals("delete")) {
|
22
|
+
persistentMessage.dbDelete(persistentMessage.getPersistPlayerId());
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
|
3
|
+
import GameMachine.Messages.Entity;
|
4
|
+
import GameMachine.Messages.MonoMessage;
|
5
|
+
|
6
|
+
public class MonoProxy {
|
7
|
+
|
8
|
+
private static class LazyHolder {
|
9
|
+
private static final MonoProxy INSTANCE = new MonoProxy();
|
10
|
+
}
|
11
|
+
|
12
|
+
public static MonoProxy getInstance() {
|
13
|
+
return LazyHolder.INSTANCE;
|
14
|
+
}
|
15
|
+
|
16
|
+
private MonoProxy() {
|
17
|
+
udpClient = new UdpClient(24320,1);
|
18
|
+
if (!udpClient.connect()) {
|
19
|
+
throw new RuntimeException("Unable to connect to mono");
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
private UdpClient udpClient;
|
24
|
+
|
25
|
+
|
26
|
+
public Entity call(String klass, Entity entity) {
|
27
|
+
MonoMessage message = new MonoMessage();
|
28
|
+
message.setKlass(klass);
|
29
|
+
message.setEntity(entity);
|
30
|
+
byte[] bytes = udpClient.send(message.toByteArray());
|
31
|
+
if (bytes == null) {
|
32
|
+
return null;
|
33
|
+
} else {
|
34
|
+
message = MonoMessage.parseFrom(bytes);
|
35
|
+
return message.entity;
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
}
|
@@ -1,5 +1,9 @@
|
|
1
1
|
package com.game_machine.core;
|
2
2
|
|
3
|
+
import java.net.InetSocketAddress;
|
4
|
+
|
5
|
+
import GameMachine.Messages.ClientMessage;
|
6
|
+
import io.netty.buffer.ByteBuf;
|
3
7
|
import io.netty.channel.ChannelHandlerContext;
|
4
8
|
|
5
9
|
public class NetMessage {
|
@@ -9,20 +13,20 @@ public class NetMessage {
|
|
9
13
|
public static final int UDT = 1;
|
10
14
|
public static final int TCP = 2;
|
11
15
|
|
12
|
-
public
|
16
|
+
public byte[] bytes;
|
17
|
+
public ByteBuf buf;
|
18
|
+
public ClientMessage clientMessage;
|
13
19
|
public final String host;
|
14
20
|
public final int port;
|
15
21
|
public final int protocol;
|
16
|
-
public final String clientId;
|
17
22
|
public final ChannelHandlerContext ctx;
|
23
|
+
public InetSocketAddress address;
|
18
24
|
|
19
|
-
public NetMessage(
|
20
|
-
this.clientId = clientId;
|
21
|
-
this.bytes = bytes;
|
25
|
+
public NetMessage(int protocol, String host, int port, ChannelHandlerContext ctx) {
|
22
26
|
this.host = host;
|
23
27
|
this.port = port;
|
24
28
|
this.protocol = protocol;
|
25
29
|
this.ctx = ctx;
|
26
30
|
}
|
27
|
-
|
31
|
+
|
28
32
|
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
|
3
|
+
import akka.actor.ActorSelection;
|
4
|
+
import GameMachine.Messages.Entity;
|
5
|
+
import GameMachine.Messages.GameMessage;
|
6
|
+
import GameMachine.Messages.GameMessages;
|
7
|
+
import GameMachine.Messages.Player;
|
8
|
+
|
9
|
+
public class PlayerCommands {
|
10
|
+
|
11
|
+
public static void sendGameMessage(GameMessage gameMessage, String playerId) {
|
12
|
+
GameMessages gameMessages = new GameMessages();
|
13
|
+
gameMessages.addGameMessage(gameMessage);
|
14
|
+
Entity entity = new Entity();
|
15
|
+
entity.setId("0");
|
16
|
+
entity.setGameMessages(gameMessages);
|
17
|
+
PlayerCommands.sendToPlayer(entity, playerId);
|
18
|
+
}
|
19
|
+
|
20
|
+
public static void sendToPlayer(Entity entity, String playerId) {
|
21
|
+
if (!entity.hasPlayer()) {
|
22
|
+
Player player = new Player();
|
23
|
+
player.setId(playerId);
|
24
|
+
entity.setPlayer(player);
|
25
|
+
}
|
26
|
+
entity.setSendToPlayer(true);
|
27
|
+
|
28
|
+
ActorSelection sel = ActorUtil.getSelectionByName("GameMachine::ClientManager");
|
29
|
+
sel.tell(entity, null);
|
30
|
+
}
|
31
|
+
}
|
@@ -0,0 +1,100 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
import java.net.InetSocketAddress;
|
3
|
+
import java.security.cert.CertificateException;
|
4
|
+
|
5
|
+
import javax.net.ssl.SSLException;
|
6
|
+
|
7
|
+
import org.slf4j.Logger;
|
8
|
+
import org.slf4j.LoggerFactory;
|
9
|
+
|
10
|
+
import io.netty.bootstrap.ServerBootstrap;
|
11
|
+
import io.netty.channel.ChannelOption;
|
12
|
+
import io.netty.channel.EventLoopGroup;
|
13
|
+
import io.netty.channel.nio.NioEventLoopGroup;
|
14
|
+
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
15
|
+
import io.netty.handler.logging.LogLevel;
|
16
|
+
import io.netty.handler.logging.LoggingHandler;
|
17
|
+
import io.netty.handler.ssl.SslContext;
|
18
|
+
import io.netty.handler.ssl.util.SelfSignedCertificate;
|
19
|
+
|
20
|
+
public class TcpServer implements Runnable {
|
21
|
+
static final boolean SSL = System.getProperty("nossl") != null;
|
22
|
+
static final int PORT = Integer.parseInt(System.getProperty("port", "8463"));
|
23
|
+
|
24
|
+
private static final Logger log = LoggerFactory.getLogger(UdpServer.class);
|
25
|
+
private static Thread serverThread;
|
26
|
+
|
27
|
+
private static TcpServer tcpServer;
|
28
|
+
|
29
|
+
private final String hostname;
|
30
|
+
private final int port;
|
31
|
+
|
32
|
+
public static TcpServer getTcpServer() {
|
33
|
+
return tcpServer;
|
34
|
+
}
|
35
|
+
|
36
|
+
public static void start(String host, Integer port) {
|
37
|
+
|
38
|
+
// Don't try to start an already running server
|
39
|
+
if (tcpServer != null) {
|
40
|
+
return;
|
41
|
+
}
|
42
|
+
|
43
|
+
tcpServer = new TcpServer(host, port);
|
44
|
+
serverThread = new Thread(tcpServer);
|
45
|
+
serverThread.start();
|
46
|
+
}
|
47
|
+
|
48
|
+
public static void stop() {
|
49
|
+
log.info("Stopping TCP server");
|
50
|
+
// Don't try to stop a server that's not running
|
51
|
+
if (tcpServer == null) {
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
public TcpServer(final String hostname, final int port) {
|
58
|
+
this.port = port;
|
59
|
+
this.hostname = hostname;
|
60
|
+
}
|
61
|
+
|
62
|
+
@Override
|
63
|
+
public void run() {
|
64
|
+
// Configure SSL.
|
65
|
+
final SslContext sslCtx;
|
66
|
+
if (SSL) {
|
67
|
+
SelfSignedCertificate ssc;
|
68
|
+
try {
|
69
|
+
ssc = new SelfSignedCertificate();
|
70
|
+
sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
|
71
|
+
} catch (CertificateException | SSLException e) {
|
72
|
+
e.printStackTrace();
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
} else {
|
76
|
+
sslCtx = null;
|
77
|
+
}
|
78
|
+
|
79
|
+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
80
|
+
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
81
|
+
try {
|
82
|
+
ServerBootstrap b = new ServerBootstrap();
|
83
|
+
b.group(bossGroup, workerGroup)
|
84
|
+
.channel(NioServerSocketChannel.class)
|
85
|
+
.childOption(ChannelOption.TCP_NODELAY, true)
|
86
|
+
.childOption(ChannelOption.SO_REUSEADDR, true)
|
87
|
+
.handler(new LoggingHandler(LogLevel.INFO))
|
88
|
+
.childHandler(new TcpServerInitializer(sslCtx));
|
89
|
+
|
90
|
+
InetSocketAddress address = new InetSocketAddress(hostname,port);
|
91
|
+
b.bind(address).sync().channel().closeFuture().sync();
|
92
|
+
} catch (InterruptedException e) {
|
93
|
+
e.printStackTrace();
|
94
|
+
} finally {
|
95
|
+
bossGroup.shutdownGracefully();
|
96
|
+
workerGroup.shutdownGracefully();
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
import java.net.InetSocketAddress;
|
3
|
+
|
4
|
+
import org.slf4j.Logger;
|
5
|
+
import org.slf4j.LoggerFactory;
|
6
|
+
|
7
|
+
import GameMachine.Messages.ClientMessage;
|
8
|
+
import akka.actor.ActorSelection;
|
9
|
+
import io.netty.channel.ChannelHandlerContext;
|
10
|
+
import io.netty.channel.SimpleChannelInboundHandler;
|
11
|
+
|
12
|
+
public class TcpServerHandler extends SimpleChannelInboundHandler<ClientMessage> {
|
13
|
+
|
14
|
+
private ActorSelection inbound;
|
15
|
+
|
16
|
+
private static final Logger log = LoggerFactory
|
17
|
+
.getLogger(TcpServerHandler.class);
|
18
|
+
|
19
|
+
public TcpServerHandler() {
|
20
|
+
this.inbound = ActorUtil
|
21
|
+
.getSelectionByName("message_gateway");
|
22
|
+
}
|
23
|
+
|
24
|
+
public static void sendClientMessage(ClientMessage clientMessage,ChannelHandlerContext ctx) {
|
25
|
+
ctx.write(clientMessage);
|
26
|
+
ctx.flush();
|
27
|
+
}
|
28
|
+
|
29
|
+
@Override
|
30
|
+
public void channelRead0(ChannelHandlerContext ctx, ClientMessage clientMessage) throws Exception {
|
31
|
+
InetSocketAddress address = (InetSocketAddress)ctx.channel().remoteAddress();
|
32
|
+
|
33
|
+
NetMessage netMessage = new NetMessage(NetMessage.TCP, address.getHostString(), address.getPort(), ctx);
|
34
|
+
netMessage.clientMessage = clientMessage;
|
35
|
+
this.inbound.tell(netMessage, null);
|
36
|
+
}
|
37
|
+
|
38
|
+
@Override
|
39
|
+
public void channelActive(final ChannelHandlerContext ctx) {
|
40
|
+
log.info("Tcp server active");
|
41
|
+
}
|
42
|
+
|
43
|
+
@Override
|
44
|
+
public void channelReadComplete(ChannelHandlerContext ctx) {
|
45
|
+
ctx.flush();
|
46
|
+
}
|
47
|
+
|
48
|
+
@Override
|
49
|
+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
50
|
+
//cause.printStackTrace();
|
51
|
+
ctx.close();
|
52
|
+
}
|
53
|
+
|
54
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
import io.netty.channel.ChannelInitializer;
|
3
|
+
import io.netty.channel.ChannelPipeline;
|
4
|
+
import io.netty.channel.socket.SocketChannel;
|
5
|
+
import io.netty.handler.codec.protobuf.ProtobufEncoder;
|
6
|
+
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
|
7
|
+
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
|
8
|
+
import io.netty.handler.ssl.SslContext;
|
9
|
+
|
10
|
+
public class TcpServerInitializer extends ChannelInitializer<SocketChannel> {
|
11
|
+
private final SslContext sslCtx;
|
12
|
+
|
13
|
+
public TcpServerInitializer(SslContext sslCtx) {
|
14
|
+
this.sslCtx = sslCtx;
|
15
|
+
}
|
16
|
+
|
17
|
+
@Override
|
18
|
+
public void initChannel(SocketChannel ch) throws Exception {
|
19
|
+
ChannelPipeline p = ch.pipeline();
|
20
|
+
if (sslCtx != null) {
|
21
|
+
p.addLast(sslCtx.newHandler(ch.alloc()));
|
22
|
+
}
|
23
|
+
|
24
|
+
p.addLast(new ProtobufVarint32FrameDecoder());
|
25
|
+
p.addLast(new ClientMessageDecoder());
|
26
|
+
|
27
|
+
//p.addLast(new ProtobufVarint32LengthFieldPrepender());
|
28
|
+
p.addLast(new ClientMessageEncoder());
|
29
|
+
|
30
|
+
p.addLast(new TcpServerHandler());
|
31
|
+
}
|
32
|
+
}
|
@@ -0,0 +1,86 @@
|
|
1
|
+
package com.game_machine.core;
|
2
|
+
|
3
|
+
import java.io.IOException;
|
4
|
+
import java.net.DatagramPacket;
|
5
|
+
import java.net.DatagramSocket;
|
6
|
+
import java.net.InetAddress;
|
7
|
+
import java.net.SocketException;
|
8
|
+
import java.net.SocketTimeoutException;
|
9
|
+
import java.util.Arrays;
|
10
|
+
|
11
|
+
import org.slf4j.Logger;
|
12
|
+
import org.slf4j.LoggerFactory;
|
13
|
+
|
14
|
+
public class UdpClient {
|
15
|
+
|
16
|
+
private static final Logger log = LoggerFactory.getLogger(UdpClient.class);
|
17
|
+
private int port;
|
18
|
+
private InetAddress hostAddress;
|
19
|
+
private DatagramSocket s;
|
20
|
+
private DatagramPacket dp;
|
21
|
+
private int timeout;
|
22
|
+
|
23
|
+
public UdpClient(int port, int timeout) {
|
24
|
+
this.port = port;
|
25
|
+
this.timeout = timeout;
|
26
|
+
|
27
|
+
hostAddress = InetAddress.getLoopbackAddress();
|
28
|
+
byte[] in = new byte[2048];
|
29
|
+
dp = new DatagramPacket(in, in.length);
|
30
|
+
}
|
31
|
+
|
32
|
+
public Boolean connect() {
|
33
|
+
|
34
|
+
try {
|
35
|
+
s = new DatagramSocket();
|
36
|
+
s.setSoTimeout(timeout);
|
37
|
+
return true;
|
38
|
+
} catch (SocketException e) {
|
39
|
+
e.printStackTrace();
|
40
|
+
return false;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
public byte[] send(byte[] bytes) {
|
45
|
+
try {
|
46
|
+
|
47
|
+
int retryCount = 0;
|
48
|
+
int retryLimit = 3;
|
49
|
+
|
50
|
+
DatagramPacket out = new DatagramPacket(bytes, bytes.length,
|
51
|
+
hostAddress, port);
|
52
|
+
|
53
|
+
|
54
|
+
while (true) {
|
55
|
+
s.send(out);
|
56
|
+
|
57
|
+
try {
|
58
|
+
s.receive(dp);
|
59
|
+
byte[] received = Arrays.copyOfRange(dp.getData(),
|
60
|
+
dp.getOffset(), dp.getOffset() + dp.getLength());
|
61
|
+
return received;
|
62
|
+
} catch (SocketTimeoutException e) {
|
63
|
+
try {
|
64
|
+
Thread.sleep(retryCount + 1, 0);
|
65
|
+
} catch (InterruptedException e1) {
|
66
|
+
e1.printStackTrace();
|
67
|
+
}
|
68
|
+
if (retryCount > retryLimit) {
|
69
|
+
log.warn("Timeout retry limit exceeded " + e + " "
|
70
|
+
+ retryCount);
|
71
|
+
return null;
|
72
|
+
}
|
73
|
+
retryCount++;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
} catch (SocketException e1) {
|
78
|
+
log.warn("Socket closed " + e1);
|
79
|
+
return null;
|
80
|
+
|
81
|
+
} catch (IOException e) {
|
82
|
+
e.printStackTrace();
|
83
|
+
return null;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
@@ -1,23 +1,16 @@
|
|
1
1
|
package com.game_machine.core;
|
2
2
|
|
3
|
-
import org.slf4j.Logger;
|
4
|
-
import org.slf4j.LoggerFactory;
|
5
|
-
|
6
3
|
import io.netty.bootstrap.Bootstrap;
|
7
|
-
import io.netty.channel.ChannelFuture;
|
8
4
|
import io.netty.channel.ChannelHandlerContext;
|
9
|
-
import io.netty.channel.ChannelInitializer;
|
10
5
|
import io.netty.channel.ChannelOption;
|
11
|
-
import io.netty.channel.ChannelPipeline;
|
12
6
|
import io.netty.channel.EventLoopGroup;
|
13
|
-
import io.netty.channel.group.ChannelGroup;
|
14
|
-
import io.netty.channel.group.ChannelGroupFuture;
|
15
|
-
import io.netty.channel.group.DefaultChannelGroup;
|
16
7
|
import io.netty.channel.nio.NioEventLoopGroup;
|
17
8
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
18
|
-
import io.netty.util.concurrent.DefaultEventExecutorGroup;
|
19
9
|
|
20
|
-
import
|
10
|
+
import java.net.InetSocketAddress;
|
11
|
+
|
12
|
+
import org.slf4j.Logger;
|
13
|
+
import org.slf4j.LoggerFactory;
|
21
14
|
|
22
15
|
public final class UdpServer implements Runnable {
|
23
16
|
|
@@ -31,7 +24,6 @@ public final class UdpServer implements Runnable {
|
|
31
24
|
|
32
25
|
private final UdpServerHandler handler;
|
33
26
|
|
34
|
-
|
35
27
|
public static UdpServer getUdpServer() {
|
36
28
|
return udpServer;
|
37
29
|
}
|
@@ -68,30 +60,29 @@ public final class UdpServer implements Runnable {
|
|
68
60
|
}
|
69
61
|
|
70
62
|
public void run() {
|
71
|
-
log.info("Starting UdpServer port=" + this.port + " hostname="
|
63
|
+
log.info("Starting UdpServer port=" + this.port + " hostname="
|
64
|
+
+ this.hostname);
|
72
65
|
Thread.currentThread().setName("udp-server");
|
73
66
|
EventLoopGroup group = new NioEventLoopGroup();
|
74
67
|
try {
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
68
|
+
Bootstrap boot = new Bootstrap();
|
69
|
+
boot.channel(NioDatagramChannel.class);
|
70
|
+
boot.group(group);
|
71
|
+
boot.option(ChannelOption.SO_BROADCAST, false);
|
72
|
+
boot.handler(new UdpServerHandler());
|
73
|
+
|
74
|
+
InetSocketAddress address = new InetSocketAddress(hostname,port);
|
75
|
+
boot.bind(address).sync().channel().closeFuture().await();
|
76
|
+
|
83
77
|
} catch (InterruptedException e) {
|
84
|
-
// TODO Auto-generated catch block
|
85
78
|
e.printStackTrace();
|
86
79
|
} finally {
|
87
|
-
|
80
|
+
group.shutdownGracefully();
|
88
81
|
}
|
89
82
|
}
|
90
83
|
|
91
|
-
public void sendToClient(byte[] bytes,
|
92
|
-
handler.send(
|
84
|
+
public void sendToClient(InetSocketAddress address, byte[] bytes, ChannelHandlerContext ctx) {
|
85
|
+
handler.send(address, bytes, ctx);
|
93
86
|
}
|
94
87
|
|
95
|
-
|
96
|
-
|
97
88
|
}
|