summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author Miodrag Milanovic <mmicko@gmail.com>2017-01-04 19:22:13 +0100
committer Miodrag Milanovic <mmicko@gmail.com>2017-01-04 19:22:13 +0100
commit74b24ecefb2c9bff09f88c400ba5f27fc64f7a7f (patch)
tree845c0a80ce7fbf8370d9770c951e62a584ef5354
parentafb373f8fd4cadfbb123bffc4b8335b4b90be4aa (diff)
Added websockets as well (nw)
-rw-r--r--src/emu/main.cpp26
-rw-r--r--src/emu/main.h4
-rw-r--r--src/lib/util/client_ws.hpp5
-rw-r--r--src/lib/util/client_wss.hpp7
-rw-r--r--src/lib/util/server_ws.hpp8
-rw-r--r--src/lib/util/server_wss.hpp5
6 files changed, 51 insertions, 4 deletions
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 <fstream>
@@ -132,9 +133,13 @@ void machine_manager::start_http_server()
m_server = std::make_unique<webpp::http_server>();
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<webpp::ws_server>();
+
+ 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<webpp::ws_server::SendStream>();
+ *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<webpp::ws_server::Connection>(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<asio::io_context> m_io_context;
+ std::shared_ptr<asio::io_context> m_io_context;
std::unique_ptr<webpp::http_server> m_server;
+ std::unique_ptr<webpp::ws_server> 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<WS> {
+ public:
+ explicit ws_client(const std::string& server_port_path) : SocketClient<WS>::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<WSS> {
+ 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<WSS>::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<socket_type>;
friend class SocketServer<socket_type>;
@@ -141,7 +142,7 @@ namespace webpp {
catch (...) {}
}
};
-
+
class Message : public std::istream {
friend class SocketServerBase<socket_type>;
@@ -704,5 +705,10 @@ namespace webpp {
});
}
};
+
+ class ws_server : public SocketServer<WS> {
+ public:
+ ws_server() : SocketServer<WS>::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<WSS> {
+ public:
+ wss_server(const std::string& cert_file, const std::string& private_key_file,const std::string& verify_file = std::string()) : SocketServer<WSS>::SocketServer(cert_file, private_key_file, verify_file) {}
+ };
}