Skip to content

Commit

Permalink
refactor: use uint8_t instead of char & make method const
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkeeyCat committed Mar 12, 2024
1 parent 42f5fb1 commit a92ed1b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 69 deletions.
37 changes: 9 additions & 28 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest, ubuntu-20.04]
os: [ubuntu-latest, ubuntu-20.04]
include:
- os: ubuntu-latest
cmake-args: -G Ninja
Expand All @@ -29,16 +29,6 @@ jobs:
gtest-env: GTEST_FILTER=-*SQLite*
package-file: "*-linux_x86_64.tar.xz"
fancy: false
- os: macOS-latest
cmake-args: -G Ninja
cmake-init-env: CXXFLAGS=-Werror
package-file: "*-macos.dmg"
fancy: false
- os: windows-latest
cmake-args: -A x64
cmake-init-env: CXXFLAGS=/WX LDFLAGS=/WX
package-file: "*-win64.zip"
fancy: false

steps:
- uses: actions/checkout@v4
Expand All @@ -53,15 +43,6 @@ jobs:
# sudo apt-get upgrade -y
sudo apt-get install pkg-config cmake ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libvulkan-dev glslang-tools spirv-tools libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev libpng-dev valgrind gcovr -y
# high iq solution
git clone https://github.com/protocolbuffers/protobuf.git --branch v3.21.12 --single-branch
cd protobuf
git submodule update --init --recursive
mkdir build
cd build
cmake ..
sudo make install
- name: Prepare Linux (non-fancy)
if: ${{ contains(matrix.os, 'ubuntu') && !matrix.fancy }}
run: |
Expand Down Expand Up @@ -96,15 +77,15 @@ jobs:
FLUSH PRIVILEGES;
EOF
- name: Prepare macOS
if: contains(matrix.os, 'macOS')
- name: Compile & install protobuf
run: |
brew update || true
brew install pkg-config sdl2 ffmpeg ninja molten-vk vulkan-headers glslang spirv-tools protobuf || true
brew upgrade freetype
pip3 install --break-system-packages dmgbuild
echo /Library/Frameworks/Python.framework/Versions/3.12/bin >> $GITHUB_PATH
sudo rm -rf /Library/Developer/CommandLineTools
git clone https://github.com/protocolbuffers/protobuf.git --branch v3.21.12 --single-branch
cd protobuf
git submodule update --init --recursive
mkdir build
cd build
cmake ${{ matrix.cmake-args }} ..
sudo cmake --build . --config Release --target install
- name: Cache Rust dependencies
uses: Swatinem/rust-cache@v2
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ jobs:
if: matrix.language == 'cpp'
run: |
sudo apt-get update -y
sudo apt-get install pkg-config cmake ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev libmariadb-dev libwebsockets-dev libvulkan-dev glslang-tools spirv-tools protobuf-compiler -y
sudo apt-get install pkg-config cmake ninja-build libfreetype6-dev libnotify-dev libsdl2-dev libsqlite3-dev libavcodec-dev libavformat-dev libavutil-dev libswresample-dev libswscale-dev libx264-dev libmariadb-dev libwebsockets-dev libvulkan-dev glslang-tools spirv-tools -y
- name: Compile & install protobuf
run: |
git clone https://github.com/protocolbuffers/protobuf.git --branch v3.21.12 --single-branch
cd protobuf
git submodule update --init --recursive
mkdir build
cd build
cmake ${{ matrix.cmake-args }} ..
sudo cmake --build . --config Release --target install
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
40 changes: 14 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ if(SECURITY_COMPILER_FLAGS)
endif()

# ddts
find_package(Protobuf REQUIRED)
find_package(Protobuf CONFIG REQUIRED)

if(TARGET_OS AND TARGET_OS STREQUAL "mac")
find_program(CMAKE_OTOOL otool)
Expand Down Expand Up @@ -1843,31 +1843,19 @@ add_custom_command(OUTPUT "src/game/generated/wordlist.h"
# DDTS
########################################################################

message("${Protobuf_VERSION}")

file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/src/protos)

