Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 27, 2024
1 parent c537d1c commit 6a55e88
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(SDL2_DIR "${CMAKE_BINARY_DIR}")

if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
find_package(Boost CONFIG REQUIRED)
find_package(OpenSSL CONFIG REQUIRED)
endif()

find_package(Ogg CONFIG REQUIRED)
Expand Down Expand Up @@ -104,5 +105,5 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
)

if (NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::boost OpenAL::OpenAL)
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::boost OpenAL::OpenAL openssl::openssl)
endif()
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ build: ## Build

configure: clean ## Configure
conan remote update conancenter --url https://center2.conan.io
conan install . --output-folder=build --build=missing --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Release
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON
conan install . --output-folder=build --build=missing --profile=default --settings compiler.cppstd=20 --settings build_type=Release
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=OFF -DSANDBOX=ON -DENABLE_PROFILING=ON
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def requirements(self):

if self.settings.os != "Emscripten":
self.requires("boost/1.86.0")
self.requires("openssl/3.3.2")

def generate(self):
tc = CMakeToolchain(self)
Expand Down
3 changes: 3 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#ifndef EMSCRIPTEN
#ifdef WEBSOCKET
#include <boost/asio.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#endif
#endif

Expand Down
62 changes: 49 additions & 13 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ EM_BOOL websocket_on_close(int, const EmscriptenWebSocketCloseEvent *event, void

socket::socket() noexcept
#ifndef EMSCRIPTEN
: _resolver(net::make_strand(_io_context)), _ws(net::make_strand(_io_context))
: _resolver(net::make_strand(_io_context)),
_ssl_context(boost::asio::ssl::context::tlsv13_client),
_ws(boost::asio::make_strand(_io_context), _ssl_context)
#endif
{
_queue.reserve(8);
#ifndef EMSCRIPTEN
#ifndef LOCAL
_ssl_context.set_verify_mode(boost::asio::ssl::verify_peer);
_ssl_context.set_default_verify_paths();
#endif
#endif
}

socket::~socket() noexcept {
Expand All @@ -58,7 +66,7 @@ socket::~socket() noexcept {
}
#else
_ws.async_close(websocket::close_code::normal, [](beast::error_code ec) {
UNUSED(ec);
std::cerr << "[socket] websocket close error: " << ec.message() << std::endl;
});
#endif
}
Expand Down Expand Up @@ -89,13 +97,8 @@ void socket::connect() noexcept {
emscripten_websocket_set_onclose_callback(_socket, this, websocket_on_close);
#else
_resolver.async_resolve(
#ifdef LOCAL
"localhost",
"3000",
#else
"carimbo.run",
"443",
#endif
beast::bind_front_handler(&socket::on_resolve, this)
);

Expand Down Expand Up @@ -168,7 +171,19 @@ void socket::on_resolve(beast::error_code ec, tcp::resolver::results_type result
}

beast::get_lowest_layer(_ws).expires_after(std::chrono::seconds(30));
beast::get_lowest_layer(_ws).async_connect(results, beast::bind_front_handler(&socket::on_connect, this));
beast::get_lowest_layer(_ws).async_connect(
results,
beast::bind_front_handler(
&socket::on_connect,
this
)
);

// auto &layer = _ws.next_layer();
// layer.async_connect(
// results,
// boost::beast::bind_front_handler(&socket::on_connect, this)
// );
}

void socket::on_connect(beast::error_code ec, const tcp::resolver::results_type::endpoint_type &endpoint) noexcept {
Expand All @@ -180,15 +195,30 @@ void socket::on_connect(beast::error_code ec, const tcp::resolver::results_type:
}

_connected = true;
beast::get_lowest_layer(_ws).expires_after(std::chrono::seconds(30));

SSL_set_tlsext_host_name(_ws.next_layer().native_handle(), "carimbo.run");

_ws.next_layer().async_handshake(
ssl::stream_base::client,
beast::bind_front_handler(
&socket::on_ssl_handshake,
this
)
);
}

void socket::on_ssl_handshake(beast::error_code ec) noexcept {
if (ec) {
std::cerr << "[socket] ssl handshake error: " << ec.message() << std::endl;
return;
}

beast::get_lowest_layer(_ws).expires_never();
_ws.set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));

_ws.async_handshake(
#ifdef LOCAL
"localhost",
#else
"carimbo.run",
#endif
"/socket",
beast::bind_front_handler(&socket::on_handshake, this)
);
Expand Down Expand Up @@ -219,7 +249,13 @@ void socket::on_read(beast::error_code ec, std::size_t bytes_transferred) noexce
}

void socket::do_read() {
_ws.async_read(_buffer, beast::bind_front_handler(&socket::on_read, this));
_ws.async_read(
_buffer,
beast::bind_front_handler(
&socket::on_read,
this
)
);
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions src/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "common.hpp"

namespace network {

#ifndef EMSCRIPTEN
namespace beast = boost::beast;
namespace websocket = beast::websocket;
namespace net = boost::asio;
namespace ssl = net::ssl;
using tcp = net::ip::tcp;
#endif

Expand All @@ -32,6 +32,7 @@ class socket {
#ifndef EMSCRIPTEN
void on_resolve(beast::error_code ec, tcp::resolver::results_type results) noexcept;
void on_connect(beast::error_code ec, const tcp::resolver::results_type::endpoint_type &endpoint) noexcept;
void on_ssl_handshake(beast::error_code ec) noexcept;
void on_handshake(beast::error_code ec) noexcept;
void on_read(beast::error_code ec, std::size_t bytes_transferred) noexcept;
void do_read();
Expand All @@ -50,10 +51,9 @@ class socket {
#else
net::io_context _io_context;
tcp::resolver _resolver;
websocket::stream<beast::tcp_stream> _ws;
boost::asio::ssl::context _ssl_context;
websocket::stream<ssl::stream<beast::tcp_stream>> _ws;
beast::flat_buffer _buffer;
// std::string host_;
// std::string port_;
std::function<void(const std::string &)> _on_message;
#endif
};
Expand Down

0 comments on commit 6a55e88

Please sign in to comment.