diff --git a/src/emu/main.cpp b/src/emu/main.cpp index 07043960027..e8e07ea52b1 100644 --- a/src/emu/main.cpp +++ b/src/emu/main.cpp @@ -11,6 +11,7 @@ Controls execution of the core MAME system. #include "emu.h" #include "emuopts.h" #include "main.h" +#include "server_ws.hpp" #include "server_http.hpp" #include @@ -132,9 +133,13 @@ void machine_manager::start_http_server() m_server = std::make_unique(); m_server->m_config.port = options().http_port(); m_server->set_io_context(m_io_context); - std::string doc_root = options().http_root(); + m_wsserver = std::make_unique(); + + auto& endpoint = m_wsserver->endpoint["/"]; + + m_server->on_get([this](auto response, auto request) { + std::string doc_root = this->options().http_root(); - m_server->on_get([this, doc_root](auto response, auto request) { std::string path = request->path; // If path ends in slash (i.e. is a directory) then add "index.html". if (path[path.size() - 1] == '/') @@ -173,6 +178,23 @@ void machine_manager::start_http_server() response->status(200).send(content); }); + + endpoint.on_open = [&](auto connection) { + auto send_stream = std::make_shared(); + *send_stream << "update_machine"; + m_wsserver->send(connection, send_stream, [](const std::error_code& ec) { }); + }; + + m_server->on_upgrade = [this](auto socket, auto request) { + auto connection = std::make_shared(socket); + connection->method = std::move(request->method); + connection->path = std::move(request->path); + connection->http_version = std::move(request->http_version); + connection->header = std::move(request->header); + connection->remote_endpoint_address = std::move(request->remote_endpoint_address); + connection->remote_endpoint_port = request->remote_endpoint_port; + m_wsserver->upgrade(connection); + }; m_server->start(); } } diff --git a/src/emu/main.h b/src/emu/main.h index 42cc8a3e233..3a5f5a5c689 100644 --- a/src/emu/main.h +++ b/src/emu/main.h @@ -73,6 +73,7 @@ namespace asio namespace webpp { class http_server; + class ws_server; } class machine_manager @@ -104,8 +105,9 @@ protected: osd_interface & m_osd; // reference to OSD system emu_options & m_options; // reference to options running_machine * m_machine; - std::shared_ptr m_io_context; + std::shared_ptr m_io_context; std::unique_ptr m_server; + std::unique_ptr m_wsserver; std::thread m_server_thread; }; diff --git a/src/lib/util/client_ws.hpp b/src/lib/util/client_ws.hpp index 52f294bc983..f3c4ded6bb2 100644 --- a/src/lib/util/client_ws.hpp +++ b/src/lib/util/client_ws.hpp @@ -507,6 +507,11 @@ namespace webpp { }); } }; + + class ws_client : public SocketClient { + public: + explicit ws_client(const std::string& server_port_path) : SocketClient::SocketClient(server_port_path) {} + }; } #endif /* CLIENT_WS_HPP */ diff --git a/src/lib/util/client_wss.hpp b/src/lib/util/client_wss.hpp index ae00c0346cb..ef30b459607 100644 --- a/src/lib/util/client_wss.hpp +++ b/src/lib/util/client_wss.hpp @@ -70,6 +70,13 @@ namespace webpp { }); } }; + + class wss_client : public SocketClient { + public: + explicit wss_client(const std::string& server_port_path, bool verify_certificate = true, + const std::string& cert_file = std::string(), const std::string& private_key_file = std::string(), + const std::string& verify_file = std::string()) : SocketClient::SocketClient(server_port_path, verify_certificate, cert_file, private_key_file, verify_file) {} + }; } #endif /* CLIENT_WSS_HPP */ diff --git a/src/lib/util/server_ws.hpp b/src/lib/util/server_ws.hpp index 94437026196..170dc31ae63 100644 --- a/src/lib/util/server_ws.hpp +++ b/src/lib/util/server_ws.hpp @@ -64,6 +64,7 @@ namespace webpp { } }; + class Connection { friend class SocketServerBase; friend class SocketServer; @@ -141,7 +142,7 @@ namespace webpp { catch (...) {} } }; - + class Message : public std::istream { friend class SocketServerBase; @@ -704,5 +705,10 @@ namespace webpp { }); } }; + + class ws_server : public SocketServer { + public: + ws_server() : SocketServer::SocketServer() {} + }; } #endif /* SERVER_WS_HPP */ diff --git a/src/lib/util/server_wss.hpp b/src/lib/util/server_wss.hpp index 000c00bf83d..ecf46fb1524 100644 --- a/src/lib/util/server_wss.hpp +++ b/src/lib/util/server_wss.hpp @@ -70,6 +70,11 @@ namespace webpp { }); } }; + + class wss_server : public SocketServer { + public: + wss_server(const std::string& cert_file, const std::string& private_key_file,const std::string& verify_file = std::string()) : SocketServer::SocketServer(cert_file, private_key_file, verify_file) {} + }; }