Skip to content
Merged
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
106 changes: 104 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ on:
branches: [ "master" ]

jobs:
build:
##########################################################################################
## Tests
##########################################################################################
test:
name: Tests
runs-on: ${{ matrix.os }}

strategy:
fail-fast: true
matrix:
Expand Down Expand Up @@ -116,3 +119,102 @@ jobs:
working-directory: ${{ steps.strings.outputs.test-dir }}
run: ./${{ steps.strings.outputs.test-exe }}
timeout-minutes: 30

##########################################################################################
## Examples
##########################################################################################

examples:
name: Examples
needs: test
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
build_type: [Release]
copiler_suite: [msvc, llvm, gnu]
exclude:
- os: windows-latest
copiler_suite: gnu
- os: windows-latest
copiler_suite: llvm

- os: ubuntu-latest
copiler_suite: msvc

- os: macos-latest
copiler_suite: msvc
- os: macos-latest
copiler_suite: gnu

steps:
- uses: actions/checkout@v3

- name: Set strings
id: strings
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
echo "exe-subdir=${{ matrix.build_type }}/" >> "$GITHUB_OUTPUT"
echo "exe-suffix=.exe" >> "$GITHUB_OUTPUT"
fi

if [ "${{ matrix.copiler_suite }}" == "msvc" ]; then
echo "compiler_c=cl" >> "$GITHUB_OUTPUT"
echo "compiler_cxx=cl" >> "$GITHUB_OUTPUT"
elif [ "${{ matrix.copiler_suite }}" == "llvm" ]; then
echo "compiler_c=clang" >> "$GITHUB_OUTPUT"
echo "compiler_cxx=clang++" >> "$GITHUB_OUTPUT"
else
echo "compiler_c=gcc" >> "$GITHUB_OUTPUT"
echo "compiler_cxx=g++" >> "$GITHUB_OUTPUT"
fi

- name: Configure CMake
working-directory: ${{ github.workspace }}/examples
shell: bash
run: |
export CC="${{ steps.strings.outputs.compiler_c }}"
export CXX="${{ steps.strings.outputs.compiler_cxx }}"
if [ "$RUNNER_OS" == "macOS" ]; then
export CC=$(brew --prefix llvm@15)/bin/$CC
export CXX=$(brew --prefix llvm@15)/bin/$CXX
fi
cmake -B "${{ github.workspace }}/examples/build" \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-S "${{ github.workspace }}/examples"

- name: Build
shell: bash
run: |
cmake --build "${{ github.workspace }}/examples/build" \
--config ${{ matrix.build_type }}

- name: Run
working-directory: ${{ github.workspace }}/examples/build
timeout-minutes: 5
shell: bash
run: |
invoke_exe() {
local arg="$1"
if [ "$RUNNER_OS" = "Windows" ]; then
local exe_path="./$arg/${{ matrix.build_type }}/$arg.exe"
else
local exe_path="./$arg/$arg"
fi
if [ -x "$exe_path" ]; then
"$exe_path"
else
echo "Error: Executable not found or not executable at $exe_path"
return 1
fi
}
invoke_exe scheduler_01_simple_task
invoke_exe scheduler_02_coroutine_task
invoke_exe scheduler_03_multistep_task
invoke_exe scheduler_04_interacting_tasks
invoke_exe iocore_01_tcp_hello
invoke_exe iocore_02_ssl_hello
invoke_exe iocore_03_pipeline
invoke_exe iocore_04_tcp_periodic
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
-Wall -Wextra -Wpedantic -Werror -Wno-unknown-warning-option -Wunused-function
-Wno-invalid-offsetof -Wno-unused-value -Wno-deprecated-copy
-Wno-gnu-zero-variadic-macro-arguments
-fno-rtti
)

set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
Expand Down Expand Up @@ -42,5 +41,9 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif ()

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(bench)
if (NOT MG_SKIP_TEST)
add_subdirectory(test)
endif()
if (NOT MG_SKIP_BENCHES)
add_subdirectory(bench)
endif()
70 changes: 70 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
cmake_minimum_required (VERSION 3.8)

project("Examples")

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
message(STATUS "Build type ${CMAKE_BUILD_TYPE}")