add_custom_command(
OUTPUT
src/protos/request.pb.h
src/protos/request.pb.cc
src/protos/response.pb.h
src/protos/response.pb.cc
src/protos/common.pb.cc
src/protos/common.pb.h
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/datasrc
COMMAND protoc protos/*.proto
--cpp_out=${PROJECT_BINARY_DIR}/src/protos
--proto_path=./protos
)
file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src/protos")
file(GLOB PROTO_SOURCE "${PROJECT_SOURCE_DIR}/datasrc/protos/*.proto")

add_library(Proto OBJECT "${PROTO_SOURCE}")
target_link_libraries(Proto PUBLIC protobuf::libprotobuf)
target_include_directories(Proto PUBLIC "$<BUILD_INTERFACE:${PROTO_BINARY_DIR}>")
set(PROTO_BINARY_DIR "${PROJECT_BINARY_DIR}/src/protos")

set(PROTO_GENERATED
src/protos/request.pb.h
src/protos/request.pb.cc
src/protos/response.pb.h
src/protos/response.pb.cc
src/protos/common.pb.cc
src/protos/common.pb.h
protobuf_generate(
TARGET Proto
LANGUAGE cpp
IMPORT_DIRS "${PROJECT_SOURCE_DIR}/datasrc/protos"
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
)

########################################################################
Expand Down Expand Up @@ -2648,7 +2636,7 @@ if(SERVER)
rust_engine_shared

# ddts
protobuf::libprotobuf
Proto

${LIBS}
)
Expand Down
24 changes: 12 additions & 12 deletions src/engine/server/ddts.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// WARNING: if you know how to code, please dont check this code
// you will have a heart attack

#include "base/system.h"
#include <base/system.h>
#include <arpa/inet.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
Expand All @@ -12,17 +13,16 @@
#include <unistd.h>

#include "ddts.h"
#include "protos/common.pb.h"
#include <protos/common.pb.h>
#include <protos/request.pb.h>

const int PORT = 42069;
const std::string HOST = "127.0.0.1";

const std::string SHM_NAME = "/ddts2";
const std::string SHM_NAME = "/ddts";
const uint16_t PORT = 42069;
const size_t SHM_SIZE = 10;

CDDTS::CDDTS(unsigned int id, unsigned int happeningId) :
m_HappeningId(happeningId), m_Id(id), m_Shutdown(false)
CDDTS::CDDTS(unsigned int Id, unsigned int HappeningId) :
m_HappeningId(HappeningId), m_Id(Id), m_Shutdown(false)
{
fd = shm_open(SHM_NAME.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR);

Expand All @@ -40,7 +40,7 @@ CDDTS::CDDTS(unsigned int id, unsigned int happeningId) :

bool CDDTS::CheckShutdownSignal()
{
char *mem = static_cast<char *>(sharedMemory);
uint8_t *mem = static_cast<uint8_t *>(sharedMemory);

if(mem[m_Id] == 1)
{
Expand All @@ -51,7 +51,7 @@ bool CDDTS::CheckShutdownSignal()
return false;
}

void CDDTS::Shutdown()
void CDDTS::Shutdown() const
{
if(!m_Shutdown)
{
Expand All @@ -71,19 +71,19 @@ void CDDTS::Shutdown()
request.set_origin(::Origin::DDNET);

const size_t size = request.ByteSizeLong();
char *bytes = new char[size];
uint8_t *bytes = new uint8_t[size];

request.SerializeToArray(bytes, size);

send(sock, bytes, size, 0);

std::vector<char> gottem(1024);
std::vector<uint8_t> gottem(1024);

// wait for the response owo. works for now
recv(sock, gottem.data(), gottem.size(), 0);
}

char *mem = static_cast<char *>(sharedMemory);
uint8_t *mem = static_cast<uint8_t *>(sharedMemory);
mem[m_Id] = 0;

munmap(sharedMemory, SHM_SIZE);
Expand Down
4 changes: 2 additions & 2 deletions src/engine/server/ddts.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CDDTS
unsigned int m_Id;
bool m_Shutdown;

CDDTS(unsigned int id, unsigned int happeningId);
CDDTS(unsigned int Id, unsigned int HappeningId);
bool CheckShutdownSignal();
void Shutdown();
void Shutdown() const;
};

0 comments on commit a92ed1b

Please sign in to comment.