Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Networking packets test #45

Draft
wants to merge 5 commits into
base: airships
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added airships/assets/scenes/menu/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions airships/assets/scenes/menu/background.png.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "1b6dd65e-7b2c-40c6-8612-af22e6e0a9d1"
}
8 changes: 8 additions & 0 deletions airships/assets/scenes/menu/menu.cubos
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"entities": {
"render-target": {
"cubos::engine::RenderTargetDefaults": {},
"cubos::engine::GizmosTarget": {},
},
}
}
3 changes: 3 additions & 0 deletions airships/assets/scenes/menu/menu.cubos.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "21e4d65f-ca53-4edc-9144-a3df8a131967"
}
Binary file added airships/assets/scenes/menu/title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions airships/assets/scenes/menu/title.png.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "a04bb3d8-7eac-41fe-b69d-cf167dd7fd90"
}
1 change: 1 addition & 0 deletions airships/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

add_executable(airships-client
src/main.cpp
src/menu/plugin.cpp
)

target_link_libraries(airships-client airships-common)
Expand Down
16 changes: 10 additions & 6 deletions airships/client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include <cubos/engine/defaults/plugin.hpp>
#include <cubos/engine/tools/plugin.hpp>
#include <cubos/engine/utils/free_camera/plugin.hpp>
#include <cubos/engine/ui/image/plugin.hpp>
#include "menu/plugin.hpp"

#include <common/empty.hpp> // TODO: here just to serve as an example
// #include <common/empty.hpp> // TODO: here just to serve as an example

using namespace cubos::engine;

Expand All @@ -23,6 +25,8 @@ int main(int argc, char** argv)
cubos.plugin(defaultsPlugin);
cubos.plugin(freeCameraPlugin);
cubos.plugin(toolsPlugin);
cubos.plugin(uiImagePlugin);
cubos.plugin(airships::client::menuPlugin);

// Add game plugins
// TODO
Expand All @@ -46,11 +50,11 @@ int main(int argc, char** argv)
environment.skyGradient[1] = {0.25F, 0.65F, 1.0F};
});

cubos.startupSystem("load and spawn the Main Scene")
.tagged(assetsTag)
.call([](Commands cmds, const Assets& assets, Settings& settings) {
cmds.spawn(assets.read(MainSceneAsset)->blueprint);
});
// cubos.startupSystem("load and spawn the Main Scene")
// .tagged(assetsTag)
// .call([](Commands cmds, const Assets& assets, Settings& settings) {
// cmds.spawn(assets.read(MainSceneAsset)->blueprint);
// });

cubos.run();
}
52 changes: 52 additions & 0 deletions airships/client/src/menu/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "plugin.hpp"
#include <cubos/engine/assets/plugin.hpp>
#include <cubos/engine/render/target/target.hpp>
#include <cubos/engine/scene/scene.hpp>
#include <cubos/engine/transform/child_of.hpp>
#include <cubos/engine/ui/canvas/canvas.hpp>
#include <cubos/engine/ui/image/image.hpp>
#include <cubos/engine/ui/canvas/plugin.hpp>
#include <cubos/engine/ui/image/plugin.hpp>
#include <cubos/engine/ui/text/plugin.hpp>
#include <cubos/engine/ui/canvas/element.hpp>
#include <cubos/engine/render/target/plugin.hpp>
#include <cubos/engine/window/plugin.hpp>
#include <cubos/engine/settings/plugin.hpp>

using namespace cubos::engine;

// static const Asset<Scene> MenuSceneAsset = AnyAsset("21e4d65f-ca53-4edc-9144-a3df8a131967");
// static const Asset<Image> BackgroundAsset = AnyAsset("1b6dd65e-7b2c-40c6-8612-af22e6e0a9d1");
// static const Asset<Image> TitleAsset = AnyAsset("a04bb3d8-7eac-41fe-b69d-cf167dd7fd90");