if (NOT DEFINED CMAKE_CXX_STANDARD)
message(STATUS "Using C++20 standard as default")
set(CMAKE_CXX_STANDARD 20)
else()
message(STATUS "Using C++${CMAKE_CXX_STANDARD} standard as explicitly requested")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(
-Wall -Wextra -Wpedantic -Werror -Wno-unknown-warning-option -Wunused-function
-Wno-invalid-offsetof -Wno-unused-value -Wno-deprecated-copy
-Wno-gnu-zero-variadic-macro-arguments
)

set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-pthread")
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-pthread")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options(
# This is needed at least for correct macro handling. Default behaviour won't
# expand __VA_ARGS__ correctly.
/Zc:preprocessor
/WX /wd4266 /wd4324 /wd4355 /wd4365 /wd4458 /wd4514 /wd4548 /wd4625 /wd4626
/wd4668 /wd4710 /wd4820 /wd5026 /wd5027 /wd5039 /wd5045 /wd5105 /wd5219 /wd26439
/wd26800
# It ignores 'break' and 'fallthrough' done via a macro which makes it annoying
# and pointless.
/wd5262
# Info message about a function being inlined.
/wd4711
)
endif()

set(MG_SERVERBOX_BUILD_DIR ${CMAKE_BINARY_DIR}/build_serverbox)
set(MG_SERVERBOX_DIR ${MG_SERVERBOX_BUILD_DIR}/installed)

add_custom_target(install_serverbox
COMMAND ${CMAKE_COMMAND}
-S ${CMAKE_SOURCE_DIR}/..
-B ${MG_SERVERBOX_BUILD_DIR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DMG_SKIP_BENCHES=1
-DMG_SKIP_TEST=1
-DCMAKE_INSTALL_PREFIX=${MG_SERVERBOX_DIR}

COMMAND ${CMAKE_COMMAND}
--build ${MG_SERVERBOX_BUILD_DIR} --config ${CMAKE_BUILD_TYPE} -j

COMMAND ${CMAKE_COMMAND}
--install ${MG_SERVERBOX_BUILD_DIR} --config ${CMAKE_BUILD_TYPE}

COMMENT "Installing serverbox"
)

add_subdirectory(iocore_01_tcp_hello)
add_subdirectory(iocore_02_ssl_hello)
add_subdirectory(iocore_03_pipeline)
add_subdirectory(iocore_04_tcp_periodic)
add_subdirectory(scheduler_01_simple_task)
add_subdirectory(scheduler_02_coroutine_task)
add_subdirectory(scheduler_03_multistep_task)
add_subdirectory(scheduler_04_interacting_tasks)
25 changes: 25 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Examples

The folder provides series of examples how to use the most interesting parts of Serverbox.

Each example can be built and run on its own, they only depend on Serverbox itself, and not on each other.

They can be used as reference points when you want to try and build something of your own.

The recommended way of reading them is firstly all `scheduler_*`, then all `iocore_*`. The sequence 01, 02, etc is not required, each example is self-sufficient build- and code-wise. But later examples might not explain some simple things already covered in the previous examples.

## Running

The recommended way to use them is this (on non-Windows):
```Bash
# Build them all.
mkdir -p build
cd build
cmake ..
make -j

# Run any of them like this:
./scheduler_01_simple_task/scheduler_01_simple_task
```

On Windows platform they also compile and run, but one would have to run them via cmd/PowerShell/VisualStudio.
21 changes: 21 additions & 0 deletions examples/iocore_01_tcp_hello/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
add_executable(iocore_01_tcp_hello main.cpp)
add_dependencies(iocore_01_tcp_hello install_serverbox)

target_include_directories(iocore_01_tcp_hello PUBLIC
${MG_SERVERBOX_DIR}/include
)
target_link_directories(iocore_01_tcp_hello PUBLIC
${MG_SERVERBOX_DIR}/lib
)
set(libs
mgaio
mgnet
mgbox
mgboxstub
)
if(WIN32)
set(libs ${libs} ws2_32.lib crypt32.lib)
endif()
target_link_libraries(iocore_01_tcp_hello
${libs}
)
Loading
Loading