Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
qchateau committed Sep 12, 2020
1 parent f9acb80 commit 55632a9
Show file tree
Hide file tree
Showing 16 changed files with 1,087 additions and 1 deletion.
86 changes: 86 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Stroustrup
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
FixNamespaceComments: false
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<[a-z_]+>$'
Priority: 10
- Regex: '^<.*>$'
Priority: 50
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: 'vlc_module_begin'
MacroBlockEnd: 'vlc_module_end'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakAssignment: 50
PenaltyBreakBeforeFirstCallParameter: 100
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 100
PenaltyExcessCharacter: 10
PenaltyReturnTypeOnItsOwnLine: 10000
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never
---
Language: Proto
TabWidth: 4
IndentWidth: 4
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build/
.history/
.vs-code/
.clangd/

compile_commands.json
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# experimental-ws-game
# experimental-ws-game

Trying out boost.beast with C++20 coroutines and websockets.

Maybe this will become a mini-game one day.

# How to run

```bash
# build the server
mkdir -p server/build
cd server/build
CXX=clang++-10 CC=clang-10 cmake .. -DCMAKE_BUILD_TYPE=Release
make -j
cd ../../

# serve the application
docker-compose up -d
```
13 changes: 13 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Test client</title>
<script src="index.js"></script>
</head>

<body>
<div>Hello !</div>
<canvas id="canvas"></canvas>
</body>
</html>
62 changes: 62 additions & 0 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class CanvasManager {
constructor(id) {
this.canvas = document.getElementById(id);
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.ctx = this.canvas.getContext("2d");
}

drawRect(x, y) {
const w = 100;
const h = 100;

this.clear();
this.ctx.fillStyle = "green";
this.ctx.fillRect(x - w / 2, y - h / 2, w, h);
}

clear() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}

class Engine {
constructor(url, canvasId) {
this.canvas = new CanvasManager(canvasId);
this.sock = new WebSocket(url);
this.sock.onopen = function () {
this.onOpen();
}.bind(this);

// Log errors
this.sock.onerror = function (error) {
this.onError(error);
}.bind(this);

// Log messages from the server
this.sock.onmessage = function (e) {
const msg = JSON.parse(e.data);
this.onMessage(msg);
}.bind(this);
}

onOpen() {
console.log("Starting communication");
}

onError(error) {
console.error("WebSocket error", error);
}

onMessage(msg) {
console.log(msg);
this.canvas.drawRect(msg.x, msg.y);
}
}

function main() {
console.log("Running ...");
engine = new Engine("ws://localhost:8080/ws", "canvas");
}

window.onload = main;
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.2"

services:
nginx:
restart: always
image: nginx:1.19
volumes:
- ./nginx:/etc/nginx/templates
- ./client:/www/data
ports:
- "8080:80"
environment:
- WS_SERVER=ws_backend:5678

ws_backend:
restart: always
image: alpine:3.12
volumes:
- ./server/build/bin:/server/
command: /server/server 0.0.0.0 5678 1
25 changes: 25 additions & 0 deletions nginx/default.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

upstream websocket {
server ${WS_SERVER};
}

server {
listen 80;

location / {
root /www/data;
}

location /ws {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
51 changes: 51 additions & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_minimum_required(VERSION 3.10.2)
project(spaceinvader_server)

include(cmake/conan.cmake)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

add_compile_options(-fcoroutines-ts -stdlib=libc++)
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -stdlib=libc++ -lc++abi")

# add this to have a working compile_commands.json ...
include_directories(SYSTEM /usr/lib/llvm-10/include/c++/v1/)

conan_cmake_run(
BASIC_SETUP
CMAKE_TARGETS
REQUIRES
fmt/7.0.1
spdlog/1.8.0
boost/1.74.0
nlohmann_json/3.9.1
INSTALL_ARGS
--build missing
)

add_executable(
server

main.cpp
listener.cpp
session.cpp
)
target_link_libraries(
server

CONAN_PKG::fmt
CONAN_PKG::spdlog
CONAN_PKG::boost
CONAN_PKG::nlohmann_json
)
target_link_options(
server
PUBLIC

-static
-stdlib=libc++
-lc++abi
-fuse-ld=lld
)
Loading

0 comments on commit 55632a9

Please sign in to comment.