Added websockets as well (nw)

This commit is contained in:
Miodrag Milanovic 2017-01-04 19:22:13 +01:00
parent afb373f8fd
commit 74b24ecefb
6 changed files with 51 additions and 4 deletions

View File

@ -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();
}
}

View File

@ -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;
};

View File

@ -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 */

View File

@ -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 */

View File

@ -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 */

View File

@ -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) {}
};
}