From 5abd11124d0c4d0be309aa5aa622b57de4c83269 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 27 Jan 2024 11:42:27 -0800 Subject: [PATCH] making basic server work with dependencies for uwebsockets added to cmake --- .clang-format | 190 +---------------------------------- server.sh | 18 ++++ server/CMakeLists.txt | 5 + server/compile_commands.json | 7 ++ server/src/Main.cpp | 68 ++++++++++++- 5 files changed, 98 insertions(+), 190 deletions(-) create mode 100755 server.sh create mode 100644 server/compile_commands.json diff --git a/.clang-format b/.clang-format index d23cc24..35d7059 100644 --- a/.clang-format +++ b/.clang-format @@ -1,192 +1,4 @@ --- Language: Cpp -# BasedOnStyle: Microsoft -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -AlignArrayOfStructures: None -AlignConsecutiveMacros: None -AlignConsecutiveAssignments: None -AlignConsecutiveBitFields: None -AlignConsecutiveDeclarations: None -AlignEscapedNewlines: Right -AlignOperands: Align -AlignTrailingComments: true -AllowAllArgumentsOnNextLine: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortEnumsOnASingleLine: false -AllowShortBlocksOnASingleLine: Never -AllowShortCaseLabelsOnASingleLine: false -AllowShortFunctionsOnASingleLine: None -AllowShortLambdasOnASingleLine: All -AllowShortIfStatementsOnASingleLine: Never -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: None -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine -AttributeMacros: - - __capability -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterCaseLabel: false - AfterClass: true - AfterControlStatement: Always - AfterEnum: true - AfterFunction: true - AfterNamespace: true - AfterObjCDeclaration: true - AfterStruct: true - AfterUnion: false - AfterExternBlock: true - BeforeCatch: true - BeforeElse: true - BeforeLambdaBody: false - BeforeWhile: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeConceptDeclarations: true -BreakBeforeBraces: Custom -BreakBeforeInheritanceComma: false -BreakInheritanceList: BeforeColon -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: true -ColumnLimit: 120 -CommentPragmas: '^ IWYU pragma:' -QualifierAlignment: Leave -CompactNamespaces: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DeriveLineEnding: true -DerivePointerAlignment: false -DisableFormat: false -EmptyLineAfterAccessModifier: Never -EmptyLineBeforeAccessModifier: LogicalBlock -ExperimentalAutoDetectBinPacking: false -PackConstructorInitializers: BinPack -BasedOnStyle: '' -ConstructorInitializerAllOnOneLineOrOnePerLine: false -AllowAllConstructorInitializersOnNextLine: true -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IfMacros: - - KJ_IF_MAYBE -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - SortPriority: 0 - CaseSensitive: false - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - SortPriority: 0 - CaseSensitive: false - - Regex: '.*' - Priority: 1 - SortPriority: 0 - CaseSensitive: false -IncludeIsMainRegex: '(Test)?$' -IncludeIsMainSourceRegex: '' -IndentAccessModifiers: false -IndentCaseLabels: false -IndentCaseBlocks: false -IndentGotoLabels: true -IndentPPDirectives: None -IndentExternBlock: AfterExternBlock -IndentRequires: false -IndentWidth: 4 -IndentWrappedFunctionNames: false -InsertTrailingCommas: None -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -LambdaBodyIndentation: Signature -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBinPackProtocolList: Auto -ObjCBlockIndentWidth: 2 -ObjCBreakBeforeNestedBlockParam: true -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakOpenParenthesis: 0 -PenaltyBreakString: 1000 -PenaltyBreakTemplateDeclaration: 10 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 1000 -PenaltyIndentedWhitespace: 0 -PointerAlignment: Right -PPIndentWidth: -1 -ReferenceAlignment: Pointer -ReflowComments: true -RemoveBracesLLVM: false -SeparateDefinitionBlocks: Leave -ShortNamespaceLines: 1 -SortIncludes: CaseSensitive -SortJavaStaticImport: Before -SortUsingDeclarations: true -SpaceAfterCStyleCast: false -SpaceAfterLogicalNot: false -SpaceAfterTemplateKeyword: true -SpaceBeforeAssignmentOperators: true -SpaceBeforeCaseColon: false -SpaceBeforeCpp11BracedList: false -SpaceBeforeCtorInitializerColon: true -SpaceBeforeInheritanceColon: true -SpaceBeforeParens: ControlStatements -SpaceBeforeParensOptions: - AfterControlStatements: true - AfterForeachMacros: true - AfterFunctionDefinitionName: false - AfterFunctionDeclarationName: false - AfterIfMacros: true - AfterOverloadedOperator: false - BeforeNonEmptyParentheses: false -SpaceAroundPointerQualifiers: Default -SpaceBeforeRangeBasedForLoopColon: true -SpaceInEmptyBlock: false -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: Never -SpacesInConditionalStatement: false -SpacesInContainerLiterals: true -SpacesInCStyleCastParentheses: false -SpacesInLineCommentPrefix: - Minimum: 1 - Maximum: -1 -SpacesInParentheses: false -SpacesInSquareBrackets: false -SpaceBeforeSquareBrackets: false -BitFieldColonSpacing: Both -Standard: Latest -StatementAttributeLikeMacros: - - Q_EMIT -StatementMacros: - - Q_UNUSED - - QT_REQUIRE_VERSION -TabWidth: 4 -UseCRLF: false -UseTab: Never -WhitespaceSensitiveMacros: - - STRINGIZE - - PP_STRINGIZE - - BOOST_PP_STRINGIZE - - NS_SWIFT_NAME - - CF_SWIFT_NAME -... +BasedOnStyle: Microsoft diff --git a/server.sh b/server.sh new file mode 100755 index 0000000..2077050 --- /dev/null +++ b/server.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +cd server/ + +rm -rf build/ +mkdir -p build/ +cd build + +# Build the project +cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .. + +make + +cp ./compile_commands.json ../ + +./HomechatServer diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 0f350ef..95406ce 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -4,6 +4,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project(HomechatServer) +find_library(USOCKETS_LIBRARY uSockets.a PATHS /usr/local/lib REQUIRED) +find_package(ZLIB REQUIRED) + set(source_dir ${CMAKE_CURRENT_SOURCE_DIR}/src) file(GLOB source_files ${source_dir}/*.cpp @@ -11,3 +14,5 @@ file(GLOB source_files add_executable(${PROJECT_NAME} ${source_files}) target_include_directories(${PROJECT_NAME} PUBLIC ${source_dir}/include) +target_link_libraries(${PROJECT_NAME} ${USOCKETS_LIBRARY}) +target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB) diff --git a/server/compile_commands.json b/server/compile_commands.json new file mode 100644 index 0000000..2439a0b --- /dev/null +++ b/server/compile_commands.json @@ -0,0 +1,7 @@ +[ +{ + "directory": "/home/talksik/code/homechat/server/build", + "command": "/usr/bin/c++ -I/home/talksik/code/homechat/server/src/include -g -o CMakeFiles/HomechatServer.dir/src/Main.cpp.o -c /home/talksik/code/homechat/server/src/Main.cpp", + "file": "/home/talksik/code/homechat/server/src/Main.cpp" +} +] \ No newline at end of file diff --git a/server/src/Main.cpp b/server/src/Main.cpp index d494d7a..7d4a007 100644 --- a/server/src/Main.cpp +++ b/server/src/Main.cpp @@ -1,7 +1,73 @@ #include +#include int main() { - std::cout << "Hello World!\n"; + std::cout << "Welcome to the homechat server!\n"; + + /* ws->getUserData returns one of these */ + struct PerSocketData + { + /* Fill with user data */ + }; + + /* Keep in mind that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support. + * You may swap to using uWS:App() if you don't need SSL */ + uWS::App({/* There are example certificates in uWebSockets.js repo */ + .key_file_name = "misc/key.pem", + .cert_file_name = "misc/cert.pem", + .passphrase = "1234"}) + .ws( + "/*", {/* Settings */ + .compression = uWS::CompressOptions(uWS::DEDICATED_COMPRESSOR_4KB | uWS::DEDICATED_DECOMPRESSOR), + .maxPayloadLength = 100 * 1024 * 1024, + .idleTimeout = 16, + .maxBackpressure = 100 * 1024 * 1024, + .closeOnBackpressureLimit = false, + .resetIdleTimeoutOnSend = false, + .sendPingsAutomatically = true, + /* Handlers */ + .upgrade = nullptr, + .open = + [](auto * /*ws*/) { + /* Open event here, you may access ws->getUserData() which points to a PerSocketData struct + */ + }, + .message = + [](auto *ws, std::string_view message, uWS::OpCode opCode) { + /* This is the opposite of what you probably want; compress if message is LARGER than 16 kb + * the reason we do the opposite here; compress if SMALLER than 16 kb is to allow for + * benchmarking of large message sending without compression */ + ws->send(message, opCode, message.length() < 16 * 1024); + }, + .dropped = + [](auto * /*ws*/, std::string_view /*message*/, uWS::OpCode /*opCode*/) { + /* A message was dropped due to set maxBackpressure and closeOnBackpressureLimit limit */ + }, + .drain = + [](auto * /*ws*/) { + /* Check ws->getBufferedAmount() here */ + }, + .ping = + [](auto * /*ws*/, std::string_view) { + /* Not implemented yet */ + }, + .pong = + [](auto * /*ws*/, std::string_view) { + /* Not implemented yet */ + }, + .close = + [](auto * /*ws*/, int /*code*/, std::string_view /*message*/) { + /* You may access ws->getUserData() here */ + }}) + .listen(9001, + [](auto *listen_socket) { + if (listen_socket) + { + std::cout << "Listening on port " << 9001 << std::endl; + } + }) + .run(); + return 0; }