Skip to content

Commit

Permalink
Merge pull request #12 from Jacquwes/asio
Browse files Browse the repository at this point in the history
Use asio instead of WinSocks
  • Loading branch information
Jacquwes committed Sep 28, 2023
2 parents c40c27c + 4c55bf2 commit 4ec3d6b
Show file tree
Hide file tree
Showing 52 changed files with 3,926 additions and 468 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/ctest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CTest on multiple platforms

on:
workflow_dispatch:
push:
branches: [ "master", "api-v2", "asio" ]
paths:
- '**.cpp'
- '**.h'
- '**.hpp'
- '**.c'
- '**/CMakeLists.txt'
pull_request:
branches: [ "master", "api-v2", "asio" ]
paths:
- '**.cpp'
- '**.h'
- '**.hpp'
- '**.c'
- '**/CMakeLists.txt'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
exclude:
- os: windows-latest
c_compiler: gcc
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v3

- name: Install vcpkg
# Install vcpkg to a known location
run: git clone https://github.com/microsoft/vcpkg.git ${{ github.workspace }}/vcpkg

- name: Bootstrap vcpkg linux
# Bootstrap vcpkg using the appropriate triplet for the current runner operating system
if: matrix.os == 'ubuntu-latest'
run: ${{ github.workspace }}/vcpkg/bootstrap-vcpkg.sh -disableMetrics

- name: Bootstrap vcpkg windows
# Bootstrap vcpkg using the appropriate triplet for the current runner operating system
if: matrix.os == 'windows-latest'
run: ${{ github.workspace }}/vcpkg/bootstrap-vcpkg.bat -disableMetrics

- name: Install asio and gtest linux
# Install dependencies using the appropriate triplet for the current runner operating system
run: ${{ github.workspace }}/vcpkg/vcpkg install asio:x64-linux gtest:x64-linux
if: matrix.os == 'ubuntu-latest'

- name: Install asio and gtest windows
# Install dependencies using the appropriate triplet for the current runner operating system
run: ${{ github.workspace }}/vcpkg/vcpkg install asio:x64-windows gtest:x64-windows
if: matrix.os == 'windows-latest'

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DCMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
52 changes: 52 additions & 0 deletions .github/workflows/doxygen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
on:
workflow_dispatch:
push:
branches: [ "master", "api-v2", "asio" ]
paths:
- '**.h'
- '**.md'
- '**doxygen.yml'
- '**doxyfile'
pull_request:
branches: [ "master", "api-v2", "asio" ]
paths:
- '**.h'
- '**.md'
- '**doxygen.yml'
- '**doxyfile'

name: Build doxygen documentation
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Doxygen and Dot
run: sudo apt install doxygen graphviz

- name: Run doxygen
run: doxygen doxyfile

- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
path: docs/html

deploy:
runs-on: ubuntu-latest
needs:
- build

permissions:
pages: write
id-token: write

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Deploy to Github Pages
id: deployment
uses: actions/deploy-pages@v2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

docs

# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
CMakeSettings.json

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

enable_testing()

find_package(asio CONFIG REQUIRED)
find_package(GTest CONFIG REQUIRED)

if (WIN32)
add_definitions(-D_WIN32_WINNT=0x0A00)
endif()

add_subdirectory(client)
add_subdirectory(examples)
add_subdirectory(shared)
add_subdirectory(server)
add_subdirectory(tests)
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
# Pine

Pine is a modern and lightweight Windows chat/forum application built with the
[Windows App SDK](https://docs.microsoft.com/windows/apps/windows-app-sdk/), and
[WinUI3](https://docs.microsoft.com/windows/apps/winui/winui3/).
[![CMake on multiple platforms](https://github.com/Jacquwes/Pine/actions/workflows/cmake.yml/badge.svg?branch=asio)](https://github.com/Jacquwes/Pine/actions/workflows/cmake.yml)

Server side is built on
[WinSocks2](https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-start-page-2).
Pine is a modern and lightweight C++20 library for building chat applications.

It uses its own protocol, which is based on TCP using asio. Therefore, it is
not suitable for use on the web.

It is multi-threaded and uses asynchronous I/O, and is designed to be
easily extensible.

## Building

_Build will not work if using clang_

It is recommended to use [vcpkg](https://github.com/microsoft/vcpkg) to install
dependencies. Pine uses CMake as its build system. To build Pine, run the
following commands:

Dependencies:

```bash
vcpkg install asio:x64-windows gtest:x64-windows
vcpkg install asio:x64-linux gtest:x64-linux
```

```bash
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=<path to vcpkg>/scripts/buildsystems/vcpkg.cmake
cmake --build .
```
14 changes: 14 additions & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
project(client)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE HEADER_FILES "include/*.h")
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")

add_library(client STATIC ${HEADER_FILES} ${SOURCE_FILES})

target_include_directories(client PUBLIC include)

target_link_libraries(client PRIVATE shared)
target_link_libraries(client PRIVATE asio)
65 changes: 65 additions & 0 deletions client/include/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <cstdint>
#include <functional>
#include <thread>
#include <memory>
#include <string>
#include <vector>

#include <asio/io_context.hpp>
#include <asio/ip/tcp.hpp>
#include <coroutine.h>
#include <message.h>
#include <user.h>

#include "client_connection.h"

namespace pine
{
/// @brief A client that can connect to a single server.
class client
{
friend class client_connection;
public:
/// @brief Initialize a client.
/// @param username: The username of the client.
client(std::string username);

/// @brief Connect to a server.
/// @param host: Hostname or ip address of the server.
/// @param port: TCP port of the server.
/// @return True if the connection was successful, false otherwise.
bool connect(std::string const& host = "localhost", uint16_t const& port = 80);

/// @brief Disconnect from the server.
void disconnect();

/// @brief Send a message to the server.
async_task message_server(std::shared_ptr<socket_messages::message> const& message) const;

/// @brief Register a callback for when a message is received.
/// @return A reference to the client.
client& on_message(std::function<async_task(
std::shared_ptr<socket_messages::message>
)> const& on_message);

/// @brief Get the user data of the client.
[[nodiscard]]
constexpr user const& get_user() const;

private:
asio::ip::tcp::socket socket;
std::jthread connection_thread;

std::vector<
std::function<async_task(
std::shared_ptr<socket_messages::message>
)>
> on_message_callbacks;

asio::io_context io_context;
std::unique_ptr<client_connection> connection;
user user_data;
};
}
30 changes: 30 additions & 0 deletions client/include/client_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <cstdint>
#include <string>
#include <thread>

#include <asio/error_code.hpp>
#include <asio/ip/tcp.hpp>
#include <connection.h>
#include <coroutine.h>

namespace pine
{
class client_connection : public connection
{
public:
client_connection(asio::ip::tcp::socket& socket);

bool connect(std::string const& host, uint16_t const& port = 80);

void disconnect();

async_task listen();

asio::error_code ec;

private:
std::jthread client_thread;
};
}
Loading

0 comments on commit 4ec3d6b

Please sign in to comment.