void airships::client::menuPlugin(Cubos& cubos)
{
cubos.depends(assetsPlugin);
cubos.depends(uiCanvasPlugin);
cubos.depends(uiImagePlugin);
cubos.depends(uiTextPlugin);
cubos.depends(renderTargetPlugin);
cubos.depends(settingsPlugin);
cubos.depends(windowPlugin);

cubos.startupSystem("load ui").after(windowInitTag).after(settingsTag).call([](Commands cmds) {
auto canvas = cmds.create().add(UICanvas{}).add(RenderTarget{}).entity();
auto background = cmds.create()
.add(UIElement{})
.add(UIImage{.asset = AnyAsset("1b6dd65e-7b2c-40c6-8612-af22e6e0a9d1")})
.entity();
cmds.relate(background, canvas, ChildOf{});
});

// cubos.startupSystem("load and spawn main menu").tagged(assetsTag).call([](Commands cmds, Assets& assets) {
// auto bp = assets.read(MenuSceneAsset)->blueprint;
// for (auto& ent : bp.entities())
// {
// if (ent.second == "background")
// {
// bp.add(ent.first, UIImage{.asset = BackgroundAsset});
// }
// }
// cmds.spawn(bp);
// });
}
8 changes: 8 additions & 0 deletions airships/client/src/menu/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <cubos/engine/prelude.hpp>

namespace airships::client
{
void menuPlugin(cubos::engine::Cubos& cubos);
}
7 changes: 6 additions & 1 deletion airships/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
# Airships common library build configuration

add_library(airships-common
src/empty.cpp

# Client->Server packets
"src/packets/ctos/join.cpp"

# Server->Client packets
"src/packets/stoc/disconnect.cpp"
)

target_link_libraries(airships-common PUBLIC cubos::engine)
Expand Down
3 changes: 0 additions & 3 deletions airships/common/include/common/empty.hpp

This file was deleted.

14 changes: 14 additions & 0 deletions airships/common/include/common/packets/ctos/join.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <cubos/core/reflection/reflect.hpp>
#include <string>

namespace airships::common
{
struct JoinPacket
{
CUBOS_REFLECT;

std::string name;
};
} // namespace airships::common
14 changes: 14 additions & 0 deletions airships/common/include/common/packets/stoc/disconnect.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <cubos/core/reflection/reflect.hpp>
#include <string>

namespace airships::common
{
struct ServerDisconnectPacket
{
CUBOS_REFLECT;

std::string reason;
};
} // namespace airships::common
10 changes: 10 additions & 0 deletions airships/common/include/common/packets/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace airships::common
{
enum class PacketType
{
JOIN = 1,
SERVER_DISCONNECT = 2,
};
} // namespace airships::common
3 changes: 0 additions & 3 deletions airships/common/src/empty.cpp

This file was deleted.

12 changes: 12 additions & 0 deletions airships/common/src/packets/ctos/join.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <common/packets/ctos/join.hpp>
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/string.hpp>

using namespace airships::common;

CUBOS_REFLECT_IMPL(JoinPacket)
{
return cubos::core::ecs::TypeBuilder<JoinPacket>("airships::common::JoinPacket")
.withField("name", &JoinPacket::name)
.build();
}
12 changes: 12 additions & 0 deletions airships/common/src/packets/stoc/disconnect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <common/packets/stoc/disconnect.hpp>
#include <cubos/core/ecs/reflection.hpp>
#include <cubos/core/reflection/external/string.hpp>

using namespace airships::common;

CUBOS_REFLECT_IMPL(ServerDisconnectPacket)
{
return cubos::core::ecs::TypeBuilder<ServerDisconnectPacket>("airships::common::ServerDisconnectPacket")
.withField("reason", &ServerDisconnectPacket::reason)
.build();
}
12 changes: 11 additions & 1 deletion airships/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
# Airships server build configuration

add_executable(airships-server
src/main.cpp
"src/main.cpp"

"src/network/plugin.cpp"

"src/network/in/join.cpp"

"src/game_server/plugin.cpp"
"src/game_server/game_server.cpp"

"src/player/player.cpp"
"src/player/plugin.cpp"
)

target_link_libraries(airships-server airships-common)
Expand Down
11 changes: 11 additions & 0 deletions airships/server/src/events/join.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cubos/core/ecs/entity/entity.hpp>

namespace airships::server
{
struct JoinEvent
{
cubos::core::ecs::Entity entity;
};
} // namespace airships::server
13 changes: 13 additions & 0 deletions airships/server/src/events/login.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <string>
#include <cubos/core/ecs/entity/entity.hpp>

namespace airships::server
{
struct LoginEvent
{
cubos::core::ecs::Entity entity;
std::string name;
};
} // namespace airships::server
9 changes: 9 additions & 0 deletions airships/server/src/events/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "plugin.hpp"
#include "join.hpp"
#include "login.hpp"

