Skip to content

Commit

Permalink
server switcher + some big refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Dec 5, 2023
1 parent 70c76f1 commit 067b53d
Show file tree
Hide file tree
Showing 84 changed files with 1,394 additions and 540 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

# Enable LTO (2.5x less binary size, no noticable compile time hit)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

project(globed2 VERSION 1.0.0)

option(ENABLE_DEBUG "Debug mode" OFF)
if (ENABLE_DEBUG)
add_compile_definitions(GLOBED_DEBUG=1)
else()
# Enable LTO in release (2.5x less binary size, no noticable compile time hit)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

file(GLOB_RECURSE SOURCES
Expand Down
11 changes: 4 additions & 7 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[workspace]
members = ["central", "game", "game-derives", "shared"]
members = ["central", "game", "game-derive", "shared"]
resolver = "2"
# my observation thus far with LTO:
# compile times -> ~100% increase
# executable size -> ~30% decrease
# performance -> too lazy to benchmark but probably a very minor improvement
# so.. good enough to keep!
# TODO Bring back in release

# TODO bring back in release
[profile.release]
# lto = "fat"
4 changes: 2 additions & 2 deletions server/central/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ base64 = "0.21.5"
blake2 = "0.10.6"
digest = "0.10.7"
hmac = "0.12.1"
ipnet = "2.9.0"
iprange = "0.6.7"
rand = "0.8.5"
reqwest = "0.11.22"
roa = { version = "0.6.1", features = ["router"] }
Expand All @@ -23,5 +25,3 @@ serde_json = "1.0.108"
sha2 = "0.10.8"
tokio = { version = "1.34.0", features = ["full"] }
totp-rs = "5.4.0"
iprange = "0.6.7"
ipnet = "2.9.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "globed-derives"
name = "globed-derive"
version = "1.0.0"
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ pub fn derive_encodable(input: TokenStream) -> TokenStream {
.fields
.iter()
.map(|field| {
let ident = field.ident.as_ref().unwrap();
let Some(ident) = field.ident.as_ref() else {
return quote! {
compile_error!("Encodable cannot be derived for tuple structs");
};
};

quote! {
buf.write_value(&self.#ident);
}
Expand Down
2 changes: 1 addition & 1 deletion server/game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
globed-shared = { path = "../shared" }
globed-derives = { path = "../game-derives" }
globed-derive = { path = "../game-derive" }

alloca = "0.4.0"
anyhow = "1.0.75"
Expand Down
2 changes: 1 addition & 1 deletion server/game/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub mod types;

/* re-export all important types, packets and macros */
pub use bytebufferext::*;
pub use globed_derives::*;
pub use globed_derive::*;
pub use packets::*;
pub use types::*;
9 changes: 9 additions & 0 deletions server/game/src/server_thread/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ macro_rules! gs_alloca_check_size {
($size:expr) => {
let size = $size;
if size > crate::server_thread::handlers::MAX_ALLOCA_SIZE {
// panic in debug, return an error in release mode
if cfg!(debug_assertions) {
panic!(
"attempted to allocate {} bytes on the stack - this is above the limit of {} and indicates a logic error.",
size,
crate::server_thread::handlers::MAX_ALLOCA_SIZE
);
}

let err = crate::server_thread::error::PacketHandlingError::DangerousAllocation(size);
return Err(err);
}
Expand Down
6 changes: 4 additions & 2 deletions server/protocol.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Protocol

this is a brief protocol description so that I don't forget what everything does :p
if you somehow stumbled upon this file, hi! this is a brief protocol description so that I don't forget what everything does :p

plus sign means encrypted packet
plus sign means encrypted packet.

i will probably forget to update this very often

### Client

Expand Down
4 changes: 2 additions & 2 deletions src/audio/audio_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <thread>
#include <functional>
#include <fmod.hpp>
#include <util/sync.hpp>
#include "opus_codec.hpp"

#include "audio_frame.hpp"
#include "audio_sample_queue.hpp"
#include <util/sync.hpp>

using util::sync::WrappingMutex;
using util::sync::AtomicBool;
Expand Down
1 change: 0 additions & 1 deletion src/audio/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#if GLOBED_VOICE_SUPPORT

#include "audio_frame.hpp"
#include "audio_manager.hpp"

AudioStream::AudioStream() {
Expand Down
3 changes: 2 additions & 1 deletion src/audio/opus_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#if GLOBED_VOICE_SUPPORT

#include <opus.h>

#include <util/data.hpp>
#include <data/bytebuffer.hpp>
#include <opus.h>

const size_t VOICE_MAX_BYTES_IN_FRAME = 1000; // on avg 200 bytes but y'know, just in case

Expand Down
1 change: 0 additions & 1 deletion src/audio/voice_playback_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#if GLOBED_VOICE_SUPPORT

#include "audio_frame.hpp"
#include "audio_stream.hpp"

/*
Expand Down
4 changes: 3 additions & 1 deletion src/crypto/base_box.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <defs.hpp>
#include <util/data.hpp>

#include <string>

#include <util/data.hpp>

/*
* This class contains no crypto implementation and is here just for boilerplate code.
* Implementers must override:
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/box.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "box.hpp"

#include <util/crypto.hpp>
#include <stdexcept> // std::runtime_error
#include <cstring> // std::memcpy

#include <util/crypto.hpp>

using namespace util::data;

CryptoBox::CryptoBox(byte* key) {
Expand Down
3 changes: 0 additions & 3 deletions src/crypto/box.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#pragma once
#include "base_box.hpp"
#include <defs.hpp>
#include <util/data.hpp>

#include <sodium.h>


class CryptoBox : public BaseCryptoBox {
public:

Expand Down
3 changes: 2 additions & 1 deletion src/crypto/secret_box.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "secret_box.hpp"

#include <util/crypto.hpp>
#include <cstring> // std::memcpy

#include <util/crypto.hpp>

using namespace util::data;

SecretBox::SecretBox(bytevector key) {
Expand Down
2 changes: 0 additions & 2 deletions src/crypto/secret_box.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include "base_box.hpp"
#include <defs.hpp>
#include <util/data.hpp>

#include <sodium.h>

Expand Down
3 changes: 2 additions & 1 deletion src/data/bitbuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once
#include <defs.hpp>

#include <bitset>
#include <vector>
#include <cstdint>
#include <defs.hpp>

/*
* BitBuffer - a simple interface that allows you to read/write bits
Expand Down
3 changes: 0 additions & 3 deletions src/data/bytebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ void ByteBuffer::writeBytes(const bytevector& vec) {
}

/* cocos/gd */
#ifndef GLOBED_ROOT_NO_GEODE

cocos2d::ccColor3B ByteBuffer::readColor3() {
auto r = this->readU8();
Expand Down Expand Up @@ -157,8 +156,6 @@ void ByteBuffer::writePoint(cocos2d::CCPoint point) {
this->writeF32(point.y);
}

#endif // GLOBED_ROOT_NO_GEODE

bytevector ByteBuffer::getData() const {
return _data;
}
Expand Down
9 changes: 5 additions & 4 deletions src/data/bytebuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once
#include "bitbuffer.hpp"
#include <defs.hpp>
#include <util/data.hpp>

#include <type_traits>

#include "bitbuffer.hpp"
#include <util/data.hpp>

class ByteBuffer;

// Represents a data type that can be easily written to a ByteBuffer
Expand Down Expand Up @@ -209,6 +211,7 @@ class ByteBuffer {
// Write a list of `Encodable` objects, prefixed with 4 bytes indicating the count.
template <Encodable T>
void writeValueVector(const std::vector<T>& values) {
this->writeU32(values.size());
for (const T& value : values) {
value.encode(*this);
}
Expand Down Expand Up @@ -297,7 +300,6 @@ class ByteBuffer {
return static_cast<E>(this->readPrimitive<P>());
}

#ifndef GLOBED_ROOT_NO_GEODE
/*
* Cocos/GD serializable methods
*/
Expand All @@ -315,7 +317,6 @@ class ByteBuffer {
void writeColor4(cocos2d::ccColor4B color);
// Write a CCPoint (2 floats)
void writePoint(cocos2d::CCPoint point);
#endif

/*
* Misc util functions
Expand Down
1 change: 1 addition & 0 deletions src/data/packets/all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ std::shared_ptr<Packet> matchPacket(packetid_t packetId) {
PACKET(KeepaliveResponsePacket);
PACKET(ServerDisconnectPacket);
PACKET(LoggedInPacket);
PACKET(LoginFailedPacket);
PACKET(ServerNoticePacket);

// game related
Expand Down
2 changes: 0 additions & 2 deletions src/data/packets/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*/

#pragma once
#include <defs.hpp>

#include "packet.hpp"

#include "client/connection.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/data/packets/client/game.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <defs.hpp>
#include <data/packets/packet.hpp>
#include <data/types/gd.hpp>

Expand Down
1 change: 0 additions & 1 deletion src/data/packets/client/misc.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <defs.hpp>
#include <data/packets/packet.hpp>
#include <data/types/gd.hpp>

Expand Down
3 changes: 1 addition & 2 deletions src/data/packets/packet.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include <data/bytebuffer.hpp>
#include <defs.hpp>

using packetid_t = uint16_t;
#define GLOBED_PACKET(id,enc) \
Expand Down Expand Up @@ -48,7 +47,7 @@ class PacketHeader {
id = buf.readU16();
encrypted = buf.readBool();
}

packetid_t id;
bool encrypted;
};
1 change: 0 additions & 1 deletion src/data/packets/server/game.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <defs.hpp>
#include <data/packets/packet.hpp>
#include <data/types/gd.hpp>

Expand Down
2 changes: 0 additions & 2 deletions src/defs.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma once
#include <config.hpp>

#include <defs/basic.hpp>

#include <defs/assert.hpp>
Expand Down
10 changes: 3 additions & 7 deletions src/defs/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@
* GLOBED_UNIMPL - throws a runtime error as the method was not implemented and isn't meant to be called
*/

#ifndef GLOBED_ROOT_NO_GEODE
# define GLOBED_REQUIRE_LOG geode::log::error
#endif

#if GLOBED_CAN_USE_SOURCE_LOCATION && !defined(GLOBED_ROOT_NO_GEODE)
#if GLOBED_CAN_USE_SOURCE_LOCATION
# define GLOBED_REQUIRE(condition,message) \
if (!(condition)) [[unlikely]] { \
auto ev_msg = (message); \
Expand All @@ -48,13 +44,13 @@
# define GLOBED_REQUIRE(condition,message) \
if (!(condition)) [[unlikely]] { \
auto ev_msg = (message); \
GLOBED_REQUIRE_LOG(std::string("Condition failed: ") + ev_msg); \
geode::log::error(std::string("Condition failed: ") + ev_msg); \
throw std::runtime_error(std::string(ev_msg)); \
}
# define GLOBED_HARD_ASSERT(condition,message) \
if (!(condition)) [[unlikely]] { \
auto ev_msg = (message); \
GLOBED_REQUIRE_LOG(std::string("Condition failed: ") + ev_msg); \
geode::log::error(std::string("Condition failed: ") + ev_msg); \
GLOBED_SUICIDE; \
}
#endif
Expand Down
5 changes: 1 addition & 4 deletions src/defs/basic.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#pragma once
#include <config.hpp>

#ifndef GLOBED_ROOT_NO_GEODE
# include <Geode/Geode.hpp>
#endif
#include <Geode/Geode.hpp>
Loading

0 comments on commit 067b53d

Please sign in to comment.