commit b7dfb305c7090ca5d689027594d87d33a0a1f870 Author: Andy Pack Date: Sun Jun 4 16:29:21 2023 +0100 initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1f7fba9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,13 @@ +name: ci + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + run: mkdir build && cd build && ../cbuild \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5257758 --- /dev/null +++ b/.gitignore @@ -0,0 +1,279 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Bb]uild/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..38e9db4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +# CMakeList.txt : CMake project for ImageMan, include source and define +# project specific logic here. +# +cmake_minimum_required(VERSION 3.19) + +if(${CMAKE_VERSION} VERSION_LESS 3.19) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +endif() + +include(FetchContent) + +project(proxy + VERSION + 1.0 + DESCRIPTION + "C++ scratchpad" + LANGUAGES + CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +add_subdirectory(libs/library) +add_subdirectory(src) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ac6128 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# proxy + +fiddling with sockets & multithreading in C++. diff --git a/cbuild b/cbuild new file mode 100755 index 0000000..909003a --- /dev/null +++ b/cbuild @@ -0,0 +1,2 @@ +cmake .. -DCMAKE_BUILD_TYPE=Debug +cmake --build . \ No newline at end of file diff --git a/libs/library/CMakeLists.txt b/libs/library/CMakeLists.txt new file mode 100644 index 0000000..792a8bf --- /dev/null +++ b/libs/library/CMakeLists.txt @@ -0,0 +1,15 @@ +set(LIB_NAME "library") + +add_library(${LIB_NAME} src/lib.cpp) + +# We need this directory, and users of our library will need it too +target_include_directories(${LIB_NAME} PUBLIC ../../include PRIVATE ./include) + +# All users of this library will need at least C++11 +target_compile_features(${LIB_NAME} PUBLIC cxx_std_17) + +# IDEs should put the headers in a nice place +source_group( + TREE "${PROJECT_SOURCE_DIR}/include" + PREFIX "Header Files" + FILES ${HEADER_LIST}) \ No newline at end of file diff --git a/libs/library/include/library/lib.hpp b/libs/library/include/library/lib.hpp new file mode 100644 index 0000000..cdf0cec --- /dev/null +++ b/libs/library/include/library/lib.hpp @@ -0,0 +1,3 @@ +#pragma once + +#include \ No newline at end of file diff --git a/libs/library/src/lib.cpp b/libs/library/src/lib.cpp new file mode 100644 index 0000000..a492d3b --- /dev/null +++ b/libs/library/src/lib.cpp @@ -0,0 +1,7 @@ +#include "library/lib.hpp" + +using namespace std; + +void example_func() { + cout << "hello from a func" << endl; +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..99b8202 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(proxy main.cpp) + +FetchContent_Declare( + json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG v3.11.2 +) + +FetchContent_MakeAvailable(json) + +FetchContent_Declare( + Boost + GIT_REPOSITORY https://github.com/boostorg/boost.git + GIT_TAG boost-1.82.0 +) +FetchContent_MakeAvailable(Boost) + +target_link_libraries(proxy PRIVATE nlohmann_json::nlohmann_json Boost::program_options Boost::log Boost::date_time Boost::filesystem Boost::system Boost::thread Boost::log_setup Boost::chrono Boost::atomic library) \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..bbc3aeb --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,92 @@ +#include +#include + +#include + +using namespace std; +namespace po = boost::program_options; + +void on_age(int age) +{ + BOOST_LOG_TRIVIAL(info) << "On age: " << age; +} + +shared_ptr init_config(int argc, const char *argv[]) +{ + int def; + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "produce help message") + ("compression,c", po::value(), "set compression level") + ("def,d", po::value(&def)->default_value(10), "set def level") + ("abc", po::value()->default_value(25), "set abc level") + ("phone", po::value>()->multitoken()->zero_tokens()->composing(), "Phone") + ("age,a", po::value()->notifier(on_age), "age") + ("config", po::value(), "Config file") + ; + + po::options_description other("Other"); + desc.add_options() + ("other-option,o", po::value(), "another option") + ; + + // Hidden options, will be allowed both on command line and + // in config file, but will not be shown to the user. + po::options_description hidden("Hidden options"); + hidden.add_options() + ("input-file", po::value< vector >(), "input file"); + + po::positional_options_description p; + p.add("phone", -1); + + po::options_description cmdline_options; + cmdline_options.add(desc).add(other).add(hidden); + + po::options_description config_file_options; + config_file_options.add(desc).add(hidden); + + po::options_description visible("Allowed options"); + visible.add(desc).add(other); + + //////////// + // PARSE + //////////// + + auto vm = make_shared(); + po::store(po::command_line_parser(argc, argv) + .options(desc) + .positional(p) + .run(), + *vm); + + if (vm->count("config")) + { + BOOST_LOG_TRIVIAL(info) << "Attempting file config load"; + std::ifstream ifs{(*vm)["config"].as().c_str()}; + if (ifs) { + BOOST_LOG_TRIVIAL(info) << "File opened, loading..."; + po::store(po::parse_config_file(ifs, config_file_options), *vm); + } + } + po::notify(*vm); + + if (vm->count("help")) { + BOOST_LOG_TRIVIAL(info) << desc; + } + else { + + if (vm->count("compression")) { + BOOST_LOG_TRIVIAL(info) << "Compression level was set to " << (*vm)["compression"].as() << "."; + } else { + BOOST_LOG_TRIVIAL(info) << "Compression level was not set."; + } + + if (vm->count("def")) { + BOOST_LOG_TRIVIAL(info) << "def level was set to " << (*vm)["def"].as() << "."; + } else { + BOOST_LOG_TRIVIAL(info) << "def level was not set."; + } + } + + return vm; +} \ No newline at end of file diff --git a/src/logging.cpp b/src/logging.cpp new file mode 100644 index 0000000..e85e2e5 --- /dev/null +++ b/src/logging.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +namespace logging = boost::log; +namespace src = boost::log::sources; +namespace sinks = boost::log::sinks; +namespace keywords = boost::log::keywords; + +void init_logging() +{ + logging::register_simple_formatter_factory("Severity"); + + logging::add_file_log + ( + keywords::file_name = "proxy_%N.log", + keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), + keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%" + ); + + logging::add_console_log(std::cout, boost::log::keywords::format = "[%TimeStamp%] [%Severity%] >> %Message%"); + + logging::core::get()->set_filter + ( + logging::trivial::severity >= logging::trivial::info + ); + + logging::add_common_attributes(); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..136357b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#include "logging.cpp" +#include "config.cpp" + +#include + +using namespace std; + +int main(int argc, const char *argv[]) { + try{ + init_logging(); + init_config(argc, argv); + + BOOST_LOG_TRIVIAL(info) << "hello world"; + } + catch (const po::error &ex) + { + BOOST_LOG_TRIVIAL(error) << ex.what(); + } +} \ No newline at end of file