void airships::server::eventsPlugin(cubos::engine::Cubos& cubos)
{
cubos.event<LoginEvent>();
cubos.event<JoinEvent>();
}
8 changes: 8 additions & 0 deletions airships/server/src/events/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <cubos/engine/prelude.hpp>

namespace airships::server
{
void eventsPlugin(cubos::engine::Cubos& cubos);
}
27 changes: 27 additions & 0 deletions airships/server/src/game_server/game_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "game_server.hpp"

namespace airships::server
{
GameServer::GameServer(uint16_t port) : mListener(cubos::core::net::TcpListener{})
{
mListener.listen(cubos::core::net::Address::Any, port);
mListener.setBlocking(false);
}

const cubos::core::net::TcpListener& GameServer::listener() const
{
return mListener;
}

std::chrono::milliseconds GameServer::tickStart() const
{
return mTickStart;
}

void GameServer::startTick()
{
using namespace std::chrono;
mTickStart = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
}

} // namespace airships::server
28 changes: 28 additions & 0 deletions airships/server/src/game_server/game_server.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <cubos/engine/prelude.hpp>
#include <cubos/core/net/tcp_listener.hpp>
#include <cubos/core/net/address.hpp>

namespace airships::server
{
class GameServer
{
public:
CUBOS_ANONYMOUS_REFLECT(GameServer);

GameServer(uint16_t port);

const cubos::core::net::TcpListener& listener() const;

void connect(std::shared_ptr<cubos::core::net::TcpStream> client);

std::chrono::milliseconds tickStart() const;

void startTick();

private:
cubos::core::net::TcpListener mListener;
std::chrono::milliseconds mTickStart;
};
} // namespace airships::server
58 changes: 58 additions & 0 deletions airships/server/src/game_server/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "plugin.hpp"
#include <cubos/engine/fixed_step/plugin.hpp>
#include <cubos/engine/fixed_step/fixed_delta_time.hpp>
#include <cubos/core/reflection/external/vector.hpp>
#include <cubos/engine/settings/plugin.hpp>
#include <cubos/core/net/tcp_listener.hpp>
#include <cubos/core/reflection/external/primitives.hpp>
#include "game_server.hpp"

static constexpr uint16_t DefaultPort = 26001;
static constexpr float DefaultTickrate = 1.0F / 1.0F;

CUBOS_DEFINE_TAG(airships::server::serverTickStartTag);
CUBOS_DEFINE_TAG(airships::server::serverTickTag);
CUBOS_DEFINE_TAG(airships::server::serverTickEndTag);

namespace airships::server
{
void gameServerPlugin(cubos::engine::Cubos& cubos)
{
cubos.depends(cubos::engine::fixedStepPlugin);
cubos.depends(cubos::engine::settingsPlugin);

cubos.tag(serverTickStartTag).tagged(cubos::engine::fixedStepTag);
cubos.tag(serverTickTag).tagged(cubos::engine::fixedStepTag).after(serverTickStartTag);
cubos.tag(serverTickEndTag).tagged(cubos::engine::fixedStepTag).after(serverTickTag);

cubos.uninitResource<GameServer>();

cubos.startupSystem("setup server")
.call([](cubos::engine::Commands cmds, cubos::engine::Settings& settings, cubos::engine::FixedDeltaTime& dt,
cubos::engine::ShouldQuit& quit) {
auto port = static_cast<uint16_t>(settings.getInteger("airships.server.port", DefaultPort));
auto tickrate = static_cast<float>(settings.getDouble("airships.server.tickrate", DefaultTickrate));
dt.value = tickrate;
quit.value = false;
cmds.emplaceResource<GameServer>(port);
});

cubos.system("start server tick").tagged(serverTickStartTag).call([](GameServer& server) {
server.startTick();
CUBOS_INFO("STARTED SERVER TICK");
});

cubos.system("end server tick")
.tagged(serverTickEndTag)
.call([](GameServer& server, cubos::engine::FixedDeltaTime& dt) {
auto end = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
auto elapsed = end - server.tickStart();
if (elapsed.count() > dt.value)
{
CUBOS_WARN("Tick took longer than expected: {} ms", elapsed.count());
}
CUBOS_INFO("ENDED SERVER TICK");
});
}
} // namespace airships::server
Loading